Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTodd MacIntyre
12,248 PointsCan someone explain why ...
print "What is your favorite number? "
number = gets.chomp.to_i
if (number == 3) || (number == 5)
puts "That's my favorite number!"
elsif (number > 10) && (number.even?)
puts "That's a pretty high even number!"
elsif (number.odd?) && (number % 3 == 0)
puts "That number is divisible by 3 and odd, cool!"
end
When I put this code into the treehouse workspace, the code prints as expected:
$ ruby favorite_number.rb
What is your favorite number? 12
That's a pretty high even number!
However, when I create my own doc, and put the exact same code into it, and run it on git bash where ruby is locally installed, this is my console output:
$ ruby Ask_Numbers.rb
12
What is your favorite number? That's a pretty high even number!
I am not prompted with "What is your favorite number?" before typing in my answer '12'. Is there some technical explanation for this?
Todd MacIntyre
12,248 PointsRuby version: ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32]
Running on Git Bash: git version 2.7.1.windows.1
Operating on Windows 7
Cena Mayo
55,236 PointsI just noticed you're calling two different file names: favorite_number.rb and Ask_Numbers.rb. Are you sure you're calling the correct file, and that file has the correct code?
Todd MacIntyre
12,248 PointsI happened to name my file differently when creating my own local copy. The code is the exact same and was copied from the workspace into my local file. Could this possibly be a glitch with the way Git Bash renders Ruby executable files? Should I try a different console emulator such as cmder and try there?
Alexander Davison
65,469 PointsI see. Your Ruby version on your terminal (command line) is probably older than the version on Treehouse, so it probably does not support the "print" method on line 1.
Try copy and pasting this into your code (I only modified the first line):
puts "What is your favorite number? "
number = gets.chomp.to_i
if (number == 3) || (number == 5)
puts "That's my favorite number!"
elsif (number > 10) && (number.even?)
puts "That's a pretty high even number!"
elsif (number.odd?) && (number % 3 == 0)
puts "That number is divisible by 3 and odd, cool!"
end
Hope it helps! ~xela888
Todd MacIntyre
12,248 PointsHi Xela, I installed my terminal and version of Ruby well after these videos were made, so my version would actually be newer than the one used in the video. Thanks for the thought though!
Alexander Davison
65,469 PointsSometimes newer versions get rid of scraps like the "print" method, so I recommend trying my program once.
Cena Mayo
55,236 PointsCena Mayo
55,236 Points(edited syntax highlighting as mod)
I don't have a good answer, except to say that I recreated a file on my local machine with your code and it works as expected. (OSX, Ruby 2.2.3). What operating system/version of Ruby are you using? (wondering if that may be part of the issue).