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

La buena y verdadera amistad no debe ser sospechosa en nada.
– Miguel de Cervantes Saavedra. (1547-1616) Escritor español.

Imobach González 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.

That’s right

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?

test is added as a private method of the class Object.

Thank you.

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

HTH

Best Regards

Mark Sparshatt

Some languages draw hard distinctions between “methods”, “functions”,
“subroutines”, etc., but Ruby’s much more laid-back than that. You’ll
notice a similar sort of looseness with @ and @@ variables, which can
act differently if they’re declared at a global scope than if they’re
declared within a class or method definition.

(Now, if you get seriously into Ruby’s more functional aspects you’ll
have to learn the differences between procs and lambdas, but you can
still do a lot without learning that stuff.)

Francis

“Imobach González Sosa” imodev@softhome.net wrote in message news:200402141220.14464.imobachgs@softhome.net

···

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

La buena y verdadera amistad no debe ser sospechosa en nada.
– Miguel de Cervantes Saavedra. (1547-1616) Escritor español.

Hi –

Some languages draw hard distinctions between “methods”, “functions”,
“subroutines”, etc., but Ruby’s much more laid-back than that. You’ll
notice a similar sort of looseness with @ and @@ variables, which can
act differently if they’re declared at a global scope than if they’re
declared within a class or method definition.

I think the difference is mainly cosmetic, though. At the top level
the default ‘self’ is an instance of Object, so if you assign to a
top-level class variable, it belongs to Object. Similarly, if you
assign to an instance variable at the top level, it belongs to the
default object. There’s a kind of bootstrapping of the environment
going on, but I think it’s consistent with the behavior of those
variables all around (if less visible).

David

···

On Mon, 16 Feb 2004, Francis Hwang wrote:


David A. Black
dblack@wobblini.net

Francis Hwang wrote:

Some languages draw hard distinctions between “methods”, “functions”,
“subroutines”, etc., but Ruby’s much more laid-back than that. You’ll
notice a similar sort of looseness with @ and @@ variables, which can
act differently if they’re declared at a global scope than if they’re
declared within a class or method definition.

(Now, if you get seriously into Ruby’s more functional aspects you’ll
have to learn the differences between procs and lambdas, but you can
still do a lot without learning that stuff.)

What is the technical definition right now? And which part is
implemented and which parts are future changes?

As far as I understood it, lambda’s introduced a complete new scope so
you could shadow variables above, whereas procs produced a more loose
scope. But perhaps I misunderstood. Anyone have a good definition/example?

Charles Comstock