6. Ruby Loops [“cat”, “dog”, “giraffe”].each do |s| puts s end [“cat”, “dog”, “giraffe”].each { |s| puts s }
7. Working with Arrays animals = [“zebra”, “donkey”, “lemur”] animals.sort[“donkey”, “lemur”, “zebra”] # original array is untouched animals.sort! # original array is modified! animals.delete“zebra” [“donkey”, “lemur”]
8. Ruby has Less Ceremony //C# Dictionary<string, object> hash = new Dictionary<string, object>(); #ruby hash = {} #optional semicolons & parentheses add(5, 6); is the same as add 5, 6
14. Speaking of hpricot… require ‘rubygems’ require ‘hpricot’ require ‘open-uri’ uri = open(“http://whatsit.org”) doc = Hpricot(uri) (doc/"p/a/img").each do |img| puts img.attributes['class'] end
15. Rake task :codeGen do # do the code generation end task :compile => :codeGen do #do the compilation end task :dataLoad => :codeGen do # load the test data end task :test => [:compile, :dataLoad] do # run the tests end
16. RSpec # bowling_spec.rb require 'bowling' describe Bowling do it "should score 0 for gutter game" do bowling = Bowling.new 20.times { bowling.hit 0 } bowling.score.should == 0 end end