1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Rep Power

    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.

    • Write a program that asks for a person’s first name, then middle, and then last. Finally, it should greet the person using their full name.
    • Write a program that asks for a person’s favorite number. Have your program add 1 to the number, and then suggest the result as a bigger and better favorite number. (Do be tactful about it, though.)
    I decided to combine them. The first part I have down. It's the second part that isn't working.

    Here's my code.

    puts 'Hello there! What is your first name?'
    firstname = gets.chomp
    puts 'Well that\'s a nice name. And your middle name?'
    middlename = gets.chomp
    puts 'Nice enough. And finally, your last name?'
    lastname = gets.chomp
    puts 'That\'s a good sounding name, ' + firstname + ' ' + middlename + ' ' + lastname + ', now can you tell me your favorite number?'
    favenumber = gets.chomp
    puts 'Well what do you think about ' + favenumber.to_i + 1 + '? It\'s one bigger, and thus must be one better!'
    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.

    can't convert Fixnum into String
    Could someone explain to me what I did wrong? I can't figure out what the deal is.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Alpharetta, GA
    Rep Power
    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:

    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!'
    (You could also simply add the conversion directly to the expression, like so:

    puts 'Well what do you think about ' + (favenumber.to_i + 1).to_s + '? It\'s one bigger, and thus must be one better!'
    That is a bit ugly IMAO, however.)
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Rep Power
    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.

IMN logo majestic logo threadwatch logo seochat tools logo