Ruby is filled with a series of hidden variables, and we can get some interesting information from these predefined global variables.
Global process variables
$$ represents the currently running Ruby process.
Copy Code code as follows:
We can kill ourselves from the current process.
Copy Code code as follows:
>> ' kill-9 #{$$} '
[1] 17170 killed IRB
$? Represents the state of the most recent child process
Copy Code code as follows:
>> ' echo Hello '
=> "hello\n"
>> $?
=> #<process::status:pid 18048 Exit 0>
>> $? Success?
=> true
Exceptions and Errors
Represents the information that caused the exception. For example here raise "there ' s no peanut butter", its value is there ' s no peanut butter.
Copy Code code as follows:
>> begin raise "This town Ain ' t big enough for the both of us" rescue puts $! End
This is town ain ' t big enough for the both of us
=> Nil
$@ can give the complete stack call information that caused the error, which is an array.
Copy Code code as follows:
>> begin raise ' no soup in kitchen ' rescue $@.each {|trace| puts trace} end
(IRB): 13:in ' irb_binding '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in ' eval '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in ' Evaluate '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/context.rb:254:in ' Evaluate '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:159:in ' block (2 levels) in Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273:in ' Signal_status '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156:in ' block in Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in ' block (2 levels) in Each_top_level_ Statement
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' Loop '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' block in Each_top_level_statement '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Catch '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Each_top_level_statement '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155:in ' Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70:in ' block in Start '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Catch '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Start '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/>> '
=> ["(>>"]
Strings and delimiters
$; Represents the delimiter in the String.Split, and the default is a space.
Copy Code code as follows:
>> "One spaceship, two Tiny tanks, Three misplaced". Split
=> ["One Spaceship", "two Tiny tanks", "Three misplaced Socks"]
>> $; = ","
=> ","
>> "One spaceship, two Tiny tanks, Three misplaced". Split
=> ["One Spaceship", "two Tiny tanks", "Three misplaced Socks"]
$, used in Array.join and Kernel.print, the default is nil.
>> [' One ', ' two ', ' three ', ' Green '].join
=> "Onetwothreegreen"
>> $, = "-"
=> "-"
>> [' One ', ' two ', ' three ', ' Green '].join
=> "One-two-three-green"
$/the line separator that reads the input. It is used in kernel.gets. It usually represents a new line, but can be modified. This is difficult to show because IRB relies \ nthe read delimiter, and if $/is set to Nil,gets, the entire file is read.
$\, on the contrary, is the line separator for output.
Copy Code code as follows:
>> $\ = "Mooooooo"
=> "Mooooooo"
>> puts a
nameerror-:-undefined local variable or method ' a ' for main:object-
Mooooooo from (IRB): 25
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/>> '-
Mooooooo >> A
File
Suppose there's a file called Letter.text:
Copy Code code as follows:
Dear Caroline,
I do we need some honey for tea.
I also I may have misplaced me red tie, have you seen it?
-nick
$. Represents the line number that the file is currently being read.
Copy Code code as follows:
>> open (' letter.txt '). Each {|line| puts "#{$.}: #{line}"}
1:dear Caroline,
2:i We need some honey for tea.
3:I also I may have misplaced me red tie, have you seen it?
4:
5:-nick
=> #<file:letter.txt>
$_ represents the last read row.
Copy Code code as follows:
>> open (' letter.txt '). Each {|line| puts $_.nil?}
True
True
True
True
True
=> #<file:letter.txt>
Matching and regular expressions
$~ represents the most recent regular match to the information, if any, it returns the Matchdata example, otherwise it is nil.
Copy Code code as follows:
>> "The robots are coming, the are coming, the robots are Coming" =~/ro/
=> 4
>> $~
=> #<matchdata "RO" >
>> $~.to_s
=> "Ro"
>> "The robots are coming, the are coming, the robots are Coming" =~/cats/
=> Nil
>> $~
$& is very similar to $~, which returns the last string that was matched.
Copy Code code as follows:
>> "The robots are coming, the are coming, the robots are Coming" =~/ro/
=> 4
>> $&
$ ' indicates a string that matches the following.
Copy Code code as follows:
>> "There were once ten tin robots standing in a row." =~/robot/
=> 24
>> $ '
=> "s standing in a row."
=> "Ro"
=> Nil
Other
$> represents the Ruby default output object, used in Kernel.print.
Copy Code code as follows:
>> $> = $> = $stderr
=> #<io:<stderr>>
>> puts ' no no no '
No no No
=> Nil
>> $> = $stdin
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' write ': not opened for writing (IOError)
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' print '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' block (2 levels) in Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273:in ' Signal_status '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156:in ' block in Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in ' block (2 levels) in Each_top_ Level_statement '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' Loop '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' block in Each_top_level_ Statement
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Catch '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Each_top_level_statement '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155:in ' Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70:in ' block in Start '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Catch '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Start '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/irb:12:in ' <main> '
$* is probably the most common global variable, representing an array of all the variables passed to the Ruby file, assuming there is a file called argument_echoer.rb:
Copy Code code as follows:
Run it:
Copy Code code as follows:
$ ruby argument_echoer.rb who What Where and Why
W.H.O.
What
When
Where
and
Why