• An elegant, natural syntax • Roots in Lisp, Perl, & Smalltalk A dynamic, object-oriented programming language with: Created by Yukihiro Matsumoto, a.k.a. Matz
an object • Even strings and numbers # Output "UPPER" puts "upper".upcase # Output the absolute value of -5: puts -5.abs # Output "Ruby Rocks!" 5 times 5.times do puts "Ruby Rocks!" end
case statements: # Case statement with regular expression case lang when /ruby/i puts "Matz created Ruby!" when /perl/i puts "Larry created Perl!" else puts "I don't know who created #{lang}." end
Print out a list of of people from # each person in the Array people.each do |person| puts "* #{person.name}" end # A block using the bracket syntax 5.times { puts "Ruby rocks!" } # Custom sorting [2,1,3].sort! { |a, b| b <=> a }
to hand control over to the block: # define the thrice method def thrice yield yield yield end # Output "Blocks are cool!" three times thrice { puts "Blocks are cool!" }
# redefine the thrice method def thrice yield(1) yield(2) yield(3) end # Output "Blocks are cool!" three times, # prefix it with the count thrice { | i | puts "#{i}: Blocks are cool!" }
• Full support for introspection • Support for method and class redefinition • Create your own domain languages • Modules vs. multiple inheritance • method_missing, etc...
Duck class class Duck def quack puts "quack!" end end # The Mallard class (without inheritance) class Mallard def quack puts "qwuaacck!! quak!" end end
the creation of simple sub-languages called domain languages. An example from Rails: class Firm < ActiveRecord::Base has_many :clients has_one :account belongs_to :conglomorate end
model • Action Pack provides a tightly integrated view and controller: Action View & Action Controller Rails libraries form an MVC framework: Plus some: • Action Mailer • Active Support
for version control • Create a Recipes Model • Build a Recipes Controller • Dress up the Views • Twitter Bootstrap • Deploy to Heroku Let’s build a rails application from scratch!