Array Typeerror: Can't Convert Fixnum into String

Array TypeError: can't convert Fixnum into String

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"

+' can't convert Fixnum into String (TypeError)

Ruby (unlike some other languages) does not cast objects to strings when they are operands in String#+ method. Either cast to string manually:

puts 'Yeah ' + favenum.to_s + ' is nice enough but ' + better.to_s + ' is bigger and better by far!'

or use string interpolation (note the double quotes):

puts "Yeah #{favenum} is nice enough but #{better} is bigger and better by far!"

can't convert Fixnum into String (TypeError)

Seems like you are using Ruby 1.9 in your second example. The []= method has changed and only accepts strings.

You can convert an integer value to a string using chr:

@buff[4] = 0x30.chr

And retrieve a character's byte value with:

@buff[4].ord
# => 48

Ruby no implicit conversion of Fixnum into String (TypeError)... to_i not working

Your error is a result of trying to add string "" with number num.

Unlike JavaScript, which will try to convert types, Ruby does not allow you to use different types with math operators (unless they are numerical, such as float or integer).

Correct line 2 to say: sum = 0.

Ruby error - '+': no implicit conversion of Fixnum into String (TypeError)

I have managed to reproduce the problem and with some debugging output I can see that the first iteration runs fine and the problem only appears on the second iteration (shift = 2).

In the first iteration charLine is an array of integers, so integer + integer works out fine for this line: charLine.each_index { |i| charLine[i] = (charLine[i] + shift)}

But then charLine is converted into an array of strings on the next line of the loop:

 #converting back to letters
charLine.each_index { |x| charLine[x] = charLine[x].chr }

So at the start of second iteration charLine is now an array of Strings, because it wasn't converted back. Then on the .each_line it tries to add up string + integer and it blows up.

The solution is to re-map your string array to integer at the start of every iteration.

charLine.map!(&:ord)

The alternative is to not modify the array and save the results into a temp variable, here is a working example:

def caesar_cipher(string)
shiftyArray = []
charLine = string.split(//)
charLine.map!(&:ord)

shift = 1
alphabet_size = 26
while shift <= alphabet_size
shifted_array = charLine.map { |c| (c + shift) < 122 ? (c + shift) : (c + shift) - 26 }
shifted_array.map!(&:chr)
p shifted_array

shift += 1
shiftyArray.push(shifted_array.join)
end
end

caesar_cipher("testing")

ruby TypeError - no implicit conversion of Fixnum into String?

This code is extremely risky and I can't see a reason for doing this in the first place. Remove the eval and you get this very ordinary code:

File.open('nagai.txt', 'a+') do |f|
f.puts parts[params[:salutation]]
end

The error comes from trying to concatenate a Fixnum/Integer to a String in the process of constructing the code you then eval. This code is invalid and yields the same error:

"1" + 1

Ruby isn't like other languages such as JavaScript, PHP or Perl which arbitrarily convert integers to strings and vice-versa. There's a hard separation between the two and any conversion must be specified with things like .to_s or .to_i.

That fixed version should be equivalent. If you need to defer this to some later point in time, you can write a method:

def write_nagai(params)
File.open('nagai.txt', 'a+') do |f|
f.puts parts[params[:salutation]]
end
end

No implicit conversion of Fixnum into String, though to_i is used

You can't add a number to a string in ruby. You have to make it a string.

puts 'Hello, what\' your favorite number?'
number = gets.to_i
puts 'Here\' a better bigger favorite number - ' + (number + 1).to_s
# or
bigger_number = number + 1
puts 'Here\' a better bigger favorite number - ' + bigger_number.to_s
# or
puts "Here's a better bigger favorite number - #{bigger_number}"

Ruby, can't convert fixnum into array? but it is an array

In order to use @array.tranpose, your @array needs to be array of arrays. With normal array you get this error message.

Edit:

In your test you are setting the following for the @board_layout:

 board.state = [0,0,1,0,0,1,0,0,1]

and when you do

@board_layout.transpose[row]

You'll get the error message.



Related Topics



Leave a reply



Submit