Error in Ruby quickstart tutorial

O

oinkoink

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
 
M

Marcin Mielżyński

oinkoink said:
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
 
O

oinkoink

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.
 
M

Marcin Mielżyński

oinkoink said:
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
 
E

Eero Saynatkari

--9Ek0hoCL9XbhcSqy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

oinkoink wrote:
=20
=20
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.

--9Ek0hoCL9XbhcSqy
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFJEAc7Nh7RM4TrhIRAgPWAJ9QL+v/TUsH7GbDsgxmh95TrXgvlwCeKN/F
f4z9PGfikw40cRgM8LQLejE=
=xmPq
-----END PGP SIGNATURE-----

--9Ek0hoCL9XbhcSqy--
 
D

dblack

---2049402039-1681331018-1160005768=:15444
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1681331018-1160005768=:15444"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1681331018-1160005768=:15444
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

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 :) 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

--=20
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
---2049402039-1681331018-1160005768=:15444--
---2049402039-1681331018-1160005768=:15444--
 
H

hkhellhkhell

(e-mail address removed) 寫é“:

香港IT界超级溅人 -> Dan

在香港一间ITå…¬å¸åå«Advanced Telesoft,
有个程åºå‘˜å«é˜¿Dan, 平时扮cool, 背後æ’ä½ ,
在è€æ¿é¢å‰æ•°ä½ ä¸æ˜¯, è·µè¸ä½ çš„技术.


第一, 作为一个男人, 根本无å¯èƒ½åšå‡ºæ•°è‡­äººå®¶çš„事,
这个是å°å¥³äººæ‰€ä¸º.
第二, 为何你åªæ•°è‡­æ–°å…¥æ¥çš„员工,
ä¸æ˜¯æ€•å¾—罪高层是甚麽
第å, ä½ åªæ‡‚asp.net,
ä½ ä¸è¯´ç»™æˆ‘知åšasp.net会å˜æˆç”µè„‘高手,
所以你根本无资格笑人
第四, 人家入æ¥å·¥ä½œåªæœ‰ä¸€ä¸ªæœˆ, 平日和你都无两å¥,
ä½ åˆçŸ¥äººå®¶æŠ€æœ¯å¥½ä¸å¥½?
第五, 人家是内地程åºå‘˜, 你就开å£è‹±æ–‡é—­å£è‹±æ–‡,
家家å¬ä¸æ˜Žç™½, 你就在è€æ¿é¢å‰è¯´ä»–是个废人,
你身为中国人, 少许中国编程术语都ä¸æ‡‚, 还讲人?
第六, 支æŒå°ç‹¬, è¯å°æ¹¾ç”µè„‘书å°åˆ·ç²¾ç¾Ž,
内地书是垃圾, 最过份的就是说希望陈水æ‰ç‹¬ç«‹,
å攻内地.


请内地程åºå‘˜forward此文, 多谢
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top