simple code doesnt work

Discussion in 'Ruby' started by Vince /., Sep 20, 2006.

  1. Vince /.

    Vince /. Guest

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


    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/.
    Vince /., Sep 20, 2006
    #1
    1. Advertising

  2. Hello Vince ;-) !

    > 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 :)
    >
    >
    > 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...)
    Vincent Fourmond, Sep 20, 2006
    #2
    1. Advertising

  3. Vince /.

    El Draper Guest

    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
    El Draper, Sep 20, 2006
    #3
  4. Vince /.

    Jan Svitok Guest

    On 9/20/06, Vince /. <> wrote:
    > 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 :)
    >
    >
    > 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()
    > end
    Jan Svitok, Sep 20, 2006
    #4
  5. Vince /.

    MonkeeSage Guest

    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
    MonkeeSage, Sep 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sachin bond
    Replies:
    1
    Views:
    276
    Kevin Goodsell
    Sep 7, 2003
  2. Janaka Perera
    Replies:
    0
    Views:
    1,111
    Janaka Perera
    Oct 29, 2007
  3. Replies:
    3
    Views:
    425
    alex23
    May 27, 2008
  4. Aristotle

    why a simple $a =~ $b doesnt work ?

    Aristotle, Jul 17, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    73
    Tassilo v. Parseval
    Jul 17, 2004
  5. pd
    Replies:
    2
    Views:
    111
    ChrisHolland@InternetBrands
    Jun 1, 2007
Loading...

Share This Page