0

I have a method that I created that does some calaculation's based on an integer passed as a parameter. When I add the method to my view it only outputs the last variable.

Budget.rb(model)

def self.percent_calculate(question)
    reception = question * 0.5
    ceremony = question * 0.03
    clothes = question * 0.1
    flowers = question * 0.1
    entertain = question * 0.1
    photo = question * 0.12
    rings = question * 0.03
    misc = question * 0.02
end

index.html.erb(View)

<%= Budget.percent_calculate(5000.to_i) %>

Output from this looks is: 150.0, which is the calculation of the misc variable.

I want to output each variable out separately one after the other.

1
  • This is a very basic question for Ruby programming. It'd be really smart for you to learn a lot more about Ruby before working in Rails. Things that are mystical or magical to you about Rails will be easily understood with a year of Ruby under your belt. Commented Mar 28, 2013 at 5:11

2 Answers 2

2

ruby only returns the last line of the method unless you call return in the middle of the method. That said, you'd want to separate your calculation to different methods and call them 1 by 1. With some metaprogramming, this can be achieved by

CALCULATION_PERCENTAGES = {
  reception: 0.5,
  ceremony: 0.03,
  clothes: 0.1,
  flowers: 0.1,
  entertain: 0.1,
  photo: 0.12,
  rings: 0.03,
  misc: 0.02
}

class << self
  CALCULATION_PERCENTAGES.each do |cp, weight|
    define_method "#{cp}_value" do |question|
      weight * question
    end
  end
end

Then you can just call

<%= Budget.reception_value 5000 %>
<%= Budget.ceremony_value 5000 %>
Sign up to request clarification or add additional context in comments.

2 Comments

This is very nice. If you can understand this you'll have gone a long way towards understanding Ruby.
@jvnill With this solution you need to use Budget.new.reception_value. Define the methods inside class << self :)
1

Every method in Ruby returns the value of the last statement in the method, which is why you're getting misc.

You're saying you want to output each variable. You either need separate methods, or you need to return some kind of array, object, or hash that compacts all the data you're trying to use. For example, this would return a hash:

def self.percent_calculate(question)
{:reception => question * 0.5,
  :ceremony => question * 0.03,
  :clothes => question * 0.1,
  :flowers => question * 0.1,
  :entertain => question * 0.1,
  :photo => question * 0.12,
  :rings => question * 0.03,
  :misc => question * 0.02}
end

And then you could access it like

calculations = percent_calculate(question)
calculations[:reception]

to get the reception value.

1 Comment

Walking around yesterday I remembered that there's an obscure exception to Ruby returning the last statement in any method. If you define a setter inside a class, Ruby will return the value of the argument, no matter what the code in the setter is. For example, class Foo; def bar=(b); @b = b; return 5; end; end will have Foo.new.bar=7 return 7, not 5. And thanks for the edit, Tin Man. I got rushed at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.