[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Error in Ruby quickstart tutorial

oinkoink

10/4/2006 7:59:00 PM

On the quickstart tutorial on the Ruby homepage,
http://www.ruby-lang.org/en/qui... , it says,
"In Ruby, you can open a class up again and modify it. That doesn't
change any objects that already exist, but it does affect any new
objects you create." This is incorrect. For example:

irb(main):001:0> class FooClass; end
nil
irb(main):002:0> sally = FooClass.new
#<FooClass:0xb7e0b214>
irb(main):003:0> class FooClass; def foo; puts "bar"; end; end
nil
irb(main):004:0> sally.foo
bar
nil

Changing a class changes the objects of that class which already exist
(which is pretty cool).

Regards, Bret
Bret Jolly

6 Answers

Marcin Mielzynski

10/4/2006 8:53:00 PM

0

oinkoink wrote:
> On the quickstart tutorial on the Ruby homepage,
> http://www.ruby-lang.org/en/qui... , it says,
> "In Ruby, you can open a class up again and modify it. That doesn't
> change any objects that already exist, but it does affect any new
> objects you create." This is incorrect. For example:

Nope, this is correct.

This in fact changes the object behaviour, but the objects themselves
are not changed. Methods belongs to classes, not objects.

lopex

oinkoink

10/4/2006 9:27:00 PM

0

Marcin Mielzynski wrote:
> oinkoink wrote:
> > On the quickstart tutorial on the Ruby homepage,
> > http://www.ruby-lang.org/en/qui... , it says,
> > "In Ruby, you can open a class up again and modify it. That doesn't
> > change any objects that already exist, but it does affect any new
> > objects you create." This is incorrect. For example:

> Nope, this is correct.
> This in fact changes the object behaviour, but the objects themselves
> are not changed. Methods belongs to classes, not objects.
> lopex

The problem is that the tutorial suggests that objects which have
already been created before a class is modified are affected
differently from objects which are created after the class is modified.

Marcin Mielzynski

10/4/2006 10:47:00 PM

0

oinkoink wrote:

> The problem is that the tutorial suggests that objects which have
> already been created before a class is modified are affected
> differently from objects which are created after the class is modified.
>

Ok, let's put it another way. Modifying a class doesn't change two thins
- instance variables of existing objects
- anything that is stored in singleton/eigen/meta/whatever class

You can define a method that belongs only to certain object and is not
stored in the object's class:

class Foo

def bar
p "bar"
end

end

f = Foo.new

# defining a method on an object (it is stored in a special class that
belongs _only_ to f)

def f.bar
p "my_bar"
end

# redefining an instance method, class is changed
class Foo

def bar
p "new_bar"
end

end


# not affected:

f.bar

-> "my_bar"

but new instances will have the new Foo#bar method

recommended reading:
http://www.whytheluckystiff.net/articles/seeingMetaclassesCl...

lopex

Eero Saynatkari

10/4/2006 11:16:00 PM

0

On 2006.10.05 07:50, Marcin Miel??y??ski wrote:
> oinkoink wrote:
>
> >The problem is that the tutorial suggests that objects which have
> >already been created before a class is modified are affected
> >differently from objects which are created after the class is modified.
> >
>
> Ok, let's put it another way. Modifying a class doesn't change two thins
> - instance variables of existing objects
> - anything that is stored in singleton/eigen/meta/whatever class

What you are saying is correct, the problem is just that the tutorial
is *definitely* saying the same thing you are :)

Any existing objects are affected by changes to the class.

This should be corrected or, if they actually *did* intend to say what
you have said here, 'clarified' with a heavy hand.

dblack

10/4/2006 11:50:00 PM

0

hkhellhkhell

10/5/2006 1:16:00 AM

0

dblack@wobblini.net ??:>??IT????? -> Dan?????IT????Advanced Telesoft,???????Dan, ???cool, ????,?????????, ??????.??, ??????, ?????????????,????????.??, ????????????,????????????, ???asp.net,???????asp.net???????,????????????, ???????????, ????????,?????????????, ????????, ??????????,??????, ?????????????,??????, ???????????, ??????, ????, ??????????,??????, ??????????????,????. ??????forward??, ??