January 12th, 2012, 04:10 AM
A little Ruby project
Howdy everyone! It's been a while since I was here since the last book I was using to learn Ruby wasn't too friendly to people that didn't know programming. It was more of a book for people that could program, and wanted to learn a new language.
Anyway... so, this one is GREAT.
Here's the exercise I was to do.
I decided to combine them. The first part I have down. It's the second part that isn't working.
Here's my code.
Initially I tried it without using the ".to_i" at the end of the "favenumber" part. That didn't work, so I thought "duh, because it's not a number, it's a string!" So I added that. I get the same error message.
Could someone explain to me what I did wrong? I can't figure out what the deal is.
January 13th, 2012, 11:35 AM
It's pretty much as the error message says: you are trying to treat a Fixnum as a String, and it doesn't convert them automatically. Try the following:
(You could also simply add the conversion directly to the expression, like so:
betternumber = favenumber.to_i + 1
puts 'Well what do you think about ' + betternumber.to_s + '? It\'s one bigger, and thus must be one better!'
That is a bit ugly IMAO, however.)
puts 'Well what do you think about ' + (favenumber.to_i + 1).to_s + '? It\'s one bigger, and thus must be one better!'
January 14th, 2012, 12:55 AM
Oh man, that worked out great! Thanks for the help!!!
You said that looked a bit ugly, though. Could you give me an example of how you'd do it? All of the things I used were the only things I've learned so far (I haven't learned about objects or anything), so you might have to explain it (if you'd be so kind).
And as I have no programming experience at all, if you are willing to show me and explain, try to make it as painfully clear as possible using minimal lingo. It'd go right over my head otherwise, lol.
Oh yeah, and thanks extra for showing me two ways to do it.
Edit: Man I feel stupid sometimes. Since I was stuck at this little assignment, I wouldn't move on until I completed it. In the very next chapter, he explained that a lot of people got stuck here. And it happened to be the exact same issue I had. He breaks it down. Had I just moved on, I would have seen this. But I refused to leave the problem unsolved.