You can return multiple values on a method using comma-separated values when you return the data.
Check the example below
def multiple_values
...
For further actions, you may consider blocking this person and/or reporting abuse
That is a very neat explanation, thanks for sharing!
I see your post is centered on the how and not on the why.
One of the most common errors in ruby is:
NoMethodError: undefined method 'foo' for nil:NilClass
It comes from calling a method on the result of a function that we forgot could return
nil
.Usually, when I feel tempted to return mutiple or complex values, I tend to:
A) Return a hash than documents what they are:
B) Return an object with a NullObject pattern to avoid
nil.something
down the line.I know the example is a bit contrived due to brevity, but I hope it lets my point though.
What do you think?
P.S.: Sandi Metz on this matters
Yes, you're right
NoMethodError: undefined method 'foo' for nil:NilClass
this a very common error in ruby and I understand your point of view. Thanks for sharing your opinions! 😁Thanks, I was looking for how to return a tuple.
Tip - you can unpack all the variables in one line.