Ruby is filled with a series of hidden variables. We can get some interesting information from these predefined global variables.
Global process variable
$ Indicates the currently running ruby process.
Copy codeThe Code is as follows: >>$ $
=> 17170
We can kill ourselves from the current process.
Copy codeThe Code is as follows:
> 'Kill-9 # {$ $ }'
[1] 17170 killed irb
$? Indicates the status of the latest sub-process.
Copy codeThe Code is as follows:
> 'Echo hello'
=> "Hello \ n"
>>$?
### <Process: Status: pid 18048 exit 0>
>>$ ?. Success?
=> True
Exceptions and errors
$1 indicates the information that causes the exception. For example, here raise "there's no peanut butter", its value is there's no peanut butter.
Copy codeThe Code is as follows:
> Begin raise "this town ain't big enough for the both of us" rescue puts $! End
This town ain't big enough for the both of us
=> Nil
$ @ Provides complete stack call information that causes errors. It is an array.
Copy codeThe Code is as follows:
> Begin raise 'no soup in kitchen' rescue $ @. each {| trace | puts trace} end
(Irb): 13: in 'irb _ binding'
/Home/meck/. rvm/rubian/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/rubian/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 'loan'
/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/rubian/ruby-1.9.3-p194/lib/ruby/1.9.1/irb. rb: 70: in 'block in start'
/Home/meck/. rvm/rubian/ruby-1.9.3-p194/lib/ruby/1.9.1/irb. rb: 69: in 'catch'
/Home/meck/. rvm/rubian/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/>'
=> ["(> '"]
String and Separator
$; Represents the Separator in String. split. The default Delimiter is space.
Copy codeThe Code is as follows:
> "One Spaceship, Two Tiny Tanks, Three Misplaced Socks". split
=> ["One Spaceship", "Two Tiny Tanks", "Three Misplaced Socks"]
>>$; = ","
=> ","
> "One Spaceship, Two Tiny Tanks, Three Misplaced Socks". split
=> ["One Spaceship", "Two Tiny Tanks", "Three Misplaced Socks"]
$, Used in Array. join and Kernel. print. The default value is nil.
> ['One', 'two', 'three ', 'green']. join
=> "Onetwothreegreen"
>>$, = "-"
=> "-"
> ['One', 'two', 'three ', 'green']. join
=> "One-two-three-green"
$/Represents the row Separator Used to read the input. It is used in Kernel. gets. It usually indicates a new line, but can be modified. This is hard to display, because irb depends on \ n as the read separator. If $/is set to nil, gets will read the entire file.
$ \ Is the opposite. It is the line Separator of the output.
Copy codeThe Code is as follows:
>>$ \ = "Mooooooo"
=> "Mooooooo"
> Puts
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>
File
Suppose there is a file named letter. text:
Copy codeThe Code is as follows:
Dear Caroline,
I think we need some honey for tea.
I also think that I may have misplaced my red tie, have you seen it?
-Nick
$. Indicates the row number of the file currently being read.
Copy codeThe Code is as follows:
> Open('letter.txt '). each {| line | puts "#{$. }:#{ line }"}
1: Dear Caroline,
2: I think we need some honey for tea.
3: I also think that I may have misplaced my red tie, have you seen it?
4:
5:-Nick
==## <File: letter.txt>
$ _ Indicates the last row to be read.
Copy codeThe Code is as follows:
> Open('letter.txt '). each {| line | puts $ _. nil? }
True
True
True
True
True
==## <File: letter.txt>
Matching and Regular Expressions
$ ~ Indicates the information obtained from the last regular expression match. If any, it returns the MatchData example. Otherwise, it is nil.
Copy codeThe Code is as follows:
> "The robots are coming, the robots are coming, the robots are coming" = ~ /Ro/
=> 4
>>$ ~
==## <MatchData "ro">
>>$ ~. To_s
=> "Ro"
> "The robots are coming, the robots are coming, the robots are coming" = ~ /Cats/
=> Nil
>>$ ~
$ & With $ ~ Very similar. It returns the last matched string.
Copy codeThe Code is as follows:
> "The robots are coming, the robots are coming, the robots are coming" = ~ /Ro/
=> 4
>>$ &
$ 'Indicates matching strings without any backend.
Copy codeThe Code is as follows:
> "There were once ten tin robots standing in a row." = ~ /Robot/
=> 24
>>$'
=> "S standing in a row ."
=> "Ro"
=> Nil
Others
$> Indicates the default output object of ruby, which is used in Kernel. print.
Copy codeThe Code is as follows:
>>$ >=$ Stderr
==## <IO: <STDERR>
> Puts '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/rubian/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 'loan'
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/rubian/ruby-1.9.3-p194/lib/ruby/1.9.1/irb. rb: 155: in 'eval _ input'
From/home/meck/. rvm/rubian/ruby-1.9.3-p194/lib/ruby/1.9.1/irb. rb: 70: in 'block in start'
From/home/meck/. rvm/rubian/ruby-1.9.3-p194/lib/ruby/1.9.1/irb. rb: 69: in 'catch'
From/home/meck/. rvm/rubian/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. It represents an array containing all the variables passed to the ruby file. Suppose there is an argument_echoer.rb file:
Copy codeThe Code is as follows:
$ *. Each {| arg | puts arg}
Run it:
Copy codeThe Code is as follows:
$ Ruby argument_echoer.rb Who What When Where and Why
Who
What
When
Where
And
Why