Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby, conversin to string

Tags:

string

ruby

I wrote this program in ruby:

"Nonna = CIAO CARO NIPOTINO, COME STAI??"
 puts "Tu = Bene nonna, ma non urlare"
 puts "Nonna = COME DICI, PARLA PIU' FORTE!!"

def ask()
    a = gets.chomp
    ok = a.upcase()
    numero = rand(100)
    ciao = "CIAO NONNA!"
    if a == ok
       puts "NO CARO, NON LO VEDO DAL " + numero
       ask
    else
       puts "COSA HAI DETTO? NON CI SENTO, RIPETI!"
       ask
    end
end

ask()

(Excuse the Italian) However, when I run it it gives me this error:

Traceback (most recent call last):
    2: from vecchia.rb:20:in `<main>'
    1: from vecchia.rb:11:in `chiede'
vecchia.rb:11:in `+': no implicit conversion of Integer into String (TypeError)

What can I do?

like image 471
Luca Gamerro Avatar asked Jul 03 '26 15:07

Luca Gamerro


1 Answers

It's complaining about a number concatened to a string. Two simple ways

1) turn the number into a string

puts "NO CARO, NON LO VEDO DAL " + numero.to_s

or 2) interpolate the number into the string

puts "NO CARO, NON LO VEDO DAL #{numero}"
like image 71
Ursus Avatar answered Jul 09 '26 08:07

Ursus