On Dec 6, 4:24 pm, Lee Jarvis <ljjar... / gmail.com> wrote:
> Ruby, like Perl, reads the code line by line, the same as you write it..
>
> So you need to define methods before you call them, (this is interpreted
> programming)

As a contrived example, see this code:

digit = rand 9
self.class.class_eval{
  define_method( "foo#{digit}" ){
    puts "The secret number is #{digit}"
  }
}

foo0 rescue puts "It's not 0"
foo1 rescue puts "It's not 1"
foo2 rescue puts "It's not 2"
foo3 rescue puts "It's not 3"
foo4 rescue puts "It's not 4"
foo5 rescue puts "It's not 5"
foo6 rescue puts "It's not 6"
foo7 rescue puts "It's not 7"
foo8 rescue puts "It's not 8"
foo9 rescue puts "It's not 9"

#=> It's not 0
#=> The secret number is 1
#=> It's not 2
#=> It's not 3
#=> It's not 4
#=> It's not 5
#=> It's not 6
#=> It's not 7
#=> It's not 8
#=> It's not 9

The method doesn't exist until created by define_method. Something
similar to define_method occurs when you write "def bar ... end"