question about variables in ruby

M

Mahadev Ittina

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!
 
M

Marnen Laibow-Koser

Mahadev Ittina 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,
 
M

Mahadev Ittina

Marnen said:
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!
 
J

Jesús Gabriel y Galán

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

=A0def moment_calc

=A0 =A0weight =3D 0
=A0 =A0variable_load =3D 0
=A0 =A0permanent_load =3D 0
=A0 =A0moment =3D 0

=A0 =A0weight =3D((25.0*@b*@h) / 10**6)
=A0 =A0prompts =3D ["Variable Load(kN/m) ", "Permanent Load(kN/m) ", "Max
Moment known(Nmm)"]
=A0 =A0loads =3D [0.0, 0.0, 0.0]
=A0 =A0results =3D inputbox prompts, loads, "Other Loads"
=A0 =A0variable_load, permanent_load, moment =3D results

=A0 =A0if (moment !=3D 0)
=A0 =A0 =A0@M =3D moment
=A0 =A0else
=A0 =A0 =A0uls =3D (1.35*variable_load + 1.5*permanent_load +
1.5*weight)*@length
=A0 =A0 =A0@M =3D 0.11*uls*@length
=A0 =A0 =A0@shear =3D 0.60 * uls
=A0 =A0end
=A0end

my @b =3D 280 and @h=3D300 from before.. when i try to multiply the weigh= t
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!

irb(main):001:0> @b =3D 280
=3D> 280
irb(main):002:0> @h =3D 300
=3D> 300
irb(main):003:0> weight =3D((25.0*@b*@h) / 10**6)
=3D> 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 =3D((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 =3D 0
variable_load =3D 0
permanent_load =3D 0
moment =3D 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.
 
M

Mahadev Ittina

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..
 
M

Mahadev Ittina

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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top