Methods outside classes

  • Thread starter Imobach González Sosa
  • Start date
I

Imobach González Sosa

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:

<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?

Thank you.

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

--
(o_.' Imobach González Sosa (e-mail address removed)
//\c{} (e-mail address removed) (e-mail address removed)
V__)_ (e-mail address removed) 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.
 
M

Mark

Imobach said:
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:

<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?
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 ;-)
HTH

Best Regards

Mark Sparshatt
 
F

Francis Hwang

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
 
C

Charles Comstock

Francis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top