From: "DaShiell, Jude T. CIV NAVAIR 1490, 1, 26" Date: 2010-09-30T03:52:36+09:00 Subject: Re: The beauty of Ruby through examples I take it rand() gets pseudorandom numbers from computer and as such could be improved in one respect at least if ruby can be made to sleep or wait for random numbers of milliseconds. One complication with random number generation is that two numbers can come off the belt in the same millisecond. However making ruby wait a random number of milliseconds between each sample draw sample draw is the random numbers to provide the user would solve that problem. I just found the Pickaxe book the other day and am reading through it and learning. The thing I don't know is how much solving this problem with pseudorandom numbers would improve quality of samples. -----Original Message----- From: Adriano Ferreira [mailto:adrfer@yahoo.com] Sent: Wednesday, September 29, 2010 14:41 To: ruby-talk ML Subject: The beauty of Ruby through examples 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