Calling a method with an instance

P

Paul Roche

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 method `amount'
for main
:Object (NameError)


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

Jedrin

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 method `amount'
for main
:Object (NameError)

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

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

Jedrin

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

Jeremy Bopp

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 method `amount'
for main
:Object (NameError)


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
 
P

Paul Roche

Jedrin said:
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?
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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?
 
J

Jeremy Bopp

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top