Home > Enterprise >  What will this ruby function be returning
What will this ruby function be returning

Time:11-09

I have a code which calls a boolean function.

can_process = done_recently?(load)

Here is how the done_recently function looks.

def done_recently?(load)
      time_window = 10000
      load['terminatedAt'] && (load['terminatedAt'] > time_window.minutes.ago.utc.iso8601)
    end

In my json data, inside

"load": [{ "terminatedAt": null }]

among lots of other data . This json data is converted into a hash before calling these functions.

What will the done_recently? function return(true/false)? I am new to ruby so i am getting a little confused. please help me out.

I have tried replicating in irb but got confused midway because of some errors.

CodePudding user response:

When your input is { "terminatedAt": null } then

def done_recently?(load)
  time_window = 10000
  load['terminatedAt'] && (load['terminatedAt'] > time_window.minutes.ago.utc.iso8601)
end

will return nil. nil is returned because the first part of load['terminatedAt'] && ... evaluates to nil which is considered falsy and there for the second part after the && will not be evaluated anymore and the nil is returned immediately.

  • Related