Jul 03, 2007 @ 07:49 am

The following is easy stuff but we tend to forget about it… probably because most of us are not used to this kind of behavior from a programming language.

Ruby gives the illusion that you can return more than one element from a method.

This is how you do it :

  1. def a_method_to_insult_innocent_people
  2.   error = compute_error
  3.   if error == :stupid
  4.     return false, "You made a stupid error"
  5.   elsif error == :ridiculous
  6.     return false, "You made a ridiculous error"
  7.   elsif error == :worst_of_all_time
  8.     return false, "You made the most idiot error in history. Way to go…"
  9.   else
  10.     return true, "You made no error, you are still an idiot"
  11.   end
  12. end
  13.  
  14. success, msg = a_method_to_insult_innocent_people
  15. do_something_with_success(success)
  16. destroy_hateful_words!(msg)

It’s important to note that even though it doesn’t look like it, only ONE thing is returned from this method. This thing is, you bet, an array.

In fact,

  1. return false, "you made a stupid error"

is equivalent to :

  1. return [false, "you made a stupid error"]

Ruby arranges things both on the sending and receiving end to make it look like you are returning more than one element.

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Posted under : short & sweet
7 Comments
MyAvatars 0.2

This is also how MATLAB handles multiple outputs from functions. Pretty swish.

Comment by : Chris
— July 4, 2007 @ 3:02 am

MyAvatars 0.2

That’s pretty cool. It’s a bit of a shame that you can’t do:
do_whatever if a_method_to_insult_innocent_people
I guess you could do a_method_to_insult_innocent_people.first instead, although that seems a little ugly.

Comment by : Alex
— July 4, 2007 @ 2:46 pm

MyAvatars 0.2

@Alex,

Good observation. It’s true that you have to be careful with statements like “do_whatever if a_method_to_insult_innocent_people” (since it will always return true).

I don’t dislike your solution (a_method_to_insult_innocent_people.first) but I think I would prefer : a_method_to_insult_innocent_people[0]. It may feel “less ruby” but it has the advantage of being clear about what the return value really is (an array). On the other hand, if you use “.first”, some could think that your method returned a custom object that contains a method named “first”. Anyway, just a personal taste thing :)

Thanks for your comment

Comment by : Frank
— July 5, 2007 @ 9:24 am

MyAvatars 0.2

There’s another reason that Alex’s solution might be less than idea. If you’re returning multiple values but only using one then you’re probably better off with a different method.

Comment by : RSL
— July 5, 2007 @ 4:13 pm

MyAvatars 0.2

You are right RSL, although it was a convuluted example. A real world example of usage might be:
@insult_result = a_method_to_insult_innocent_people

In which case both values are used.

To be honest though, returning a hash would probably be nicer for everyone involved :).

Alex

Comment by : Alex
— July 5, 2007 @ 4:37 pm

MyAvatars 0.2

Sorry contrived, not convuluted (spelt wrong anyway).

Alex

Comment by : Alex
— July 5, 2007 @ 4:38 pm

MyAvatars 0.2

[…] Returning multiple values from a method […]





Leave a comment
Name (required)
Email (will not be publish) (required)
Website