Error in Ruby quickstart tutorial

On the quickstart tutorial on the Ruby homepage,
http://www.ruby-lang.org/en/quickstart/3/ , 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

oinkoink wrote:

On the quickstart tutorial on the Ruby homepage,
http://www.ruby-lang.org/en/quickstart/3/ , 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

Marcin Mielzynski wrote:

oinkoink wrote:
> On the quickstart tutorial on the Ruby homepage,
> http://www.ruby-lang.org/en/quickstart/3/ , 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.

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/seeingMetaclassesClearly.html

lopex

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

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.

···

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

Hi --

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

It does if the class being changed is a singleton class :slight_smile: But
anyway...the point is that the tutorial is misleading, because, as
oinkoink points out, it suggests that existing objects and future
objects relate differently to the class (see the "but" in the middle
of the sentence, in particular; whatever terms are being used
["changed", "affected", etc.], there's clearly a contrast being
suggested).

David

···

On Thu, 5 Oct 2006, Marcin MielżyÅ~Dski wrote:

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org