Ruby No Implicit Conversion of Fixnum into String (Typeerror)

Ruby no implicit conversion of Fixnum into String (TypeError)

Add a .to_i to your gets.chomp calls. You're trying to do math operations on text. When inputted from the console, everything starts out as text, even if it's numeric looking text.

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 TypeError: no implicit conversion of fixnum into string using json

finally, this issue solved by using .to_json .
json was considering values as a string instead of taking actual type of data.
I used, following format.

  • params: { email: "email@mailinator.com", password: "xyz", ttl: 300}.to_json

now its working fine.

Ruby Error: no implicit conversion of Fixnum into String

In Ruby, the + method for adding strings doesn't work on integers. In your example, the age variable is an integer. This might seem a little strange if you are used to working with JavaScript.

You need to use string interpolation

puts("Your age, #{age} is a good one")

This works.

Ruby - no implicit conversion of Fixnum into string

This is easy to see on the command line, where you pick your cards at random and the suit.

When you go to output the results of the deal to the screen, you do so using:

return "5" + suit

The goal here being that you want "5spades" or better, "5 of spades". The problem here is that suit is a Fixnum object, or very similar to an int.

While the class String does have a + operator it can only be used with other strings.

Here's an example to reproduce your error:

"ace" + 1
TypeError: no implicit conversion of Fixnum into String

To get what your after in simple terms, you can either convert the Fixnum to a string via .to_s like this:

> "ace" + 1.to_s
=> "ace1"

Or you can use string interpolation like this:

> "ace#{1}"
=> "ace1"

Rails 4 - No Implicit Conversion of Fixnum into String - Error only occurs in production

I figured this out on my own.

The secret_key_base I made for production in secrets.yml only contained digits; apparently YAML(or Ruby's implementation of it) interprets this automatically as an integer, and the secret_key_base must be a string. Perhaps it's also necessary for the key base to be alphanumeric. I'm not sure.

I wish Rails just did a type conversion on secret_key_base so I didn't have to waste half my work day trying to root out the issue. At the very least, it would have been nice to have a relevant warning as opposed to a cryptic error that doesn't point me in any direction.



Related Topics



Leave a reply



Submit