Professional Web Applications Themes

cascading message sends - Ruby

In Smalltalk, which Ruby is much like in some ways, you can 'cascade' message sends. That is, call a bunch of methods on the same object without naming it explicitly over and over. Eg: aCollection add: 5; add: 7; add: 'foo' ! calls 'add' on 'aCollection' 3 times, sending 5, 7, and 'foo'. Does Ruby have this syntax? cheers:) PMG -- PS: yes, you can fake it, if you make the method return 'self': you just chain the methods together. In Smalltalk: ((aCollection add: 5) add: 7) add: 'foo' ! But the ';' operator is needed for the case when ...

  1. #1

    Default cascading message sends

    In Smalltalk, which Ruby is much like in some ways, you can 'cascade'
    message sends. That is, call a bunch of methods on the same object
    without naming it explicitly over and over. Eg:

    aCollection add: 5; add: 7; add: 'foo' !

    calls 'add' on 'aCollection' 3 times, sending 5, 7, and 'foo'.

    Does Ruby have this syntax?

    cheers:)
    PMG

    --

    PS: yes, you can fake it, if you make the method return 'self': you
    just chain the methods together. In Smalltalk:

    ((aCollection add: 5) add: 7) add: 'foo' !

    But the ';' operator is needed for the case when the method returns
    something other than 'self' - eg for instance, the object just added.
    In which case clearly this 'cheat' doesn't work!
    Paul Guest

  2. #2

    Default Re: cascading message sends

    Paul MG wrote: 

    Here's one option, not as elegant:

    x=[]
    x.instance_eval do
    push 1
    push 2
    push 3
    end
    p x

    Inside the do...end block, "self" is x.



    Joel Guest

  3. #3

    Default Re: cascading message sends

    Joel VanderWerf wrote:
     

    Note that you can wrap this into a nicer looking syntax, similar to
    JavaScript:

    module Kernel
    def with(o, &blk)
    o.instance_eval &blk
    end
    end

    x = []

    with x do
    push 1
    push 2
    push 3
    end

    p x

    I'm not sure if you can get something that looks like Smalltalk. I'll
    think about it
    and see what I can come up with (unless someone beats me to it).

    - Dan


    Dan Guest

  4. #4

    Default Re: cascading message sends

    On Sat, Jan 31, 2004 at 11:16:50AM -0800, Paul MG wrote: 

    Others have mentioned a couple ways to do this generally, but
    I just wanted to point out that you can do this without any
    special syntax as long as the methods involved return `self' -
    which many do, including Array#push:

    aCollection = []
    aCollection.push(5).push(7).push('foo')
    [5, 7, "foo"]


    Array#<< does, too, and long chains with it have less
    syntactic clutter:


    aCollection << 4 << 3 << 2
    [5, 7, "foo", 4, 3, 2]



    -Mark

    Mark Guest

  5. #5

    Default Re: cascading message sends

    What about this:

    [gusgusmac tmp]$ cat chain.rb
    #! /usr/bin/ruby -w

    class Chain
    def initialize(o)
    o = o
    end

    def method_missing(msg, *args)
    o.send(msg, *args)
    o
    end
    end

    class Object
    def chain
    Chain.new(self)
    end
    end

    a = [0, 1, 2, 3]
    a.chain.delete_at(2).delete_at(1)
    p a

    [gusgusmac tmp]$ ruby chain.rb
    [0, 3]

    Guillaume.

    Le 31 janv. 04, à 16:19, Mark J. Reed a écrit :
     
    >
    > Others have mentioned a couple ways to do this generally, but
    > I just wanted to point out that you can do this without any
    > special syntax as long as the methods involved return `self' -
    > which many do, including Array#push:
    >
    > aCollection = []
    > aCollection.push(5).push(7).push('foo')
    > [5, 7, "foo"]
    >
    >
    > Array#<< does, too, and long chains with it have less
    > syntactic clutter:
    >
    >
    > aCollection << 4 << 3 << 2
    > [5, 7, "foo", 4, 3, 2]
    >
    >
    >
    > -Mark
    >
    >[/ref]




    Guillaume Guest

  6. #6

    Default Re: cascading message sends


    On Jan 31, 2004, at 14:11, Guillaume Marcais wrote:
     

    Nice. A little correction below:
     
    self

    As an o's method you call may not return o itself (like bang methods).
    And this way you can have as long a chain as you need.
     
    >>
    >> Others have mentioned a couple ways to do this generally, but
    >> I just wanted to point out that you can do this without any
    >> special syntax as long as the methods involved return `self' -
    >> which many do, including Array#push:
    >>
    >> aCollection = []
    >> aCollection.push(5).push(7).push('foo')
    >> [5, 7, "foo"]
    >>
    >>
    >> Array#<< does, too, and long chains with it have less
    >> syntactic clutter:
    >>
    >>
    >> aCollection << 4 << 3 << 2
    >> [5, 7, "foo", 4, 3, 2]
    >>
    >>
    >>
    >> -Mark
    >>
    >>[/ref]
    >
    >
    >[/ref]

    Sincerely,
    Gennady Bystritsky





    Gennady Guest

  7. #7

    Default Re: cascading message sends

    "Mark J. Reed" <com> wrote in message news:<thereeds.org>...
     

    It's lucky that in Ruby Array#push and Array#<< do return self; unlike
    Smalltalk's Collection>>add: anItem which returns anItem (I believe).

    However as I said in the PS to my original post, I was curious about
    what can be done in Ruby in the general case.

    Aside: The things people have been able to do in suggesting ways to
    get this behaviour have really illustrated to me the power of the
    language: there seems to be something almost LISP-like in the way you
    can extend nearly everything about the language, eg adding methods to
    Object, Kernel, etc. Very impressive. I think Python is cute (and i
    have to admit to preferring syntactically-significant-indentation to
    ruby's "end" keyword) but Ruby seems to clearly trounce it for sheer
    power.

    Thanks for everyone's thoughts!

    cheers:)
    Paul
    Paul Guest

Similar Threads

  1. Cascading
    By nk_noorkhan in forum Macromedia ColdFusion
    Replies: 0
    Last Post: May 27th, 11:56 AM
  2. Cascading menus
    By Michael in forum Web Design
    Replies: 1
    Last Post: December 18th, 02:21 AM
  3. Cascading repeaters
    By Mikael Engdahl in forum ASP.NET General
    Replies: 1
    Last Post: August 8th, 03:40 PM
  4. cascading menues with asp.net
    By Yavuz Bogazci in forum ASP.NET General
    Replies: 1
    Last Post: July 25th, 01:47 PM
  5. Looking for inspiration: cascading CGI
    By Purl Gurl in forum PERL Miscellaneous
    Replies: 18
    Last Post: June 30th, 02:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139