Question about variables in ruby

hello folks i am preparing a ruby plugin for sketchup, i hope you can
help me with this really nasty problem i am having

here is the method ,which belongs to a class

def moment_calc

weight = 0
variable_load = 0
permanent_load = 0
moment = 0

weight =((25.0*@b*@h) / 10**6)
prompts = ["Variable Load(kN/m) ", "Permanent Load(kN/m) ", "Max

Moment known(Nmm)"]
loads = [0.0, 0.0, 0.0]
results = inputbox prompts, loads, “Other Loads”
variable_load, permanent_load, moment = results

if (moment != 0)
  @M = moment
else
  uls = (1.35*variable_load + 1.5*permanent_load +

1.5weight)@length
@M = 0.11uls@length
@shear = 0.60 * uls
end
end

my @b = 280 and @h=300 from before… when i try to multiply the weight
it should come 2.1 but in this case it is 0.0032 or so… which is
wrong… hence because of that my uls values are wrong and hence the
entire following program… i am pretty sure its problem with the
variables… just cant figure out where!

Mahadev I. wrote:
[…]

my @b = 280 and @h=300 from before… when i try to multiply the weight
it should come 2.1 but in this case it is 0.0032 or so…

Then obviously @b and @h are not being set correctly.

which is
wrong… hence because of that my uls values are wrong and hence the
entire following program… i am pretty sure its problem with the
variables… just cant figure out where!

Well, what do your tests say?

BTW, your code is problematic. Two tips:

  • That’s a pretty long method. Refactor parts of it into shorter
    methods if possible.

  • Never, never use Floats for arithmetic – they’re imprecise. Use
    integers or BigDecimal.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

BTW, your code is problematic. Two tips:

  • That’s a pretty long method. Refactor parts of it into shorter
    methods if possible.

  • Never, never use Floats for arithmetic – they’re imprecise. Use
    integers or BigDecimal.

hi thanks for your tip i will certainly look into it!

I dont think the problem is with the @b or @h because when i output
these values within the code it seems to be fine! its quiet confusing…
but yes i will read through the book to make the shorter!

Jesús Gabriel y Galán wrote:>

This calculation is correct: 2.1, so if you are getting a different
result it has to be that @b or @h don’t contain what you think they
do. Can you try to print @b, @h and weight, and show us the calling
code that produces weight being 0.00032?

def moment_calc
puts “@b is #{@b}”
puts “@h is #{@h}”
weight =((25.0*@b*@h) / 10**6)
puts “weight is #{weight}”
end

If you put this in your program, what happens?

Jesus.

actually since this is sketchup, i cannot output data in terms of
puts… i have to make a vcb messagebox or a UI messagebox, both of
which are visual outputs,
Also I am unable to use bigDecimal in the ruby script even when i put
“require ‘bigdecimal’” in the beginning it is not supported by it… but
you have to believe me it does come as 0.00032 as i saif and not 2.1!
its quiet frustrating…

On Thu, Nov 5, 2009 at 1:18 PM, Mahadev I. [email protected]
wrote:

moment = 0
else
entire following program… i am pretty sure its problem with the
variables… just cant figure out where!

irb(main):001:0> @b = 280
=> 280
irb(main):002:0> @h = 300
=> 300
irb(main):003:0> weight =((25.0*@b*@h) / 10**6)
=> 2.1

This calculation is correct: 2.1, so if you are getting a different
result it has to be that @b or @h don’t contain what you think they
do. Can you try to print @b, @h and weight, and show us the calling
code that produces weight being 0.00032?

def moment_calc
puts “@b is #{@b}”
puts “@h is #{@h}”
weight =((25.0*@b*@h) / 10**6)
puts “weight is #{weight}”
end

If you put this in your program, what happens?

Also, you don’t need this:

weight = 0
variable_load = 0
permanent_load = 0
moment = 0

in Ruby you don’t need to declare your variables before assigning
(another value) to them. So assigning them a 0 only to assign them
another value afterwards is not needed.

Jesus.

i believe i may have found the solutions… sketchup automatically
converts my numbers which is actually in mm and i was stupid enough to
put them as 280.mm which makes it numeric 280 mm and thus it becomes
11.8 inches, which is where i have been getting the 0.00032… i think
going here was the best thing i did today

http://code.google.com/apis/sketchup/docs/ourdoc/numeric.html#to_mm

thank you very much Marnen and Jesus
regards
Mahadev