6 ms·
Cryptic Ruby Global Variables and Their Meanings
- LoseThosMan 14y agoIf you sat down for a formal dinner with a space alien, you act like the table should have dinner forks and spoons arranged victorian style, like that's naturally how all beings in the universe arrange dinner tables. What a retard.
- telemachos 14y agoFor 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-defined-variables http://zenspider.com/Languages/Ruby/QuickRef.html#pre-define... [2]: http://zenspider.com/Languages/Ruby/QuickRef.html#pre-defined-global-constants http://zenspider.com/Languages/Ruby/QuickRef.html#pre-define... [3]: http://zenspider.com/Languages/Ruby/QuickRef.html http://zenspider.com/Languages/Ruby/QuickRef.html
- dlisboa 14y ago$YOUR_RUBY_PATH/lib/ruby/1.9.1/English.rb has the full list, with pretty decent explanation.
- twelvechairs 14y agoalso here http://stdlib.rubyonrails.org/libdoc/English/rdoc/files/English_rb.html http://stdlib.rubyonrails.org/libdoc/English/rdoc/files/Engl...
- Empact 14y agoYep, if you must use the variables, much much better to use "require 'english'" and these more meaningful names than the cryptic originals.
- zenspider 14y agoI have a TODO in omnifocus to fold those in. It's a shame I didn't do that earlier.
- draegtun 14y agoI 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/English.html | http://perldoc.perl.org/perlvar.html http://perldoc.perl.org/perlvar.html
- ef4 14y agoThis is one area where Matz kept too much of Perl. Thankfully, it's rare to see Ruby programmers making heavy use of these.
- rjbond3rd 14y agoIt'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 $!)
- wonnage 14y agoThe 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.
- briangonzalez 14y agoTrue, but I have seen -- $:.unshift File.dirname(__FILE__) -- used quite a bit in the wild, especially in MacRuby projects.
- vidarh 14y ago$: and $1 .. $9 are relatively frequently used. Most of the other ones are rare - several of them I've never seen used at all.
- charliesome 14y agoI use $1, $2, $3, etc. quite often. $' and $` are also useful for throwaway scripts.
- riffraff 14y agofor throwaway irb sessions, I often use stuff like foo.map { |x| x.something rescue $! }
- Xylakant 14y agoI 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 :)
- damncabbage 14y agoWhen 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>