Idiomatic ruby

Discussion in 'Ruby' started by eastcoastcoder@gmail.com, Feb 12, 2006.

  1. Guest

    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?
     
    , Feb 12, 2006
    #1
    1. Advertising

  2. Phrogz Guest

    How about returning multiple values?

    def valid?
    [ @is_valid_flag, @error_message ]
    end
     
    Phrogz, Feb 12, 2006
    #2
    1. Advertising

  3. Alex Fenton Guest

    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
     
    Alex Fenton, Feb 13, 2006
    #3
  4. 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
     
    David Vallner, Feb 13, 2006
    #4
  5. ------=_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--
     
    Brian Buckley, Feb 13, 2006
    #5
  6. 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
     
    Robert Klemme, Feb 13, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iwan van der Kleyn
    Replies:
    5
    Views:
    166
    James Edward Gray II
    Nov 23, 2004
  2. Zed A. Shaw
    Replies:
    2
    Views:
    114
    Zed A. Shaw
    Apr 23, 2005
  3. Brock Weaver

    Idiomatic ruby version of this code?

    Brock Weaver, Aug 18, 2005, in forum: Ruby
    Replies:
    16
    Views:
    191
    pat eyler
    Aug 23, 2005
  4. Idiomatic ruby

    , Feb 13, 2006, in forum: Ruby
    Replies:
    4
    Views:
    166
    Adam P. Jenkins
    Feb 13, 2006
  5. Replies:
    1
    Views:
    136
    Robert Klemme
    Feb 21, 2006
Loading...

Share This Page