[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

instance_eval and class_eval - names got mixed up?

Bharat Ruparel

3/7/2008 8:07:00 PM

Going through the Ruby Programming Language text Chapter 8 Reflection
and MetaProgramming. Section 8.2.2 on page 270.

The following paragraph has me puzzled:

"Note the subtle but crucial difference between instance_eval and
class_eval when the code being evaluated contains a method definition.
instance_eval defines singleton methods of the object (and this results
in class methods when it is called on a class object). class_eval
defines regular instance methods."

Is this not supposed to be the other way around? instance_eval should
define instance methods and class_eval should define class methods. Is
there a catch here? Ruby is supposed to be following principle of least
surprise. I must say I am surprised here, or there is something quite
profound that I don't get.

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

8 Answers

Todd Benson

3/7/2008 8:20:00 PM

0

On Fri, Mar 7, 2008 at 2:06 PM, Bharat Ruparel <bruparel@mercury.com> wrote:
> Going through the Ruby Programming Language text Chapter 8 Reflection
> and MetaProgramming. Section 8.2.2 on page 270.
>
> The following paragraph has me puzzled:
>
> "Note the subtle but crucial difference between instance_eval and
> class_eval when the code being evaluated contains a method definition.
> instance_eval defines singleton methods of the object (and this results
> in class methods when it is called on a class object). class_eval
> defines regular instance methods."
>
> Is this not supposed to be the other way around? instance_eval should
> define instance methods and class_eval should define class methods. Is
> there a catch here? Ruby is supposed to be following principle of least
> surprise. I must say I am surprised here, or there is something quite
> profound that I don't get.
>
> Bharat

It's the use of the word "instance" here that causes the problem.
Singleton methods are specific to a single instance of an object. An
instance method refers to a method that's available to _every_
instance of a class upon their creation. Maybe it should be
singleton_eval and instance_eval, but, see, that doesn't really make
sense, because that would imply eval being used within the singleton
class of the object. One of those meta-programming language
work-arounds I guess.

Todd

Stefan Lang

3/7/2008 8:28:00 PM

0

2008/3/7, Bharat Ruparel <bruparel@mercury.com>:
> Going through the Ruby Programming Language text Chapter 8 Reflection
> and MetaProgramming. Section 8.2.2 on page 270.
>
> The following paragraph has me puzzled:
>
> "Note the subtle but crucial difference between instance_eval and
> class_eval when the code being evaluated contains a method definition.
> instance_eval defines singleton methods of the object (and this results
> in class methods when it is called on a class object). class_eval
> defines regular instance methods."
>
> Is this not supposed to be the other way around? instance_eval should
> define instance methods and class_eval should define class methods. Is
> there a catch here? Ruby is supposed to be following principle of least
> surprise. I must say I am surprised here, or there is something quite
> profound that I don't get.

No, the book is correct. The "instance" in instance_eval indicates that you are
evaluating code in the context of one specific instance - if this
instance is a class
object, method definitions happen to define class methods.

instance_eval is defined in Object, so you can use it on any object - nothing
special about classes here, except that we call singleton methods on
classes "class methods".

class_eval evaluates code as if it appeared between "class ... end".
And a "def" between "class ... end" defines instance methods of the
class.

Stefan

Bharat Ruparel

3/7/2008 9:09:00 PM

0

Thanks gentlemen for your responses. It still requires mental gyrations
for me. I honestly think that this one got away from Matz.
--
Posted via http://www.ruby-....

Drew Olson

3/7/2008 9:21:00 PM

0

Bharat Ruparel wrote:
> Thanks gentlemen for your responses. It still requires mental gyrations
> for me. I honestly think that this one got away from Matz.

The confusion here is that you're calling instance eval on a specific
class. In ruby, classes are instances of the Class class (no, that is
not a type). Methods added to your class (the instance of the class
Class) are, in effect, added the the class definition. Below is an
example of using class_eval and instance_eval against a simple class, so
you might find this less confusing.

>> class Foo
>> def hi
>> puts "hi"
>> end
>> end
=> nil
>> foo = Foo.new
=> #<Foo:0x3aa250>
>> foo.instance_eval do
?> hi
>> end
hi
=> nil
>> Foo.class_eval do
?> def howdy
>> puts "howdy"
>> end
>> end
=> nil
>> foo.howdy
howdy
=> nil
--
Posted via http://www.ruby-....

7stud --

3/8/2008 12:19:00 AM

0

Bharat Ruparel wrote:
> Going through the Ruby Programming Language text Chapter 8 Reflection
> and MetaProgramming. Section 8.2.2 on page 270.
>
> The following paragraph has me puzzled:
>
> "Note the subtle but crucial difference between instance_eval and
> class_eval when the code being evaluated contains a method definition.
> instance_eval defines singleton methods of the object (and this results
> in class methods when it is called on a class object). class_eval
> defines regular instance methods."
>
> Is this not supposed to be the other way around? instance_eval should
> define instance methods and class_eval should define class methods. Is
> there a catch here?

The catch is that a class is an instance. For a moment, forget that
class_eval even exists. Without that distraction, instance_eval seems
to operate consistently: 1) it creates singleton methods for 'normal
'objects, e.g. mydog which is an instance of a Dog class, and 2) it
creates singleton methods for objects which are instances of the class
Class, i.e. all classes. It just so happens that a singleton method for
a class object is known as 'class method'.
--
Posted via http://www.ruby-....

Arlen Cuss

3/8/2008 12:45:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Sat, Mar 8, 2008 at 8:09 AM, Bharat Ruparel <bruparel@mercury.com> wrote:

> Thanks gentlemen for your responses. It still requires mental gyrations
> for me. I honestly think that this one got away from Matz.


Perhaps just try to remember that only classes have class_eval, and only
instances (i.e. everything) has instance_eval. Hence, since all objects
could have singleton methods, instance_eval must add methods to the
singleton class, whereas what you put in a `class_eval' block is similar to
when you use the `class' keyword itself.

Arlen

Sebastian Hungerecker

3/8/2008 1:46:00 PM

0

Bharat Ruparel wrote:
> I honestly think that this one got away from Matz.

So you'd want the names of the methods class_eval and instance_eval
to be switched? So that doing "lala".class_eval would be allowed,
but "lala".instance_eval would not? I'd find that a bit counter-intuitive
to be honest.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

somedeepblue

6/5/2014 2:57:00 PM

0

Am Freitag, 30. Mai 2014 01:08:35 UTC+2 schrieb cor...@ultimainfo.net:
> On Thursday, May 29, 2014 1:11:50 AM UTC+12, somede...@gmail.com wrote:
>
> > Hi everybody,
>
> >
>
> >
>
> >
>
> > as the gameplay FAQ states there is no normal way to solve this quest. But it also explains that "normal" means "i.e. without cheating".
>
> >
>
> >
>
> >
>
> > What I do not find any trace of is: how can I solve the side quest WITH cheating?
>
> >
>
> >
>
> >
>
> > Does anyone know what flags to set where or what to do else to being able to solve the quest?
>
>
>
> To solve the quest within the game is sadly impossible. The conversation scripts just don't exist AFAIK. The best you can do in U6 is use the flag settings to find out the name of the anonymous fighter who lives between Skara Brae and Britain. Given his name (Michael) you can find out a bit more about him from the people of Skara Brae. You still won't get any actual proof about the murder or be able to resolve anything with the NPCs.
>
>
>
> Unfortunately the actual resolution is in U7, the next game. Talking to the folks in Skara Brae in that game, you learn that it was Michael who murdered Quenton, and he was eventually convicted and executed for the crime.
>
>
>
>
>
> Paulon Dragon.

Hi Paulon,

thanks for taking the time to answer.

I was asking for the "cheat" to solve the quest as this probably would have enabled me to implement some flag to set while translating the Conversations of UVI to German and hereby create a "patch" for this quest. Perhaps something like
this in Quentons convo:

if (obj_in_party(58,72) > 0) { //checks for Seance spell
JUMP L2; //goto new-to-create-normal convo block
} else {
JUMP L1; //goto finger pointing convo block
}

But it looks like this is simply impossbile, too bad :-)

Something else struck me though, when you said, the convo scripts simply don't exist....

Do I understand it correctly, that all the finger pointing Quenton does, is conversation you have with him, WITHOUT Seance? AKA. "normal" chit chat, and that he would have been able to answer "understandably" (verbally) within a Seance?

Background is the Remake of UVI: Ultima 6 Project does feature a Seance with Quenton making the quest solvable...but the way they did is, that Quenton does not react at all when NOT in a Seance, and he answers with the original "finger pointing" routines IN a Seance.

Hope, that wasn't too confusing ;-)