Optional return values

Discussion in 'Ruby' started by Andreas Schwarz, Jul 18, 2007.

  1. Hi,

    I have a few methods that check permissions, e.g.

    unless user.can_view?(object)
    puts "go away"
    end

    What I want do do now is add the possibility to give the caller more
    information why the user doesn't have permission. I want to make it
    backwards compatible, so returning [false, NotInGroupError:)group =>
    'xyz')] is not an option. The only thing I could think of is giving the
    function a String or Array that is modified in place:

    def can_view?(object, message='')
    if foo.bar
    message.replace('some error message')
    false
    else
    true
    end
    end

    That's how one would have to do it in C or VHDL. It does seem kinda
    un-rubyish though. Is there some obvious way to do this better that I'm
    missing?

    Thanks
    Andreas

    --
    Posted via http://www.ruby-forum.com/.
    Andreas Schwarz, Jul 18, 2007
    #1
    1. Advertising

  2. Alle mercoled=C3=AC 18 luglio 2007, Andreas Schwarz ha scritto:
    > Hi,
    >
    > I have a few methods that check permissions, e.g.
    >
    > unless user.can_view?(object)
    > puts "go away"
    > end
    >
    > What I want do do now is add the possibility to give the caller more
    > information why the user doesn't have permission. I want to make it
    > backwards compatible, so returning [false, NotInGroupError:)group =3D>
    > 'xyz')] is not an option. The only thing I could think of is giving the
    > function a String or Array that is modified in place:
    >
    > def can_view?(object, message=3D'')
    > if foo.bar
    > message.replace('some error message')
    > false
    > else
    > true
    > end
    > end
    >
    > That's how one would have to do it in C or VHDL. It does seem kinda
    > un-rubyish though. Is there some obvious way to do this better that I'm
    > missing?
    >
    > Thanks
    > Andreas


    I'do this:

    def can_view?(object, more_info =3D false)
    if foo.bar
    more_info ? [false, 'message'] : false
    else
    more_info ? [true, nil] : true
    end
    end

    If the user doesn't specify the second argument, then the method works as t=
    he=20
    original version, otherwise it returns an array whose first argument is tru=
    e=20
    or false and whose second argument is nil if the user has the perimission o=
    r=20
    the reason he doesn't if he doesn't (in the example above, I used a string,=
    =20
    but it can be anything, of course). The only drawback I can see with this=20
    approach is that you can simply test the condition using

    if can_view?(obj, true)
    #...
    but you need

    if can_view?(obj,true)[0]
    #...

    I hope this helps

    Stefano
    Stefano Crocco, Jul 18, 2007
    #2
    1. Advertising

  3. Andreas Schwarz wrote:
    > Hi,
    >
    > I have a few methods that check permissions, e.g.
    >
    > unless user.can_view?(object)
    > puts "go away"
    > end
    >
    > What I want do do now is add the possibility to give the caller more
    > information why the user doesn't have permission. I want to make it
    > backwards compatible, so returning [false, NotInGroupError:)group =>
    > 'xyz')] is not an option.


    Maybe turn it into a block?

    user.cannot_view?(object) do |msg|
    puts msg
    end

    Daniel
    Daniel DeLorme, Jul 18, 2007
    #3
  4. 2007/7/18, Andreas Schwarz <>:
    > Hi,
    >
    > I have a few methods that check permissions, e.g.
    >
    > unless user.can_view?(object)
    > puts "go away"
    > end
    >
    > What I want do do now is add the possibility to give the caller more
    > information why the user doesn't have permission. I want to make it
    > backwards compatible, so returning [false, NotInGroupError:)group =>
    > 'xyz')] is not an option. The only thing I could think of is giving the
    > function a String or Array that is modified in place:


    I would not do that - that's plain awful for a language that has
    multiple return values.

    Some options I can think of that are better:

    - create another method returning the reason
    - use exceptions, i.e. do not check but simply do something and throw
    if the permission is not granted (typically methods dealing with the
    file system do it this way)
    - create another method returning a boolean and a reason, but do not
    use a question mark as last character in the identifier
    - invert the logic, i.e. def permission_denied? which returns the
    reason if forbidden and nil if granted (though I find this a bit
    hackish)

    I'd probably favor raising exceptions.

    Kind regards

    robert
    Robert Klemme, Jul 18, 2007
    #4
    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. Piers Chivers
    Replies:
    2
    Views:
    392
    Piers Chivers
    Mar 2, 2004
  2. Capstar
    Replies:
    1
    Views:
    384
    Capstar
    Aug 18, 2004
  3. Jeremy S
    Replies:
    6
    Views:
    679
    Gregory A. Beamer
    Jun 15, 2009
  4. DeMarcus
    Replies:
    24
    Views:
    766
    DeMarcus
    Jan 14, 2011
  5. ree32

    How to create optional values for CommandEventArgs

    ree32, Sep 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    145
    ree32
    Sep 2, 2004
Loading...

Share This Page