# Error in Ruby quickstart tutorial

**URL:** <https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048>\
**Category:** Ruby\
**Created:** [October 4, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048 "2006-10-04T22:01:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oinkoink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://www.ruby-forum.com/u/oinkoink)\
**Post date:** [October 4, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048/1 "2006-10-04T22:01:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oinkoink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://www.ruby-forum.com/u/oinkoink)\
**Post date:** [October 4, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048/2 "2006-10-04T22:55:21Z")

</div>

oinkoink wrote:

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

---

<div class="post-metadata">

**Author:** ![oinkoink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://www.ruby-forum.com/u/oinkoink)\
**Post date:** [October 4, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048/3 "2006-10-04T23:31:24Z")

</div>

Marcin Mielzynski wrote:

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

---

<div class="post-metadata">

**Author:** ![oinkoink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://www.ruby-forum.com/u/oinkoink)\
**Post date:** [October 5, 2006, 12:51am UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048/4 "2006-10-05T00:51:27Z")

</div>

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](http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html)

lopex

---

<div class="post-metadata">

**Author:** ![oinkoink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://www.ruby-forum.com/u/oinkoink)\
**Post date:** [October 5, 2006, 1:17am UTC](https://www.ruby-forum.com/t/error-in-ruby-quickstart-tutorial/71048/5 "2006-10-05T01:17:27Z")

</div>

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.
