Stupid question: Object#name

From: Mark Hubbart
Subject: Re: stupid question: Object#name

This is a fairly common OO topic, and there is a pretty good
description of classes and objects here:

http://www.visibleworkings.com/little-ruby/Chapter3.pdf

One part of this chapter (Page 5) is driving me crazy. I tried it in irb and it doesn't work as described in the chapter.

From the book (paraphrased(?)......sort of)

···

####################################################
class Celery
end

class IceCream
  def IceCream.new(starting_licks)
    Celery.new
  end
end

class IceCream
  def IceCream.small
    new(80)
  end
end

....I can send messages to what
class answers, like this:

food = IceCream.small
more_food = food.class.small

Both food and more_food would be
instances of Celery.
#######################################################

I understand how food is an instance of Celery.
But how is more_food an instance of Celery?

gavri

Hi --

···

On Wed, 11 Aug 2004, Gavri Savio Fernandez wrote:

> From: Mark Hubbart
> Subject: Re: stupid question: Object#name

> This is a fairly common OO topic, and there is a pretty good
> description of classes and objects here:
>
> http://www.visibleworkings.com/little-ruby/Chapter3.pdf

One part of this chapter (Page 5) is driving me crazy. I tried it in irb and it doesn't work as described in the chapter.

>From the book (paraphrased(?)......sort of)

####################################################
class Celery
end

class IceCream
  def IceCream.new(starting_licks)
    Celery.new
  end
end

class IceCream
  def IceCream.small
    new(80)
  end
end

....I can send messages to what
class answers, like this:

food = IceCream.small
more_food = food.class.small

Both food and more_food would be
instances of Celery.
#######################################################

I understand how food is an instance of Celery.
But how is more_food an instance of Celery?

It isn't. Maybe Brian meant that second line to be:

  more_food = food.class.new

but I'm not sure.

David

--
David A. Black
dblack@wobblini.net

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0408101652300.22756-100000@wobblini...

Hi --

> > From: Mark Hubbart
> > Subject: Re: stupid question: Object#name
>
> > This is a fairly common OO topic, and there is a pretty good
> > description of classes and objects here:
> >
> > http://www.visibleworkings.com/little-ruby/Chapter3.pdf
>
> One part of this chapter (Page 5) is driving me crazy. I tried it in

irb and it doesn't work as described in the chapter.

>
> >From the book (paraphrased(?)......sort of)
>
> ####################################################
> class Celery
> end
>
> class IceCream
> def IceCream.new(starting_licks)
> Celery.new
> end
> end
>
> class IceCream
> def IceCream.small
> new(80)
> end
> end
>
> ....I can send messages to what
> class answers, like this:
>
> food = IceCream.small
> more_food = food.class.small
>
> Both food and more_food would be
> instances of Celery.
> #######################################################
>
> I understand how food is an instance of Celery.
> But how is more_food an instance of Celery?

It isn't. Maybe Brian meant that second line to be:

  more_food = food.class.new

but I'm not sure.

Illustration:

food = IceCream.small

=> #<Celery:0x1016f848>

food.class

=> Celery

more_food = food.class.small

NoMethodError: undefined method `small' for Celery:Class
        from (irb):17

more_food = food.class.new

=> #<Celery:0x100c63b8>

Regards

    robert

···

On Wed, 11 Aug 2004, Gavri Savio Fernandez wrote: