What is $ _ [0], $ _ [1] in Ruby?

I am a Java developer, and I was provided with Ruby code for understanding, and then for working.

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

It is assigned to a variable in the code, and it is definitely not a command line argument, because I wrote the code to check this, and it failed. So, can anyone tell what its meaning is?

+6
source share
2 answers

This is one of the magic variables.

$_ contains the value of the last line read from standard input. $_[0] is therefore the first character of this string.

See English.rb for more magic variables.

 # 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 $_ 
+13
source

$_ - the line read last gets

[0] , of course, is indexed into this string.

http://www.rubyist.net/~slagell/ruby/globalvars.html

+5
source

All Articles