Calling a method with an instance

Hi. I’m playing around with creating methods and then creating an
instance that calls the instance. Here’s my code…

class Discount
attr_accessor :amount, :discount

def initialize(am, dis)
@amount = am
@discount = dis

end

def self.discount_amount(amt, disc)
newamount = amt - disc
end

end

dis1 = Discount.new(100, 20)

dis1.discount_amount(amount, discount)

The error I get is…

discount.rb:25:in <main>': undefined local variable or methodamount’
for main
:Object (NameError)

What is the best way to call a method with an instance?
Thanks

On Oct 5, 2:59 pm, Paul R. [email protected] wrote:

end

The error I get is…

discount.rb:25:in <main>': undefined local variable or methodamount’
for main
:Object (NameError)

What is the best way to call a method with an instance?
Thanks

Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

On Oct 5, 3:20 pm, Jedrin [email protected] wrote:

def initialize(am, dis)

What is the best way to call a method with an instance?
Thanks

Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

On 10/5/2010 1:59 PM, Paul R. wrote:

dis1 = Discount.new(100, 20)

What is the best way to call a method with an instance?
Thanks

There are a couple issues. First of all, you are trying to use two
variables (amount and discount) without giving them any values, in other
words without defining them. That’s why you get the error “undefined
local variable or method `amount’”. Perhaps you mean to use dis1.amount
and dis1.discount instead.

Once you clear that hurdle, you’ll find you also declared the
discount_amount method as a class method on Discount, not an instance
method. However, you go on to call the method as an instance method of
dis1. I think the corrected code would be as follows:

Discount.discount_amount(dis1.amount, dis1.discount)

-Jeremy

Um, I think you guys mean
Discount.discount_amount(dis1.amount,dis1.discount)

discount_amount is not an instance method. Or am I missing something?

Jedrin wrote:

On Oct 5, 3:20�pm, Jedrin [email protected] wrote:

�def initialize(am, dis)

What is the best way to call a method with an instance?
Thanks

Posted viahttp://www.ruby-forum.com/.

I think u want:
dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

Thanks for this. What is the best way to go about dealing with a method
such as this that has two parameters?

On 10/5/2010 2:32 PM, Paul R. wrote:

dis1.discount_amount(dis1.amount, discount)

this works, but not sure if this approach is most sensible

dis1.discount_amount(dis1.amount, dis1.discount)

Thanks for this. What is the best way to go about dealing with a method
such as this that has two parameters?

I think the comment about sensibility regards the passing of an object’s
internal values to one of the object’s own methods. Assuming you
redefine the discount_amount method as an instance method, there is no
need to pass in these values since they will be available to the method
as @amount and @discount:

class Discount
attr_accessor :amount, :discount

def initialize(amount, discount)
@amount = amount
@discount = discount
end

defined discount_amount as an instance method rather than a

class method

def discount_amount
@amount - @discount
end
end

dis1 = Discount.new(100, 20)
dis1.discount_amount # => 80

-Jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs