Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Idiomatic ruby

Reply
Thread Tools

Idiomatic ruby

 
 
eastcoastcoder@gmail.com
Guest
Posts: n/a
 
      02-12-2006
Very often I have a question method, which, in some cases, the caller
would want to know why as well.

Examples:

def valid?
end

def abort?
end

Ruby does not allow subclassing true and false, so, if these methods
return one of those, they can't return any additional info. But
sometimes the caller needs additional info, as in:

if !valid? logger.warn "Not valid: #{why not?}"

What is the best way to handle this? I could have those methods set
@instance_variables, but this seems a little hackish, and could
introduce race conditions.

Is there anyway to return false, "reason", or something of that sort?
What is the preferred, idiomatic way of doing this?

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      02-12-2006
How about returning multiple values?

def valid?
[ @is_valid_flag, @error_message ]
end

 
Reply With Quote
 
 
 
 
Alex Fenton
Guest
Posts: n/a
 
      02-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Ruby does not allow subclassing true and false, so, if these methods
> return one of those, they can't return any additional info. But
> sometimes the caller needs additional info, as in:
>
> if !valid? logger.warn "Not valid: #{why not?}"


Use exceptions, which can contain readable messages

def my_meth
validate
# .. proceed
rescue => err
logger.warn "Not valid #{err}"
end

# if you want a boolean-style method
def valid?
validate && true # assuming validate returns some kind of true value
rescue
false
end

alex
 
Reply With Quote
 
David Vallner
Guest
Posts: n/a
 
      02-13-2006
D=C5=88a Pondelok 13 Febru=C3=A1r 2006 01:03 Phrogz nap=C3=ADsal:
> How about returning multiple values?
>
> def valid?
> [ @is_valid_flag, @error_message ]
> end


Wouldn't ever evaluate as false, which means all the predicates would have =
to=20
be true on a failure, which might be a bit confusing - I prefer to code=20
assertively if possible.

David Vallner


 
Reply With Quote
 
Brian Buckley
Guest
Posts: n/a
 
      02-13-2006
------=_Part_19300_22369873.1139846608563
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

>
> What is the best way to handle this? I could have those methods set
> @instance_variables, but this seems a little hackish, and could
> introduce race conditions.
>


I'm not sure I'd agree that it is hackish to set @instance_variables (or
perhaps to use a setter instead) to capture learned info. A naming
convention might help (maybe remove the "?' and add "_reason")

class Foo
attr_accessor :valid_reason
def valid?
...
self.valid_reason =3D "age is too young"
false
end
end

------=_Part_19300_22369873.1139846608563--


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-13-2006
Brian Buckley wrote:
>> What is the best way to handle this? I could have those methods set
>> @instance_variables, but this seems a little hackish, and could
>> introduce race conditions.
>>

>
> I'm not sure I'd agree that it is hackish to set @instance_variables
> (or perhaps to use a setter instead) to capture learned info. A
> naming convention might help (maybe remove the "?' and add "_reason")
>
> class Foo
> attr_accessor :valid_reason
> def valid?
> ...
> self.valid_reason = "age is too young"
> false
> end
> end


IMHO it's bad practice to store this info in the instance. It really
doesn't belong there; you run into all sorts of problems (race conditions
in concurrent apps, consistence - you need to clear this when the instance
state changes...).

The best solution seems to be to use an exception.

class Foo
attr_accessor :name

def ensure_valid
self.name or raise "Empty name."
end
end

begin
f=Foo.new
f.ensure_valid
f.do_stuff()
rescue RuntimeError => e
$stderr.puts "Invalid because of: #{e.message}"
end

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Idiomatic Ruby: Enumerations listrecv@gmail.com Ruby 1 02-21-2006 09:46 AM
Idiomatic ruby eastcoastcoder@gmail.com Ruby 4 02-13-2006 08:53 PM
Idiomatic ruby version of this code? Brock Weaver Ruby 16 08-23-2005 07:45 PM
[ANN] Ruby/Odeum 0.2 (More Idiomatic, Better Source Layout) Zed A. Shaw Ruby 2 04-23-2005 05:40 PM
Typical/idiomatic examples of dynamic code generation with Ruby? Iwan van der Kleyn Ruby 5 11-23-2004 03:26 PM



Advertisments