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 The And (&&) Operator

Does anyone have succes with && operator question

I have with () and no results the gets.chomp.to_i is nessasity for correct input and 2 times the end keyword safes al lot of mistakes. No how to output it wright.

ruby.rb
def check_speed(car_speed)
  # write your code here
  car_speed = gets.chomp.to_i
  if (car_speed >= 40) && (car_speed <=50)
    puts "safe"
  end
end

3 Answers

Hi there,

I read that differently and came up with:

def check_speed(car_speed)
  # write your code here

  if(car_speed >= 40 && car_speed <= 50)
    return "safe"
  end
end

Seems to work for me.

Steve.

Hi Steve, i got an io error if (gets.chomp.to_i) statement was not included !

How strange - that code worked for me.

You're using gets to read user input but car_speed is passed into the method as a parameter. I think gets creates a string from user input which you can't evaluate using the operators >= or less than equal to. I think that's your issue.

If you assume that the parameter car_speed has already been created as an integer, then the comparison operators will work and you'll not need to use to_i. That's required in your code to convert from a string to an integer - that's why you get I/O issues without it because you're creating a string and then trying to compare it with a nuber - that won't work.

So, drop the user input, rely on the parameter being passed to the method, which is an integer, which allows mathematical comparison to be made without conversion. Then return the required string after the comparisons have been passed.

Make sense?

Steve.