Nicholas james
Nicholas james

Reputation: 115

ruby: return true or false based on condition inside method

I created following method to return true or false

def method
  total_value = some_value
  item_value = item_value
  return true if total_value < item_value
end

I tried to put return true but only works when its true.

It returns true if total_value < item_value but returns null if its not true.

I also tried with if else and it works but is this the best way?

def method
   total_value = some_value
   item_value = item_value
  if total_value < item_value
    true
  else
    false
  end
end

Upvotes: 0

Views: 4273

Answers (1)

Derek Wright
Derek Wright

Reputation: 1492

You want something like this:

def method_name
  total_value < item_value
end

Ruby will always return the value of the last evaluated statement in the function.

Upvotes: 4

Related Questions