From: dblack@... Date: 2002-11-20T09:53:00+09:00 Subject: Re: Learning Ruby Hi -- On Wed, 20 Nov 2002, Daniel Carrera wrote: > Hi, > > I am a Perl hacker trying to get to know Ruby. I have two questions: Welcome :-) Partial response: > I noticed that Ruby doesn't let me use functions until after I define > them (unlike Perl). Why is that? I want to have the freedom to define > the functions anywhere. Pretty much everything happens in lexical sequence in Ruby, so if you ask for a method to be called, it has to exist. As a corollary, you can also redefine methods: def my_method puts "hi" end my_method # hi def my_method puts "hello" end my_method # hello This is a pretty dinky example -- but you can see that having a call to my_method before any version of it was defined wouldn't make sense. Nor is it possible to do some kind of lookahead where the last definition of a method is known in advance. Method definition may be conditional on runtime state, so there's no way to anticipate which definition is "last". Another way to look at it is: a Ruby object responds to messages (i.e., method names), and the set of messages it can respond to is subject to change during the life of the object. Therefore, there's no way to do method existence lookahead. The transaction -- the method call -- always succeeds or fails based on the capacities of the object at the time of the call. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav