28

I am experimenting with arrays, and am reading the book "Beginning Ruby on Rails" by Steve Holzner. I made the program:

array = ['Hello', 'there', 1, 2]
puts array[1]
puts array[3]
puts array.length
array2 = Array.new
puts array2.length
array2[0] = "Banana"
array2[1] = 6
puts array2[0] + " " + array2[1]
puts array3.length

It doesn't do much, but when i run it i get the error

arrays.rb:9:in `+': can't convert Fixnum into String (TypeError)
    from arrays.rb:9

Why do I get this error?

6 Answers 6

52

You can't add a string and a integer (Fixnum), in this case you tried to add 6 to "Banana".

If on line 9 you did this:

puts array2[0] + " " + array2[1].to_s

You would get:

"Banana 6"
Sign up to request clarification or add additional context in comments.

2 Comments

I'd add throwing an error here is a feature of a strongly typed language, of which Ruby is one example. PHP, a weakly typed language, would allow such chicanery.
@bernie It's more of a direct result of Ruby's type coercion rules, not its strong typing. For example, Float + Fixnum in Ruby is legal because Ruby knows how to coerce one type to the other. You could actually reopen Fixnum and add a coerce method that would make String + Fixnum legal as well.
4

array2[1] is 6, which is a Fixnum. It doesn't know how to add itself to a string (which in this case is Banana. If you were to convert it to a string, it would work just fine.

puts array2[0] + " " + array2[1].to_s

Comments

3

The error is basically saying you cannot convert array2[1] (the value is a number, a type Fixnum in this case) into a String type. The way you would work around this is to convert the type into a String (this is for line 9 where the error occurs):

puts array2[0] + " " + array2[1].to_s

The array2[1].to_s converts the number into a String type.

Comments

1

Haven't tried this myself, but try replacing

puts array2[0] + " " + array2[1]

with

puts array2[0] + " " + array2[1].to_s

2 Comments

array2[0] is already a String type, you want to convert element 1 (array2[1]) as it holds the "6"
@Kevin Jalbert Yeah I saw it. Thanks!
0

Here is a way to convert a FixNum expression into a string,

x=2
print (x+20).to_s + "\sbanannas"

Didn't know you could tack on the FixnNum#to_s method to those parens.

Comments

0

you are trying to add an integer(fixnum) and a string, which is something you can't do on ruby unless you explicitly cast the integer(fixnum) to a string. in your code array2[0]holds a string value "banannas" and array2[1] holds an integer(fixnum) value 1. so for your code too run correctly you need too cast the value in array2[1] to a string value.

you can change your code on line 9 to this:

puts array2[0] + " " + array2[1]._s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.