correct terminology for a function that yields

Discussion in 'Ruby' started by Joe Van Dyk, Dec 23, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    def foo
    yield
    end

    foo do
    puts "hey"
    end


    What's the correct description of what is going on there? I've got a
    function foo. No arguments, right? Does it "take a block"? I know
    it gives control to the block, but I'm not sure of the correct
    terminology to use.

    Joe
    Joe Van Dyk, Dec 23, 2005
    #1
    1. Advertising

  2. --TRYliJ5NKNqkz5bu
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Fri, Dec 23, 2005 at 09:00:31AM +0900, Joe Van Dyk wrote:
    > def foo
    > yield
    > end
    >=20
    > foo do
    > puts "hey"
    > end
    >=20
    >=20
    > What's the correct description of what is going on there? I've got a
    > function foo. No arguments, right? Does it "take a block"? I know
    > it gives control to the block, but I'm not sure of the correct
    > terminology to use.


    It does indeed "take a block". The block argument is implicit. You
    could make it explicit if you wanted to:

    def foo(&blk)
    blk.call
    end

    In more general terms, any function that takes another function as an
    argument (or returns one as a result) is known as a higher-order
    function.

    regards,
    Ed

    --TRYliJ5NKNqkz5bu
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDqz73nhUz11p9MSARAsHCAJ0TkLQhz3t05LjsWtHfIpsHoUQF4QCg2s7L
    JvuJJgWUn4q8urPjAcOftQc=
    =ItPk
    -----END PGP SIGNATURE-----

    --TRYliJ5NKNqkz5bu--
    Edward Faulkner, Dec 23, 2005
    #2
    1. Advertising

  3. Joe Van Dyk

    Joe Van Dyk Guest

    On 12/22/05, Edward Faulkner <> wrote:
    > On Fri, Dec 23, 2005 at 09:00:31AM +0900, Joe Van Dyk wrote:
    > > def foo
    > > yield
    > > end
    > >
    > > foo do
    > > puts "hey"
    > > end
    > >
    > >
    > > What's the correct description of what is going on there? I've got a
    > > function foo. No arguments, right? Does it "take a block"? I know
    > > it gives control to the block, but I'm not sure of the correct
    > > terminology to use.

    >
    > It does indeed "take a block". The block argument is implicit. You
    > could make it explicit if you wanted to:
    >
    > def foo(&blk)
    > blk.call
    > end
    >
    > In more general terms, any function that takes another function as an
    > argument (or returns one as a result) is known as a higher-order
    > function.


    Ok, thanks. I'm writing documentation for a domain-specific language
    (in Ruby) that I created. People who use it are probably programmers,
    but may not be experienced with Ruby, so I wanted to explain a little
    bit about what's going on behind the scenes.
    Joe Van Dyk, Dec 23, 2005
    #3
  4. Joe Van Dyk wrote:
    > On 12/22/05, Edward Faulkner <> wrote:
    >> On Fri, Dec 23, 2005 at 09:00:31AM +0900, Joe Van Dyk wrote:
    >>> def foo
    >>> yield
    >>> end
    >>>
    >>> foo do
    >>> puts "hey"
    >>> end
    >>>
    >>>
    >>> What's the correct description of what is going on there? I've got
    >>> a function foo. No arguments, right? Does it "take a block"? I
    >>> know it gives control to the block, but I'm not sure of the correct
    >>> terminology to use.

    >>
    >> It does indeed "take a block". The block argument is implicit. You
    >> could make it explicit if you wanted to:
    >>
    >> def foo(&blk)
    >> blk.call
    >> end


    Note, that this has some performance implications though.

    >> In more general terms, any function that takes another function as an
    >> argument (or returns one as a result) is known as a higher-order
    >> function.

    >
    > Ok, thanks. I'm writing documentation for a domain-specific language
    > (in Ruby) that I created. People who use it are probably programmers,
    > but may not be experienced with Ruby, so I wanted to explain a little
    > bit about what's going on behind the scenes.


    You can as well call the block "anonymous function" or "anonymous
    callback" IMHO. What I like about the "callback" variant is that it
    precisely describes what's happening here: the caller provides a function
    as hook that is called by the method.

    Kind regards

    robert
    Robert Klemme, Dec 23, 2005
    #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. joe
    Replies:
    0
    Views:
    423
  2. Anders Borum
    Replies:
    0
    Views:
    3,433
    Anders Borum
    Jun 9, 2004
  3. TP
    Replies:
    1
    Views:
    327
    Albert Hopkins
    Jan 13, 2009
  4. Paul
    Replies:
    63
    Views:
    1,248
  5. x1
    Replies:
    11
    Views:
    263
Loading...

Share This Page