February 2, 2004 Intro to Ruby Slide #8
Prev Next

"Scalars"

Ruby doesn't have scalars like Perl and Python do. Strings, integers, and floating point numbers are each individual classes. In practice, though, you can often use then interchangeably:
     foo = "pie"
     bar = 3.14159
     baz = 42
     sum = bar + baz

But some things don't work as expected:
     zero = '0'
     one = zero + 1 # doesn't work!

If you try to do that, you'll get the error message
     TypeError: failed to convert Fixnum into String

In Ruby, numeric strings are not automatically converted to numbers. Instead, you need to use the to_i method to do the conversion explicitly:
     zero = '0'
     one = zero.to_i + 1


Prev Copyright © 2004 Walter C. Mankowski Next