[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Self and Current Class

Pedro Silva

9/2/2008 8:38:00 AM

Thomas, thanks for your reply.
>
> So I guess the general rule is: inside module declaration, both self and
> current class are set to the module we're declaring. Outside module
> declarations, current class is set to the eigenclass of self.
>
> I hope this is not a complete mess and rubbish :)

Consider this case:

class SomeClass
# self is SomeClass, current class is SomeClass
puts "Class Level - #{self}"

def some_method
# self is an instance, current class is SomeClass
puts "Some Method - #{self}"

def self.some_inside_class_method
# self is an instance, current class is instance's singleton
class
puts "Inside class method - #{self}"
end

def some_inside_instance_method
# self is an instance, current class is SomeClass
puts "Inside instance method - #{self}"
end

# self is an instance, current class is instance's singleton class
instance_eval { puts "Instance instance_eval - #{self}" }
# no class_eval for instances
end

def self.class_method
# self is SomeClass, current class is SomeClass's singleton class
puts "Class method - #{self}"
end

# self is SomeClass, current class is SomeClass's singleton class
instance_eval { puts "Class instance_eval - #{self}" }

# self is SomeClass, current class is SomeClass
class_eval { puts "Class class_eval - #{self}" }
end

Are all the comments correct about self and current class value and are
all cases covered (current class modification)?
Is def the only method that uses the current class value?
I used Class but Module has the same behaviour.

Pedro.
--
Posted via http://www.ruby-....

4 Answers

David A. Black

9/2/2008 9:14:00 AM

0

Hi --

On Tue, 2 Sep 2008, Pedro Silva wrote:

> Thomas, thanks for your reply.
>>
>> So I guess the general rule is: inside module declaration, both self and
>> current class are set to the module we're declaring. Outside module
>> declarations, current class is set to the eigenclass of self.
>>
>> I hope this is not a complete mess and rubbish :)
>
> Consider this case:
>
> class SomeClass
> # self is SomeClass, current class is SomeClass
> puts "Class Level - #{self}"
>
> def some_method
> # self is an instance, current class is SomeClass
> puts "Some Method - #{self}"
>
> def self.some_inside_class_method
> # self is an instance, current class is instance's singleton
> class
> puts "Inside class method - #{self}"

That's not a class method. self is an instance of SomeClass; you're
defining a singleton method on that instance. Every time you do:

def obj.something

you're defining a singleton method "something" on obj. self is just an
alias for a particular object at a particular point in runtime.

> end
>
> def some_inside_instance_method
> # self is an instance, current class is SomeClass
> puts "Inside instance method - #{self}"
> end

I'm not sure what you mean by "inside" in the method name. It's just
an instance method.

> # self is an instance, current class is instance's singleton class
> instance_eval { puts "Instance instance_eval - #{self}" }
> # no class_eval for instances
> end
>
> def self.class_method
> # self is SomeClass, current class is SomeClass's singleton class
> puts "Class method - #{self}"
> end
>
> # self is SomeClass, current class is SomeClass's singleton class
> instance_eval { puts "Class instance_eval - #{self}" }
>
> # self is SomeClass, current class is SomeClass
> class_eval { puts "Class class_eval - #{self}" }
> end
>
> Are all the comments correct about self and current class value and are
> all cases covered (current class modification)?
> Is def the only method that uses the current class value?

It's a keyword, rather than a method, but I think it is the only thing
that cares about the notion of "current class", at least as I
understand that notion. (I admit it's not one that I've paid much
attention to, by that name, though of course I like to know what 'def'
will do at any given time.)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

Pedro Silva

9/2/2008 9:39:00 AM

0

Hi David,

>> def self.some_inside_class_method
>> puts "Inside class method - #{self}"
>> end
> That's not a class method. self is an instance of SomeClass; you're
> defining a singleton method on that instance.

If forgot to change the text, I mean't singleton method. Thanks for
pointing that out.

>> def some_inside_instance_method
>> # self is an instance, current class is SomeClass
>> puts "Inside instance method - #{self}"
>> end
>
> I'm not sure what you mean by "inside" in the method name. It's just
> an instance method.

This case was to test the def keyword inside an instance method, because
at class level self is the class, inside instance method self is the
instance.

> It's a keyword, rather than a method, but I think it is the only thing
> that cares about the notion of "current class", at least as I
> understand that notion. (I admit it's not one that I've paid much
> attention to, by that name, though of course I like to know what 'def'
> will do at any given time.)

I really wanted to know if current class is used anywhere else by any
other keyword/method to try to fully understand these two concepts.

Pedro.


--
Posted via http://www.ruby-....

TPReal

9/2/2008 9:42:00 AM

0

Hi there.

For me, your diagram is perfectly correct (however I had some doubts at
the beginning again).

Well, I wouldn't say that now all cases are covered, even though it
looks like they are. But the diagram is not good for framing and putting
on the wall, it is only good for learning from it. Because there are
special cases like:

class<<nil;self;end #=> NilClass
class<<true;self;end #=> TrueClass

This kind of primitives appears not to have eigenclass at all, but there
is only one instance of each of the NilClass, FalseClass and TrueClass,
so it's not a problem. Still methods can be added to these primitives.

class<<5;end # raises TypeError: no virtual class for Fixnum

This primitive doesn't have eigenclass at all, you cannot add methods to
one particular instance of Fixnum. You cannot even _be_ inside this
nonexistent singleton class.

Also, have a look at this case:

eval("def q;end",nil.instance_eval{binding})

It raises an error. There's a place (inside the eval), where you _can_
be, but cannot define a method. So I guess juggling with bindings can
produce some more cases.

> Is def the only method that uses the current class value?

I suppose yes (probably alias also). But in fact it's not a method, not
even a private one. It must be something else, that cannot be hacked
easily (oh no, are there such things in Ruby?).

I must say I definitely learnt something from your question.

TPR

--
http://al2o3-cr.blo...
--
Posted via http://www.ruby-....

David A. Black

9/2/2008 10:41:00 AM

0

Hi --

On Tue, 2 Sep 2008, Thomas B. wrote:

>> Is def the only method that uses the current class value?
>
> I suppose yes (probably alias also). But in fact it's not a method, not
> even a private one. It must be something else, that cannot be hacked
> easily (oh no, are there such things in Ruby?).

It's a keyword.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!