Simple code doesnt work

Hi everyone,

Can anyone tell me why this doesnt work?

Im getting this error
cf.rb:26: undefined method `price' for #<Ticket:0x27ed554
@d="11/12/2006", @price=5.0, @v="yer"> (NoMethodError)

thanks, just learning :slight_smile:

class Ticket

聽聽def initialize(venue,date)
聽聽聽聽@v = venue
聽聽聽聽@d = date
聽聽end

聽聽def venue
聽聽聽聽@v
聽聽end

聽聽def date
聽聽聽聽@d
聽聽end

聽聽def price=(amount)
聽聽聽聽@price = amount
聽聽end
end

ticket = Ticket.new("yer","11/12/2006")
puts "ticket stuff is #{ticket.venue} #{ticket.date}"

ticket.price = (5.00)

puts "ticket mooch is #{ticket.price}"

路路路

--
Posted via http://www.ruby-forum.com/.

Hello Vince :wink: !

Im getting this error
cf.rb:26: undefined method `price' for #<Ticket:0x27ed554
@d="11/12/2006", @price=5.0, @v="yer"> (NoMethodError)

thanks, just learning :slight_smile:

class Ticket
聽聽聽聽聽聽[...]
聽聽def price=(amount)
聽聽聽聽@price = amount
聽聽end
end

ticket = Ticket.new("yer","11/12/2006")
puts "ticket stuff is #{ticket.venue} #{ticket.date}"

ticket.price = (5.00)

puts "ticket mooch is #{ticket.price}"

聽聽You define the price= method, but you don't define the price method.
You can't access to instance variables from outside the instance unless
you explicitly allow it via appropriate accessors. In your case,
replacing the price= definition by

attr_accessor :price

should work pretty well. You might want to use attr_reader to only grant
read acces to an instance variable or attr_writer to only grant write
access.

聽聽Enjoy !

聽聽Vince (yet another one...)

Hey,

Vince /. wrote:

Hi everyone,

Can anyone tell me why this doesnt work?

*snip*

Sure, where you've got a method "price=(amount)", below:

聽聽def price=(amount)
聽聽聽聽@price = amount
聽聽end
聽聽

this only handles the assigning of the price variable. This means that:

ticket.price = (5.00)
聽聽

works fine, but trying to access ticket.price results in the error you're seeing (hence why the error doesn't occur on the above line, but on a later line). You simply need a method declaration for retrieving the variable value within your class, so try putting this in there:

def price
聽聽聽聽@price
end

This then ensures you've got one method ("price=") that handles the assignation of values, and one method ("price") to retrieve the current value.

Your following code should then work:

puts "ticket mooch is #{ticket.price}"

Hope that helps!

Cheers,
-= El =-

blog: http://www.crazycool.co.uk

Hi everyone,

Can anyone tell me why this doesnt work?

Im getting this error
cf.rb:26: undefined method `price' for #<Ticket:0x27ed554
@d="11/12/2006", @price=5.0, @v="yer"> (NoMethodError)

thanks, just learning :slight_smile:

class Ticket

聽聽聽聽聽聽聽聽def initialize(venue,date)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@v = venue
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@d = date
聽聽聽聽聽聽聽聽end

聽聽聽聽聽聽聽聽def venue
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@v
聽聽聽聽聽聽聽聽end

聽聽聽聽聽聽聽聽def date
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@d
聽聽聽聽聽聽聽聽end

聽聽聽聽聽聽聽聽def price=(amount)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@price = amount
聽聽聽聽聽聽聽聽end
end

ticket = Ticket.new("yer","11/12/2006")
puts "ticket stuff is #{ticket.venue} #{ticket.date}"

ticket.price = (5.00)

puts "ticket mooch is #{ticket.price}"

You need to define attribute reader:

def price
聽聽@price
end

BTW, you could accomplish the same by using
attr_reader(R)/attr_writer(W)/attr_accessor(RW):
(if you'd rename @v, @d to @venue, @date respectively)

class Ticket

聽聽聽聽聽聽聽聽def initialize(venue,date)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@venue, @date = venue, date
聽聽聽聽聽聽聽聽end

聽聽聽聽聽聽聽聽聽聽聽attr_reader :venue, :date # this will create venue() and date()
聽聽聽聽聽聽聽聽聽聽聽attr_accessor :price # this will create price=() and price()

路路路

On 9/20/06, Vince /. <totalharmonicdistortion@hotmail.com> wrote:

end

Vince /. wrote:

聽聽def price=(amount)
聽聽聽聽@price = amount
聽聽end
end

Hi there,

You have a price= method defined, but not a price method. So when you
say ticket.price there is no method for that.

Try this in your class:

def price
聽聽@price
end

Regards,
Jordan