Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

How can I simplify this method?

def check(foo, bar = nil)
  if bar
    @object.foo > foo && @object.bar > bar
  else
    @object.foo > foo
  end
end
share|improve this question

1 Answer

up vote 3 down vote accepted
def check(foo, bar = nil)
  @object.foo > foo && (bar.nil? || @object.bar > bar)
end

Another option if such checks are common place and longer.

def checkif(m,x)
  return x.nil? || @object.send(m) > x
end

def check(foo, bar = nil)
  [[:foo,foo], [:bar,bar]].all?{|v| checkif(v[0],v[1]) }
end
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.