# Question about variables in ruby

**URL:** <https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495>\
**Category:** Ruby\
**Created:** [November 5, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495 "2009-11-05T13:17:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/1 "2009-11-05T13:17:56Z")

</div>

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.5_weight)_@length  
@M = 0.11_uls_@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!

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/2 "2009-11-05T14:38:48Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/3 "2009-11-05T17:09:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/4 "2009-11-05T22:32:27Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/5 "2009-11-05T17:22:27Z")

</div>

On Thu, Nov 5, 2009 at 1:18 PM, Mahadev I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 5, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/question-about-variables-in-ruby/178495/6 "2009-11-05T22:47:38Z")

</div>

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](http://code.google.com/apis/sketchup/docs/ourdoc/numeric.html#to_mm)

thank you very much Marnen and Jesus  
regards  
Mahadev
