Hacker News new | past | comments | ask | show | jobs | submit login
Cryptic Ruby Global Variables and Their Meanings (jimneath.org)
49 points by briangonzalez on Sept 16, 2012 | hide | past | favorite | 17 comments



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


$YOUR_RUBY_PATH/lib/ruby/1.9.1/English.rb has the full list, with pretty decent explanation.



Yep, if you must use the variables, much much better to use "require 'english'" and these more meaningful names than the cryptic originals.


I have a TODO in omnifocus to fold those in. It's a shame I didn't do that earlier.


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


This is one area where Matz kept too much of Perl. Thankfully, it's rare to see Ruby programmers making heavy use of these.


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.


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 :)


True, but I have seen -- $:.unshift File.dirname(__FILE__) -- used quite a bit in the wild, especially in MacRuby projects.


$: and $1 .. $9 are relatively frequently used. Most of the other ones are rare - several of them I've never seen used at all.


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 $!)


If you write your shell scripts in Ruby they do come in handy. I certainly wouldn't use them outside of that context.


I use $1, $2, $3, etc. quite often. $' and $` are also useful for throwaway scripts.


for throwaway irb sessions, I often use stuff like

    foo.map { |x| x.something rescue $! }


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>


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 :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: