Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:29:32+00:00 2026-05-29T19:29:32+00:00

I am following the LearnRubyTheHardWay tutorial and having a difficulty in modifying the exercise

  • 0

I am following the LearnRubyTheHardWay tutorial and having a difficulty in modifying the exercise 29. Everything works fine if I define the variables like (as in the tutorial):

people = 100000
cats = 34
dogs = 56

However, if I try to get the variables from STDIN like:

puts "How many people are here?"
people = STDIN.gets.chomp()
puts "How many cats?"
cats = STDIN.gets.chomp()
puts "And how many dogs?"
dogs = STDIN.gets.chomp()

The equality operators return false results as if they only do calculate the results using the first two digits of the numbers. So if I type 100000000 to people and 11, 12 or 13 to cats, the methods return “Too many cats..” If I type 150000000 to people and anything <15 to cats, they return “Not many cats..” Additionally I have to modify

dogs += 5

to

dogs += "5"

otherwise I get the following error: in `+’: can’t convert Fixnum into String (TypeError)

If I leave the double quotes in place and revert to people = 10000 stuff, I get the following error: in `+’: String can’t be coerced into Fixnum (TypeError)

So to say, I have no problem with the code in the tutorial, just try to learn what causes the errors introduced by the STDIN methods. I looked into the RubyDoc.org, to see whether it is an issue with fixnum, integer or string classes or anything related to chomp or gets methods but couldn’t find the reason. I also tried to_i and to_s before or after but didn’t get any result.

The full source code of the file is below:

puts "How many people are here?"
people = STDIN.gets
puts "How many cats?"
cats = STDIN.gets
puts "And how many dogs?"
dogs = STDIN.gets

#people = 100000
#cats = 34
#dogs = 56

puts "So, %d people, %d cats and %d dogs, huh?" % [people,cats,dogs]

if people < cats
  puts "Too many cats! The world is doomed!"
end

if people > cats
  puts "Not many cats! The world is saved!"
end

if people < dogs
  puts "The world is drooled on!"
end

if people > dogs
  puts "The world is dry!"
end

dogs += "5"
puts "Now there are #{dogs} dogs."

if people >= dogs
  puts "People are greater than or equal to dogs."
end

if people <= dogs
  puts "People are less than or equal to dogs."
end

if people == dogs
  puts "People are dogs."
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:29:33+00:00Added an answer on May 29, 2026 at 7:29 pm

    The problem is that STDIN.gets returns a string. All the comparison operations will therefore operate on strings. Example:

    people = "100000000"
    cats = "11"
    
    puts people < cats  # => true!
    

    This is because < will compare the strings lexicographically (and 1000... comes before 11 in the alphabet). There is actually one point in your example that makes it pretty obvious what’s going on here:

    dogs = STDIN.gets
    dogs += "5"
    

    If you input 7 here, it should print out 75. You see, it just concatenates the strings.

    How to fix this? Easy, just convert the strings to integers:

    puts "How many people are here?"
    people = STDIN.gets.to_i
    puts "How many cats?"
    cats = STDIN.gets.to_i
    puts "And how many dogs?"
    dogs = STDIN.gets.to_i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// following code works fine n open notepad... class demo { public static void
Following this question: Good crash reporting library in c# Is there any library like
Following method is accepted if I set UIColor like: [tabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]]; However I
Following my previous question regarding the rationale behind extremely long functions, I would like
Following this writeup ... The compiled binary seems to work fine, but the installed
Following my previous question, in which I wanted to dump all the variables in
Following a previous question regarding ActiveMQ and WebSockets, I would like to know if
Following on from my recent question on Large, Complex Objects as a Web Service
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following Izb's question about Best binary XML format for JavaME , I'm looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.