Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby unusual type inference

Tags:

ruby

I want to declare a float variable, with which I will later do some computations. However, the interpretor infers the variable is an Array and will later produce an error when I try to use the variable as a float.

avg = 0.0, nr = 0
p "avg is #{avg.class} "

Outputs the following:

"avg is Array "
like image 841
octavian Avatar asked Aug 12 '26 04:08

octavian


1 Answers

Your code doesn't do what you think it does, and it's easy to see:

> a=2, b=3
=> [2, 3]
> b
=> 3
> a
=> [2, 3]

Use separate statements for variables or use the following form:

avg, nr = 0.0, 0
like image 128
Karoly Horvath Avatar answered Aug 13 '26 23:08

Karoly Horvath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!