Ruby
From WhyNotWiki
This is mostly for information regarding how to use ruby -- tips and tricks and such -- which is probably what most people are most interested in most of the time. For general information about Ruby, including advocacy and analysis of, see Ruby / About. This article is also my primary repository for snippets and examples at the moment, although I'd like to move them out...
[edit] Learning Ruby
http://poignantguide.net/ruby/
http://wiki.rubygarden.org/Ruby/page/show/RubyIdioms RubyIdioms
[edit] Reference
http://www.rubycentral.com/book/ (Programming Ruby: The Pragmatic Programmer's Guide, First Edition) (buy the Second Edition instead!)
[edit] Cheat sheet / Quick reference
http://www.zenspider.com/Languages/Ruby/QuickRef.html (very good)
[edit] Miscellaneous / Examples
[edit] Data types: False values
The only false values are nil and false. In particular, 0 and '' are true! (http://woss.name/2006/05/07/notes-from-a-rails-course/)
[edit] Equivalent to Python's if __name__ == "__main__": main ()?
if $0 == __FILE__
main
end
[edit] Examples from Rubyisms in Rails
(Source: Rubyisms in Rails, by Jacob Harris. p. 28-29)
In Java:
Iterator iterator = birdlist.iterator();
while (iterator.hasNext()) {
Bird bird = (Bird) iterator.next();
if (bird.endangered()) {
call_scientists();
break;
}
}
In Ruby:
Detection:
call_scientists if birdlist.any? {|bird| bird.endangered? }
Selection:
common_birds = birds.select {|bird, count| count > 5 }
rare_birds = birds.reject {|bird, count| count > 5 }
Accumulation (summing up values):
total_bird_sightings = birds.inject {|total, bird| total += bird.count}
Searching:
winner = contestants.find {|x| x.has_answer? }
Reordering:
titles = jukebox.sort_by {|x| x.title }
Partitioning:
quick, dead = people.partition {|p| p.can_outrun? :bear }
:-)
Recombination:
blind_dates = restaurants.zip(men, women)
:-)
dates = [:OliveGarden, :TacoBell].zip([:David, :Fred], [:Sara, :Jane])
# => [[:OliveGarden, :David, :Sara], [:TacoBell, :Fred, :Jane]]
(/Source)
[edit] [Caveats (category)] ENV is not a Hash, like you'd expect, but an Object
It doesn't even have a proper class -- just a singleton class. Its class is "Object".
irb -> ENV.class
=> Object
irb -> puts ENV.class.ancestors
Object
PP::ObjectMixin
Kernel
It does behave like a Hash, however. And it is Enumerable.
{"TERM"=>"linux",
"SHELL"=>"/bin/bash",
"LOGNAME"=>"tylerrick",
"VISUAL"=>"/usr/bin/vim",
"LINES"=>"55",
"COLUMNS"=>"155",
...}
irb -> ENV.respond_to? :[]
=> true
irb -> ENV.respond_to? :to_a
=> true
irb -> ENV.respond_to? :to_hash
=> true
irb -> puts (class << ENV; self; end).ancestors
Enumerable
Object
PP::ObjectMixin
Kernel
I tried to check if there were any other constants like ENV that were just "Objects", but I didn't find any...
irb -> puts Object.constants.sort.map {|it| it + ': ' + Object.const_get(it).class.to_s }
...
[edit] Types/type conversions/type checking
irb -> nil.to_i
=> 0
"Coercion between types will use to_str, to_int, etc for attempting to coerce an object to a string/integer internally. to_s and to_i produce human-readable representations." (http://woss.name/2006/05/07/notes-from-a-rails-course/)
[edit] Meta-programming: eval
[edit] eval doesn't show the right line numbers!
You have to pass __FILE__ to eval or it will print something useless like (eval):4 in any backtraces, etc.
Grep Houston describes this problem well at http://ghouston.blogspot.com/2006/06/ruby-meta-programming-and-stack-traces.html
His solution/demonstration of his solution:
class Monkey
module_eval(<<-EOS, __FILE__, __LINE__)
def see
puts 'Hello World from Monkey.see'
puts caller(0)
end
EOS
end
Monkey.new.see
Hello World from Monkey.see Monkey-see.rb:5:in `see' Monkey-see.rb:11