0

I inherited a Ruby on Rails system. I found a tutorial on expressions, so I get the gist of what this does, but I'm not totally sure. Could someone help me:

t.apportioned_fare = this_trip_cost + (trip_position == trip_count ? ride_cost_remaining : 0)

What is the part inside the parentheses saying?

nikost
  • 788
  • 6
  • 14

1 Answers1

0

It's just an equality expression compared to the value of a ternary expression. Basically:

  1. If trip_count is truthy, then return ride_cost_remaining.
  2. If trip_count is falsy, then return 0.
  3. Compare trip_position to the return value of the ternary expression, and return a Boolean value based on whether they are equal.

What these values actually are and why the code was written this way can't be determined from your posted code, and I'm not sure that it makes sense to add a Boolean like true or false to this_trip_cost before assigning it to t.apportioned_fare.

Maybe your code works, and maybe it doesn't. Still, that's what the expressions are actually doing, but only you know what the values of each subexpression actually are.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199