[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

About class variable question?

Zhao Yi

9/2/2008 12:14:00 PM

Please see this code below. It will get "can't convert nil to string
error". Why can't the @name be assigned? I have set the @name = "world".

class Hello
@name="world"
def say
puts "Hello "+@name
end
end
h=Hello.new
h.say
--
Posted via http://www.ruby-....

6 Answers

Stefano Crocco

9/2/2008 12:18:00 PM

0

On Tuesday 02 September 2008, Zhao Yi wrote:
> Please see this code below. It will get "can't convert nil to string
> error". Why can't the @name be assigned? I have set the @name = "world".
>
> class Hello
> @name="world"
> def say
> puts "Hello "+@name
> end
> end
> h=Hello.new
> h.say

@name is an instance variable of the Hello object, not of instances of Hello,
such as h. To create an instance variable of instances of Hello, you need to
assign it a value in an instance method. Usually, this is done in the
initialize method:

class Hello

def initialize
@name = "Hello"
end

def say
puts "Hello" + @name
end

end

h = Hello.new
h.say

I hope this helps

Stefano

Zhao Yi

9/2/2008 12:47:00 PM

0

Stefano Crocco wrote:
> On Tuesday 02 September 2008, Zhao Yi wrote:
>> h.say
> @name is an instance variable of the Hello object, not of instances of
> Hello,
> such as h. To create an instance variable of instances of Hello, you
> need to
> assign it a value in an instance method. Usually, this is done in the
> initialize method:
>
> class Hello
>
> def initialize
> @name = "Hello"
> end
>
> def say
> puts "Hello" + @name
> end
>
> end
>
> h = Hello.new
> h.say
>
> I hope this helps
>
> Stefano

If the name is Hello object instance, it should be accessed by Hello
object, right? see this code:

class Hello
@name="world"

def name
@name
end
end
h=Hello.new
puts h.name

It will print "nil" which means the first line of this class
@name="world" is ignored.
Am I right?
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

9/2/2008 12:59:00 PM

0

On Tue, Sep 2, 2008 at 2:46 PM, Zhao Yi <youhaodeyi@gmail.com> wrote:
> Stefano Crocco wrote:
>> On Tuesday 02 September 2008, Zhao Yi wrote:
>>> h.say
>> @name is an instance variable of the Hello object, not of instances of
>> Hello,
>> such as h. To create an instance variable of instances of Hello, you
>> need to
>> assign it a value in an instance method. Usually, this is done in the
>> initialize method:
>>
>> class Hello
>>
>> def initialize
>> @name = "Hello"
>> end
>>
>> def say
>> puts "Hello" + @name
>> end
>>
>> end
>>
>> h = Hello.new
>> h.say
>>
>> I hope this helps
>>
>> Stefano
>
> If the name is Hello object instance, it should be accessed by Hello
> object, right? see this code:
>
> class Hello
> @name="world"
>
> def name
> @name
> end
> end
> h=Hello.new
> puts h.name
>
> It will print "nil" which means the first line of this class
> @name="world" is ignored.
> Am I right?

It's not ignored. When you see @name="something", you have to think
which object is self at that point. That object will have an instance variable
called @name with that value. The thing is that classes are also
objects (instances
of class Class), and can have instance variables as any other object. This:

class Hello
@name = "hello's name"
end

creates an instance variable of the object Hello. To achieve what you
want you need to do as Stefano showed: create the @name instance variable
in a place where self is the instance of the Hello class: inside a
method, like initialize.

Jesus.

Lloyd Linklater

9/2/2008 1:24:00 PM

0

Zhao Yi wrote:
> Please see this code below. It will get "can't convert nil to string
> error". Why can't the @name be assigned? I have set the @name = "world".
>
> class Hello
> @name="world"
> def say
> puts "Hello "+@name
> end
> end
> h=Hello.new
> h.say

Try it this way:

class Hello
@@name="world"
def say
puts "Hello "+@@name
end
end
h=Hello.new

h.say
--
Posted via http://www.ruby-....

Zhao Yi

9/3/2008 12:38:00 AM

0


This is really different with Java at this point.
--
Posted via http://www.ruby-....

David A. Black

9/3/2008 6:32:00 AM

0

HI --

On Wed, 3 Sep 2008, Zhao Yi wrote:

> This is really different with Java at this point.

Yes -- it's a totally different language. Enjoy it :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!