Ruby 中的 $_[0] ,$_[1] 是什么? [英] What is $_[0] ,$_[1] in Ruby?

查看:35
本文介绍了Ruby 中的 $_[0] ,$_[1] 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Java 开发人员,我已经获得了 Ruby 代码以供理解并在以后继续工作.

I am a Java developer, and I have been given Ruby code to understand and later to work on.

我浏览了 tutorialspoint.com 上的 Ruby 教程,但我不知道 $_[0] 是什么.

I went through the Ruby tutorials on tutorialspoint.com but I can't figure out what $_[0] is.

它被分配给代码中的一个变量,它绝对不是命令行参数,因为我编写了代码来测试它并且失败了.那么,谁能说说它的意义是什么?

It is being assigned to a variable in the code, and it is definitely not a command-line argument because I wrote code to test that and it failed. So, can anyone say what the significance of it is?

推荐答案

这是神奇的变量之一.

$_ 保存从标准输入读取的最后一行的值.因此,$_[0] 是该字符串的第一个符号.

$_ holds value of the last line read from standard input. $_[0] is, therefore, first symbol of that string.

更多魔法变量见English.rb

# The last line read by <tt>Kernel.gets</tt> or
# <tt>Kernel.readline</tt>. Many string-related functions in the
# +Kernel+ module operate on <tt>$_</tt> by default. The variable is
# local to the current scope. Thread local.
alias $LAST_READ_LINE          $_

这篇关于Ruby 中的 $_[0] ,$_[1] 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆