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 trial

Ruby Ruby Operators and Control Structures Logical Operators Multiple Conditions with && and ||

Todd MacIntyre
Todd MacIntyre
12,248 Points

Can 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?

(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).

Todd MacIntyre
Todd MacIntyre
12,248 Points

Ruby 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

I 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
Todd MacIntyre
12,248 Points

I 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?

I 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
Todd MacIntyre
12,248 Points

Hi 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!

Sometimes newer versions get rid of scraps like the "print" method, so I recommend trying my program once.