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 Ruby Control Structures Elsif challenge

Is this acceptable, and what are the alternative ways to write this?

going_speed_limit = !too_fast = car_speed > speed_limit ? true : false

ruby.rb
car_speed = 55
speed_limit = 55

going_speed_limit = !too_fast = car_speed > speed_limit ? true : false

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

If you're going for one-liner here. You can just write it as

going_speed_limit = !too_fast = car_speed > speed_limit

the ? true : false at the end is not needed.

Or simply

going_speed_limit = !(car_speed < speed_limit)

I don't think that's how the code challenge is expecting you to solve it; but the grader is fine with it; so I guess it's alright.

Can't believe I didn't think of that XD I was doing something similar for earlier challenges

I just learned the ternary operator so decide to use it (before my code it didn't use ? true : false it was the something a bit different)

Michael Hess
Michael Hess
24,512 Points

Hi Andrew,

Yes, that is an acceptable way using the ternary operator -- it's also more terse. I like it!

Another way of writing the same thing is:

car_speed = 55
speed_limit = 55


if car_speed < speed_limit
  too_fast = false  
else if car_speed > speed_limit
  too_fast = true
else
  going_speed_limit = true;
end
end

Hope this helps!