Search code examples
ruby-on-railsrubyrabl

Why does this RABL node return nil?


node :transitType do |u|
    :Entry if u.type == 'HorseEntryTransit'
    :Exit  if u.type == 'HorseExitTransit'
end

I tried the following and this code snippet returns true.

node :transitType do |u|
    u.type == 'HorseEntryTransit'
end

Solution

  • You can improve the code block and avoid testing two conditions ( when the first one is met). Also, presume a case where neither condition is met. It looks like you would get nil when the second condition fails even though the first condition was true.

     node :transitType do |u|
         if u.type == 'HorseEntryTransit'
              :Entry 
         elsif u.type == 'HorseExitTransit'
              :Exit
         else
              nil #or :Neither
         end
    end
    

    Your code does not work when the first condition is actually met since the second condition will still be tested and the result of that condition is nil.