segmented method names

Discussion in 'Ruby' started by Jan Hegewald, May 8, 2008.

  1. Jan Hegewald

    Jan Hegewald Guest

    [Note: parts of this message were removed to make it a legal post.]

    Dear all,
    in Objective-C it is possible to split a method name into several
    segments to be able to put a meaningful description before each
    argument, like
    (BOOL)containsPointX:(int)x Y:(int)y

    is there a ruby way to achieve something similar?

    Many TIA,
    -- Jan
     
    Jan Hegewald, May 8, 2008
    #1
    1. Advertising

  2. A solution can be:

    #function
    def contains_point(opts = { :x=>nil, :y=>nil })

    end

    # call
    contains_point :x=>value, :y=>value





    On Thu, May 8, 2008 at 11:56 AM, Jan Hegewald <-bs.de> wrote:
    > Dear all,
    > in Objective-C it is possible to split a method name into several segments
    > to be able to put a meaningful description before each argument, like
    > (BOOL)containsPointX:(int)x Y:(int)y
    >
    > is there a ruby way to achieve something similar?
    >
    > Many TIA,
    > -- Jan
    >
    >
    >




    --
    Go outside! The graphics are amazing!
     
    Sandro Paganotti, May 8, 2008
    #2
    1. Advertising

  3. On Thu, May 8, 2008 at 8:32 AM, Sandro Paganotti
    <> wrote:
    > On Thu, May 8, 2008 at 11:56 AM, Jan Hegewald <-bs.de> wrote:
    > > in Objective-C it is possible to split a method name into several segments
    > > to be able to put a meaningful description before each argument, like
    > > (BOOL)containsPointX:(int)x Y:(int)y
    > >
    > > is there a ruby way to achieve something similar?> A solution can be:

    >
    > #function
    > def contains_point(opts = { :x=>nil, :y=>nil })
    >
    > end
    >
    > # call
    > contains_point :x=>value, :y=>value


    And in Ruby 1.9 you can also use the new hash literal syntax in the
    call so it becomes:

    contains_point x: value y: value

    Apple's RubyCocoa, which is implementing Ruby on the Objective-C
    runtime actually turns this into an objective-c call.

    However, the semantics of Ruby keyword/hash option parameters are
    slightly different from the Smalltalk inspired Objective-C method
    selectors. In Smalltalk/ObjectiveC x:y: is a different message and
    will find a different method than y:x:, whereas Ruby keyword/hash
    parameters are order independent and aren't involved in resolving a
    message to a method.

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, May 8, 2008
    #3
  4. Hi,

    At Thu, 8 May 2008 21:49:06 +0900,
    Rick DeNatale wrote in [ruby-talk:301103]:
    > And in Ruby 1.9 you can also use the new hash literal syntax in the
    > call so it becomes:
    >
    > contains_point x: value y: value


    You forgot a comma between first value and y:.

    > However, the semantics of Ruby keyword/hash option parameters are
    > slightly different from the Smalltalk inspired Objective-C method
    > selectors. In Smalltalk/ObjectiveC x:y: is a different message and
    > will find a different method than y:x:, whereas Ruby keyword/hash
    > parameters are order independent and aren't involved in resolving a
    > message to a method.


    And the same selector can duplicate, x:x:x: is valid and
    differs from x:, but it's impossible with a hash.

    --
    Nobu Nakada
     
    Nobuyoshi Nakada, May 9, 2008
    #4
  5. Jan Hegewald

    Jan Hegewald Guest

    Hi,

    thank you for all the suggestions. The Hash solution seem to be a =20
    feasible workaround. I=B4ll try that. To bad there is no "real" way to =20=

    split the name in ruby...


    Cheers,
    -- Jan
     
    Jan Hegewald, May 9, 2008
    #5
  6. On Thu, May 8, 2008 at 11:31 PM, Nobuyoshi Nakada <> wrote:
    > Hi,
    >
    > At Thu, 8 May 2008 21:49:06 +0900,
    > Rick DeNatale wrote in [ruby-talk:301103]:
    >> And in Ruby 1.9 you can also use the new hash literal syntax in the
    >> call so it becomes:
    >>
    >> contains_point x: value y: value

    >
    > You forgot a comma between first value and y:.


    You are correct of course, thanks.

    >> However, the semantics of Ruby keyword/hash option parameters are
    >> slightly different from the Smalltalk inspired Objective-C method
    >> selectors. In Smalltalk/ObjectiveC x:y: is a different message and
    >> will find a different method than y:x:, whereas Ruby keyword/hash
    >> parameters are order independent and aren't involved in resolving a
    >> message to a method.

    >
    > And the same selector can duplicate, x:x:x: is valid and
    > differs from x:, but it's impossible with a hash.


    Good point.

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, May 9, 2008
    #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. Paddy McCarthy
    Replies:
    3
    Views:
    741
    Anthony J Bybell
    Sep 24, 2004
  2. John Lenton
    Replies:
    0
    Views:
    387
    John Lenton
    Jan 11, 2005
  3. Replies:
    0
    Views:
    314
  4. Greg Hauptmann
    Replies:
    1
    Views:
    128
    Yossef Mendelssohn
    Nov 6, 2008
  5. Replies:
    0
    Views:
    171
Loading...

Share This Page