As someone who came from Java background and being a newbie to Ruby,
I was wondering if there is a simple way of doing this with ruby.
new_values = foo(bar)
if new_values
if arr
arr << new_values
else
arr = new_values
end
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming “arr” is either an array or nil, I would use:
If you’re doing this in a loop or some such, there might be more idiomatic ways to do it. For example, if you’re iterating a list, passing each value to
foo(), and constructing an array of results, you could just use: