News for hackers. Stuff that matters.
Re:Cryptic Ruby Global Variables and Their Meanings by telemachos on Sunday September 16, 2012 @09:36PM (#4530995)
For a longer list (though the explanations are briefer), see Zenspider's Ruby QuickRef[1]. It also includes a handy listing of predefined global constants[2] (and a lot more).[3]

[1]: http://zenspider.com/Languages/Ruby/QuickRef.html#pre-define...

[2]: http://zenspider.com/Languages/Ruby/QuickRef.html#pre-define...

[3]: http://zenspider.com/Languages/Ruby/QuickRef.html

Re:Cryptic Ruby Global Variables and Their Meanings by dlisboa on Sunday September 16, 2012 @10:02PM (#4531053)
$YOUR_RUBY_PATH/lib/ruby/1.9.1/English.rb has the full list, with pretty decent explanation.
Re:Cryptic Ruby Global Variables and Their Meanings by twelvechairs on Sunday September 16, 2012 @10:45PM (#4531144)
Re:Cryptic Ruby Global Variables and Their Meanings by Empact on Monday September 17, 2012 @12:22AM (#4531321)
Yep, if you must use the variables, much much better to use "require 'english'" and these more meaningful names than the cryptic originals.
Re:Cryptic Ruby Global Variables and Their Meanings by zenspider on Monday September 17, 2012 @03:20AM (#4531582)
I have a TODO in omnifocus to fold those in. It's a shame I didn't do that earlier.
Re:Cryptic Ruby Global Variables and Their Meanings by draegtun on Monday September 17, 2012 @05:04AM (#4531749)
I didn't realise Ruby also had this. Here's the Perl corollary:

  perldoc English
  perldoc perlvar
links: http://perldoc.perl.org/English.html | http://perldoc.perl.org/perlvar.html
Re:Cryptic Ruby Global Variables and Their Meanings by ef4 on Sunday September 16, 2012 @10:47PM (#4531150)
This is one area where Matz kept too much of Perl. Thankfully, it's rare to see Ruby programmers making heavy use of these.
Re:Cryptic Ruby Global Variables and Their Meanings by wonnage on Monday September 17, 2012 @12:33AM (#4531336)
The former ubiquity of Perl made most of these common knowledge. For example, $? works in both Perl and bash (actually, I don't know which did this first...). So in the context of shell scripts, where Ruby started, having these magic globals makes a lot of sense.

IMO they can make code easier to understand, once you've learned what they mean. $2 conveys the same idea as matches[2] without having to think about array indices, and why the matches aren't zero-indexed like a normal array. This is obviously totally subjective.

Re:Cryptic Ruby Global Variables and Their Meanings by draegtun on Monday September 17, 2012 @07:33AM (#4532067)
Especially when Ruby has no way of scoping these variables... unlike Perl:

  {
      # level 1
      local $\ = "[1]\n";
      some_print_operation();
  
      {
          # level 2
          local $\ = "[2]\n";
          some_print_operation();
      }

      # level 1 again
      some_print_operation();
  }

  # $\ back to what it was before all this :)
Re:Cryptic Ruby Global Variables and Their Meanings by briangonzalez on Monday September 17, 2012 @02:09AM (#4531457)
True, but I have seen -- $:.unshift File.dirname(__FILE__) -- used quite a bit in the wild, especially in MacRuby projects.
Re:Cryptic Ruby Global Variables and Their Meanings by vidarh on Monday September 17, 2012 @05:20AM (#4531784)
$: and $1 .. $9 are relatively frequently used. Most of the other ones are rare - several of them I've never seen used at all.
Re:Cryptic Ruby Global Variables and Their Meanings by rjbond3rd on Sunday September 16, 2012 @11:16PM (#4531198)
It's also rare to see them heavily used in Perl nowadays. Lots of recent code doesn't use them at all (e.g., with autodie, no need for $!)
Re:Cryptic Ruby Global Variables and Their Meanings by ZoFreX on Monday September 17, 2012 @06:19AM (#4531897)
If you write your shell scripts in Ruby they do come in handy. I certainly wouldn't use them outside of that context.
Re:Cryptic Ruby Global Variables and Their Meanings by charliesome on Monday September 17, 2012 @02:19AM (#4531469)
I use $1, $2, $3, etc. quite often. $' and $` are also useful for throwaway scripts.
Re:Cryptic Ruby Global Variables and Their Meanings by riffraff on Monday September 17, 2012 @03:50AM (#4531633)
for throwaway irb sessions, I often use stuff like

    foo.map { |x| x.something rescue $! }
Re:Cryptic Ruby Global Variables and Their Meanings by damncabbage on Monday September 17, 2012 @06:37AM (#4531945)
When using the debugger you can sometimes end up in an exception handler that doesn't provide a way to get access to said exception. $! can be a life-saver to help figure out what's going on, eg.

  (rdb:1) list
     11    do_something_stupid
     12  rescue
  => 13    log("Something went wrong")
     14  end
 
  (rdb:2) e $!
  #<Errno::ENOENT: No such file or directory - /tmp/durr>
Re:Cryptic Ruby Global Variables and Their Meanings by Xylakant on Monday September 17, 2012 @06:06AM (#4531869)
I rarely see those used except when you look at codegolf contests. There you can find all of those variables put to good and creative use :)