From: Adriano Ferreira Date: 2010-09-30T03:40:35+09:00 Subject: The beauty of Ruby through examples --0-506172408-1285785632=:79961 Content-Type: text/plain; charset=us-ascii Hey all, I'm about to introduce Ruby to a group of people that is not familiar to the language, and the approach i'd like to take is simply by distilling out a few sample codes. I've been collecting some from around the internet, but i'd appreciate your suggestions as well. I'm looking for code snippets that represent some of the beauty of Ruby actually, such as: # Simplicity # Expressiveness # Productivity # Flexibility # Dynamism # Freedom # Happiness Just to clear things up, here are few examples: # Simplicity vowels = %w(a e i o u) alfabet = ('a'..'z').to_a cons = alfabet - vowels p cons # Expressiveness hello = 'Hello Ruby' 5.times { puts hello } # Productivity class Person # def name # @name # end # # def name=(other) # @name = other # end # # def age # @age # end # # def age=(other) # @age = other # end attr_accessor :name, :age end john = Person.new john.name = 'John' john.age = 25 p john # Flexibility class Array def pick self[rand(self.size)] end alias :choose :pick end puts %w(1 2 3 4 5).pick puts %w(1 2 3 4 5).choose # Dynamism Padawan = Class.new class Jedi def train(padawan) def padawan.control_the_force puts "Now i'm ready to become a Jedi!" end end end skywalker = Padawan.new yoda = Jedi.new p skywalker.respond_to? :control_the_force # => false yoda.train skywalker p skywalker.respond_to? :control_the_force # => true skywalker.control_the_force# => Now i'm ready to become a Jedi! ...and so on. Any more examples? Thanks in advance, Adriano --0-506172408-1285785632=:79961--