Defining methods outside classes

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.

However, I can do this:

#!/usr/bin/env ruby

def test
puts "This is a test"
end

test

If test is really a method… what class it belongs to? Or is it a function
and I’ve missed something?

Thank you.

PD: OK, I know, I’m more worried about conceptual point of view that I’d must
be :wink:

···


(o_.’ Imobach González Sosa imobachgs@softhome.net
//\c{} imobachgs@step.es a2419@dis.ulpgc.es
V__)_ imodev@softhome.net osoh en jabber.at y jabber.org
Usuario Linux #201634
Debian GNU/Linux `Sarge’ con núcleo 2.4.24 sobre Intel Pentium 4

Juventud, ¿sabes que la tuya no es la primera generación que anhela una vida
plena de belleza y libertad?
– Albert Einstein. (1879-1955) Físico alemán.

Imobach González Sosa wrote:

Hi all,

Moin!

However, I can do this:
[snip]
If test is really a method… what class it belongs to? Or is it a function
and I’ve missed something?

It’s in module Kernel, which contains other function-style methods like
gets, puts and the like. These are available as private methods on all
objects. (Because Object include-s Kernel.)

Thank you.

No problem!

PD: OK, I know, I’m more worried about conceptual point of view that I’d must
be :wink:

Heh, might be interesting to know what matz thinks about it. :slight_smile:

Regards,
Florian Gross

Wrote Imobach González Sosa imodev@softhome.net, on Sat, Mar 20, 2004 at 05:48:14AM +0900:

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.

It’s a method of Object:

irb(main):001:0> def foo; end
=> nil
irb(main):002:0> Object.methods
=> [“send”, “name”, “class_eval”, “object_id”, “autoload?”, “new”,

“display”, “foo”, “>=”, “respond_to?”, “instance_method”, “<=”,
^^^^^^
“const_defined?”, “instance_of?”]
irb(main):003:0>

You are in the context of an object of class Object, so you can call foo
without specifying a receiver, so it looks like a “function”, but its a
method of your class.

This has got to be a FAQ, check the FAQ at ruby-lang.org.

Cheers,
Sam

···


Sam Roberts sroberts@certicom.com

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:

#!/usr/bin/env ruby

def test
puts “This is a test”
end

test

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 :slight_smile:

PD: OK, I know, I’m more worried about conceptual point of view that
I’d must
be :wink:

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

···

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

Hi all,

well, what can I say? Thank you all for your nice and quick answers :wink:

See ya!

···


(o_.’ Imobach González Sosa imobachgs@softhome.net
//\c{} imobachgs@step.es a2419@dis.ulpgc.es
V__)_ imodev@softhome.net osoh en jabber.at y jabber.org
Usuario Linux #201634
Debian GNU/Linux `Sarge’ con núcleo 2.4.24 sobre Intel Pentium 4

Inmortales, mortales, inmortales. Nuestra vida es la muerte de los primeros y
su vida es nuestra muerte.
– Heráclito de Efeso. (535-475 A.C.) Filósofo griego.