[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why don't these writable atributes work?

Edward

9/17/2006 12:27:00 PM

Both of these code examples for making class properties don't work
although they are pretty much straight out of the book "Programming
Ruby". What I am doing wrong here?

error is: undefined method `firstName' for #<User:0xb7f76b24
@firstName="Newton"> (NoMethodError)
----------------------
class User

attr_writer :firstName

def initialize(firstName)
@firstName = firstName
end
end

user = User.new('Hal')
user.firstName = 'Newton'
print user.firstName
--------------------------------

class User

def firstName=(newFirstName)
@firstName = newFirstName
end

def initialize(firstName)
@firstName = firstName
end
end

user = User.new('Hal')
user.firstName = 'Newton'
print user.firstName

--------------------------------

Thanks,

Edward Tanguay
All my projects: http://www.ta...

3 Answers

Marcin Mielzynski

9/17/2006 12:33:00 PM

0

Edward wrote:
> Both of these code examples for making class properties don't work
> although they are pretty much straight out of the book "Programming
> Ruby". What I am doing wrong here?
>
> error is: undefined method `firstName' for #<User:0xb7f76b24
> @firstName="Newton"> (NoMethodError)
> ----------------------

you didn't create an attribute reader:

attr_accessor :firstName - creates both reader and writer

attr_writer :firstName - creates a writer

attr_reader :firstName - creates a reader (the one that you need)

You can also create a reader by hand:

def firstName
@firstName
end


lopex

Farrel Lifson

9/17/2006 12:36:00 PM

0

On 17/09/06, Edward <edward@tanguay.info> wrote:
> Both of these code examples for making class properties don't work
> although they are pretty much straight out of the book "Programming
> Ruby". What I am doing wrong here?
>
> error is: undefined method `firstName' for #<User:0xb7f76b24
> @firstName="Newton"> (NoMethodError)
> ----------------------
> class User
>
> attr_writer :firstName
>
> def initialize(firstName)
> @firstName = firstName
> end
> end
>
> user = User.new('Hal')
> user.firstName = 'Newton'
> print user.firstName
> --------------------------------
>
> class User
>
> def firstName=(newFirstName)
> @firstName = newFirstName
> end
>
> def initialize(firstName)
> @firstName = firstName
> end
> end
>
> user = User.new('Hal')
> user.firstName = 'Newton'
> print user.firstName
>
> --------------------------------
>
> Thanks,
>
> Edward Tanguay
> All my projects: http://www.ta...
>
>
>

'attr_writer :firstName' only creates the 'firstName=' method just
like 'attr_reader :firstName' will only create the 'firstName' method.
What you need is 'attr_accessor :firstName' which creates both.

Farrel

Robert Klemme

9/17/2006 2:00:00 PM

0

Edward wrote:
> Both of these code examples for making class properties don't work
> although they are pretty much straight out of the book "Programming
> Ruby". What I am doing wrong here?
>
> error is: undefined method `firstName' for #<User:0xb7f76b24
> @firstName="Newton"> (NoMethodError)
> ----------------------
> class User
>
> attr_writer :firstName

Just an additional note: the usual conventions for method and variable
identifiers in Ruby is to separate words by underscore and use camel
case only for class and module names. So that would rather be

class User
attr_accessor :first_name
end


Kind regards

robert