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

后端 未结 2 661
孤城傲影
孤城傲影 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 Kernel.gets or
    # Kernel.readline. Many string-related functions in the
    # +Kernel+ module operate on $_ by default. The variable is
    # local to the current scope. Thread local.
    alias $LAST_READ_LINE          $_
    

提交回复
热议问题