On Mar 19, 2004, at 12:48 PM, Imobach GonzáÍez Sosa wrote:

> Hi all,
>
> I've got a little curiosity about defining methods. As far as I know 
> (and
> maybe I'm wrong) functions doesn't exist in Ruby... right? Ruby only 
> have
> methods.

Correct, Ruby doesn't have functions... But at tiems, Ruby's methods 
can *look* like functions...

> However, I can do this:
>
> <code>
> #!/usr/bin/env ruby
>
> def test
>         puts "This is a test"
> end
>
> test
> </code>
>
> If test is really a method... what class it belongs to? Or is it a 
> function
> and I've missed something?

When you define a method at the top level of your script (as in your 
example), the method is added to Object. So:

   def one
     puts "one"
   end

is equivalent to:

   class Object
     def two
       puts "two"
     end
   end

This makes it so that if you define a method at the top level, you can 
use it inside any classes you define, since they will all inherit the 
method from Object.

> Thank you.

Sure :)

> PD: OK, I know, I'm more worried about conceptual point of view that 
> I'd must
> be ;-)

This is actually a fairly common question... I don't think it's on the 
FAQ, though.