3

Is there a shorter way to do this in ruby?

if defined? some_var
    other_var = some_var
end

Set other_var with the value of some_var if and only if some_var exists, otherwise do nothing.

2
  • what happened with @CDub's answer? Commented Jan 29, 2014 at 0:40
  • I removed it due to the fact that if some_var was not defined, it would throw an error. Commented Jan 29, 2014 at 15:00

2 Answers 2

3
other_var = some_var if defined? some_var
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is more easily to understand
1

Even shorter!

defined?(some_var) && other_var = some_var

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.