I have been doing different things w/ Ruby for a couple of years now and
the only bad thing I can say about it is that it makes programming in other
languages feel awfully burdensome. = )
Anyhow, I really like how everything makes sense. There is one thing that
I have not been able to fit in entirely. A Ruby program starts in the
context of the "main" object,
However, the methods I declare in the context of this "main" object are
somehow available as private methods of class Object (and naturally usable
from subclasses):
def foo
puts "hello"
end
class Bar
def amethod
foo
end
end
b = Bar.new
b.amethod
outputs:
hello
Is there some logical explanation for this? I understand that this is very
convenient as these methods appear as "stand-alone functions", but taking
it just as "it just is so" breaks the otherwise consistent rules, as
normally you have to define a method in the context of a class for it to be
a method of that class. However, the "main" object is not class Object, nor
is it a class at all:
So what is the logic behind the "main" object? Is there a logical reason
for the methods defined in its context to appear as private methods of
class Object?
"Antti Karanta" <Antti.Karanta@removethis.iki.fi.nospam> wrote in message
news:dpbr1c$p2b$1@phys-news4.kolumbus.fi...
···
Hi!
I have been doing different things w/ Ruby for a couple of years now and
the only bad thing I can say about it is that it makes programming in
other
languages feel awfully burdensome. = )
Anyhow, I really like how everything makes sense. There is one thing that
I have not been able to fit in entirely. A Ruby program starts in the
context of the "main" object,
However, the methods I declare in the context of this "main" object are
somehow available as private methods of class Object (and naturally usable
from subclasses):
def foo
puts "hello"
end
class Bar
def amethod
foo
end
end
b = Bar.new
b.amethod
outputs:
hello
Is there some logical explanation for this? I understand that this is
very
convenient as these methods appear as "stand-alone functions", but taking
it just as "it just is so" breaks the otherwise consistent rules, as
normally you have to define a method in the context of a class for it to
be
a method of that class. However, the "main" object is not class Object,
nor
is it a class at all:
So what is the logic behind the "main" object? Is there a logical reason
for the methods defined in its context to appear as private methods of
class Object?
Is there some logical explanation for this? I understand that this is very
convenient as these methods appear as "stand-alone functions", but taking
it just as "it just is so" breaks the otherwise consistent rules, as
normally you have to define a method in the context of a class for it to be
a method of that class. However, the "main" object is not class Object, nor
is it a class at all:
Note that you are not doing def self.method() end. It's just that def method() end at the top level adds the methods to the Kernel module (which is included in Object meaning its methods are available everywhere) instead of raising an exception or adding them to the top level object itself.
It's also interesting to see which singleton methods the top level object has:
to_s is so you see "main" when doing p self etc. at the top level.
The other three ones are top level versions of the respective instance method of Module.
So what is the logic behind the "main" object? Is there a logical reason
for the methods defined in its context to appear as private methods of
class Object?
To quote Guy Decoux:
no, not really. ruby has self and *internally* ruby_class which give it
where the method must be defined.
For example :
* at top level it has : self = main, ruby_class = Object
when you define a method at top level this will be an Object method
* in the class A, it has : self = A, ruby_class = A
when you define a method in A, this will be an instance method for A
Now, I have a question for Guy:
Flagellate = Class.new {
def banana_boat; puts 'hurrah!' end
}
makes banana_boat a method on Flagellate, which makes me think that methods go where self is.
However:
class FranklinRoosevelt
def smooth_operator; def banana_boat; puts 'javohl!' end end
end
FranklinRoosevelt.new.smooth_operator
makes banana_boat a method on FranklinRoosevelt, which is either ruby_class or self.class, but not self.
What's goin' on? Is there another internal variable that determines where methods go, or am I confusing things? Are Class.new and class_eval just the exception to the rule that method definitions go on ruby_class? If so, how?
Flagellate = Class.new {
def banana_boat; puts 'hurrah!' end
}
With this you have this case
* in the class A, it has : self = A, ruby_class = A
This mean that you have self = ruby_class = Flagellate
However:
class FranklinRoosevelt
def smooth_operator; def banana_boat; puts 'javohl!' end end
end
FranklinRoosevelt.new.smooth_operator
makes banana_boat a method on FranklinRoosevelt, which is either
ruby_class or self.class, but not self.
When the method #smoot_operator is called
* self is an instance of FranklinRoosevelt
* ruby_class is FranklinRoosevelt
#banana_boot become a method on FranklinRoosevelt
What's goin' on? Is there another internal variable that determines
where methods go, or am I confusing things?
You are confusing : the problem is that you can't access the variable
ruby_class, it's completely hidden but ruby always use this variable to
know where it must store a method.
Are Class.new and class_eval just the exception to the rule that method
definitions go on ruby_class? If so, how?
No, they are not exceptions. This is just that when you write
Flagellate = Class.new {
def banana_boat; puts 'hurrah!' end
}
many persons will except that ruby will do the same that if you write
class Flagellate
def banana_boat; puts 'hurrah!' end
end
This is why it define self = ruby_class = Flagellate