From: Lyle Johnson Date: 2001-03-23T23:40:02+09:00 Subject: [ruby-talk:13138] How would Ruby say this? One group of C++ functions I'm wrapping for FXRuby have signatures like this: bool getValue(double& result, const char* prompt); and in C++ you would call them like so: double result = 42.0; if (getValue(result, "What's the frequency, Kenneth?") == true) { printf("The user responds: %g\n", result); } else { printf("The user cancelled without responding.\n"); } That is to say, it stores the primary return value in the "result" argument (which is often done with C++ pointers or references) but it also returns a boolean status code to indicate whether the operation succeeded or not. The first obvious option for exposing this function in Ruby is to return both values as a two-element array, e.g. initial = 42.0 status, result = getValue(initial, "What it is?") if status puts "The user responds: #{result}" else puts "The user cancelled without responding" end But this is awkward. Another option I'm considering is to have the function raise an exception for the "false" return status: begin initial = 42.0 result = getValue(initial, "What it is?") puts "The user responds: #{result}" rescue CancelError puts "The user cancelled without responding" end This seems a little cleaner to me but I'm still interested in hearing what the "Ruby" way is to handle this. A lot of the standard library classes return "nil" on failure (e.g. attempting to access a non-existent array index or hash key); is that the preferred way of handling this? Any comments or suggestions appreciated, Lyle