# Passing a variable

Discussion in 'Ruby' started by flebber, Nov 15, 2010.

1. ### flebberGuest

I recently discussed in this group the basic starting of a project. I
am getting a little confused at passing a variable between a
calculation.

I am learning so can take some feedback and criticism the part of the
code I am referring to is Scenario 1. I am trying to ensure that the
calcualted value of x is never greater than 1.0. So I am aiming to
determine the value of x for the second calc if the result of the
first calculation produces a valid result.

r = 5.0
p = 10.0
per = 190.0
max_x = 0.8

x = 0.05
const = r*100 # simplified without X
x += 0.05 while (x*const < per) && (x <= max_x)
puts x <= max_x ? "#{x*100.0}%" : "no answer"

Bank = \$500.00 # later to be a running total calculated

def ROIcalc
const = r*100 # simplified without X
x += 0.05 while (x*const < per) && (x <= max_x)
puts x <= max_x ? "#{x*100.0}%" : "no answer"
end

def pool
if Bank > 200 then Bank * 0.05
Else \$10.00
end

# here in case I need it
def round
return floor(self+0.5) if self > 0.0
return ceil(self-0.5) if self < 0.0
return 0.0
end

# Scenario 1
# simple 2 option scenario where the max allocation is to option a
a = ROIcalc(per = 190, max_x = 0.8, puts "What ratio of return do you
expect?" r = gets.chomp, pool)
If for x in a < 0.6 then x in a = 0.6 & for x in b = 0.4
elseif x > 0.8 then puts " This option is invalid you exceed
allocation" # take user somewhere
# Take user to choose another option
else for x in b = 1.0 - x
b = ROIcalc(per = 200, max_x = 0.4, r = 12.00, pool)

flebber, Nov 15, 2010

2. ### flebberGuest

On Nov 15, 11:40 pm, flebber <> wrote:
> I recently discussed in this group the basic starting of a project. I
> am getting a little confused at passing a variable between a
> calculation.
>
> I am learning so can take some feedback and criticism the part of the
> code I am referring to is Scenario 1. I am trying to ensure that the
> calcualted value of x is never greater than 1.0. So I am aiming to
> determine the value of x for the second calc if the result of the
> first calculation produces a valid result.
>
> r = 5.0
> p = 10.0
> per = 190.0
> max_x = 0.8
>
> x = 0.05
> const = r*100 # simplified without X
> x += 0.05 while (x*const < per) && (x <= max_x)
> puts x <= max_x ? "#{x*100.0}%" : "no answer"
>
> Bank = \$500.00 # later to be a running total calculated
>
> def ROIcalc
> const = r*100 # simplified without X
> x += 0.05 while (x*const < per) && (x <= max_x)
> puts x <= max_x ? "#{x*100.0}%" : "no answer"
> end
>
> def pool
> if Bank > 200 then Bank * 0.05
> Else \$10.00
> end
>
> # here in case I need it
> def round
>   return floor(self+0.5) if self > 0.0
>   return ceil(self-0.5)  if self < 0.0
>   return 0.0
> end
>
> # Scenario 1
> # simple 2 option scenario where the max allocation is to option a
> a = ROIcalc(per = 190, max_x = 0.8, puts "What ratio of return do you
> expect?" r = gets.chomp, pool)
>         If for x in a < 0.6 then x in a = 0.6 & for x in b = 0.4
>                 elseif x > 0.8 then puts " This option isinvalid you exceed
> allocation" # take user somewhere
>                 # Take user to choose another option
>                 else for x in b = 1.0 - x
>         b = ROIcalc(per = 200, max_x = 0.4, r = 12.00, pool)

totally changed

flebber, Nov 16, 2010

3. ### flebberGuest

On Nov 15, 11:40 pm, flebber <> wrote:
> I recently discussed in this group the basic starting of a project. I
> am getting a little confused at passing a variable between a
> calculation.
>
> I am learning so can take some feedback and criticism the part of the
> code I am referring to is Scenario 1. I am trying to ensure that the
> calcualted value of x is never greater than 1.0. So I am aiming to
> determine the value of x for the second calc if the result of the
> first calculation produces a valid result.
>
> r = 5.0
> p = 10.0
> per = 190.0
> max_x = 0.8
>
> x = 0.05
> const = r*100 # simplified without X
> x += 0.05 while (x*const < per) && (x <= max_x)
> puts x <= max_x ? "#{x*100.0}%" : "no answer"
>
> Bank = \$500.00 # later to be a running total calculated
>
> def ROIcalc
> const = r*100 # simplified without X
> x += 0.05 while (x*const < per) && (x <= max_x)
> puts x <= max_x ? "#{x*100.0}%" : "no answer"
> end
>
> def pool
> if Bank > 200 then Bank * 0.05
> Else \$10.00
> end
>
> # here in case I need it
> def round
>   return floor(self+0.5) if self > 0.0
>   return ceil(self-0.5)  if self < 0.0
>   return 0.0
> end
>
> # Scenario 1
> # simple 2 option scenario where the max allocation is to option a
> a = ROIcalc(per = 190, max_x = 0.8, puts "What ratio of return do you
> expect?" r = gets.chomp, pool)
>         If for x in a < 0.6 then x in a = 0.6 & for x in b = 0.4
>                 elseif x > 0.8 then puts " This option isinvalid you exceed
> allocation" # take user somewhere
>                 # Take user to choose another option
>                 else for x in b = 1.0 - x
>         b = ROIcalc(per = 200, max_x = 0.4, r = 12.00, pool)

totally changed the way I was going about it.

Can this be simplified any further?

# Bank = A running total
def pool
if Bank > 200 then Bank * 0.05
Else \$10.00
end

def ROIcalc2
((xunits * max_return)/pool)*100 > Per
end

# scenario 1
xunits = (0.6)step(0.8, 0.05)
Per = 190
puts "What ratio of return do you expect?"
gets.chomp
ROI = (gets.chomp).to_f
max_return = pool * ROI
when xunits in ROIcalc2(xunits, max_return, pool, Per) is true then
Xunits_2 = 1.0 - xunits
puts "xunits equals " + xunits + " xunits2 equals " + Xunits_2
else
puts " This is am invalid option please choose another option "
end

flebber, Nov 16, 2010
4. ### Brian CandlerGuest

Sayth Renshaw wrote in post #961804:
> # Bank = A running total
> def pool
> if Bank > 200 then Bank * 0.05
> Else \$10.00
> end
>
> def ROIcalc2
> ((xunits * max_return)/pool)*100 > Per
> end
>
> # scenario 1
> xunits = (0.6)step(0.8, 0.05)
> Per = 190
> puts "What ratio of return do you expect?"
> gets.chomp
> ROI = (gets.chomp).to_f
> max_return = pool * ROI
> when xunits in ROIcalc2(xunits, max_return, pool, Per) is true then
> Xunits_2 = 1.0 - xunits
> puts "xunits equals " + xunits + " xunits2 equals " + Xunits_2
> else
> puts " This is am invalid option please choose another option "
> end

What language is this in? Did you try running it?

Getting your code to work is arguably more important than style

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

Brian Candler, Nov 18, 2010