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

后端 未结 2 624
孤城傲影
孤城傲影 2021-01-12 18:19

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

I went through the Ruby tutorials on tutorialspoint.com but I can\'t figur

相关标签:
2条回答
  • 2021-01-12 18:37

    It's one of the magic variables.

    $_ holds value of the last line read from standard input. $_[0] is, therefore, first symbol of that 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          $_
    
    0 讨论(0)
  • 2021-01-12 18:51

    $_ - string last read by gets

    [0] is of course, indexing into that string.

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

    0 讨论(0)
提交回复
热议问题