On Sep 29, 1:40=A0pm, Adriano Ferreira <adr... / yahoo.com> wrote:
> [Note: =A0parts of this message were removed to make it a legal post.]
>
> 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 =3D %w(a e i o u)
> alfabet =3D ('a'..'z').to_a
> cons =3D alfabet - vowels
> p cons
>
> # Expressiveness
>
> hello =3D 'Hello Ruby'
> 5.times { puts hello }
>
> # Productivity
>
> class Person
> =A0 # def name
> =A0 # =A0 @name
> =A0 # end
> =A0 #
> =A0 # def name=3D(other)
> =A0 # =A0 @name =3D other
> =A0 # end
> =A0 #
> =A0 # def age
> =A0 # =A0 @age
> =A0 # end
> =A0 #
> =A0 # def age=3D(other)
> =A0 # =A0 @age =3D other
> =A0 # end
>
> =A0 attr_accessor :name, :age
> end
>
> john =3D Person.new
> john.name =3D 'John'
> john.age =3D 25
> p john
>
> # Flexibility
>
> class Array
> =A0 def pick
> =A0 =A0 self[rand(self.size)]
> =A0 end
>
> =A0 alias :choose :pick
> end
>
> puts %w(1 2 3 4 5).pick
> puts %w(1 2 3 4 5).choose
>
> # Dynamism
>
> Padawan =3D Class.new
>
> class Jedi
> =A0 def train(padawan)
> =A0 =A0 def padawan.control_the_force
> =A0 =A0 =A0 puts "Now i'm ready to become a Jedi!"
> =A0 =A0 end
> =A0 end
> end
>
> skywalker =3D Padawan.new
> yoda =3D Jedi.new
> p skywalker.respond_to? :control_the_force =A0# =3D> false
>
> yoda.train skywalker
> p skywalker.respond_to? :control_the_force =A0# =3D> true
> skywalker.control_the_force# =3D> Now i'm ready to become a Jedi!
>
> ..and so on.
>
> Any more examples?
>
> Thanks in advance,
>
> Adriano

In comp.lang.lisp, this elisp code was posted as an example
of how to shuffle a vector:

(defun emms-shuffle-vector (vector)
  "Shuffle VECTOR."
  (let ((i (- (length vector) 1)))
    (while (>=3D i 0)
      (let* ((r (random (1+ i)))
             (old (aref vector r)))
        (aset vector r (aref vector i))
        (aset vector i old))
      (setq i (- i 1))))
  vector)

Ruby:

(9..20).sort_by{ rand }
    =3D=3D>[15, 14, 12, 18, 16, 10, 9, 13, 11, 20, 17, 19]