Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > correct terminology for a function that yields

Reply
Thread Tools

correct terminology for a function that yields

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      12-23-2005
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


 
Reply With Quote
 
 
 
 
Edward Faulkner
Guest
Posts: n/a
 
      12-23-2005
--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)

iD8DBQFDqz73nhUz11p9MSARAsHCAJ0TkLQhz3t05LjsWtHfIp sHoUQF4QCg2s7L
JvuJJgWUn4q8urPjAcOftQc=
=ItPk
-----END PGP SIGNATURE-----

--TRYliJ5NKNqkz5bu--


 
Reply With Quote
 
 
 
 
Joe Van Dyk
Guest
Posts: n/a
 
      12-23-2005
On 12/22/05, Edward Faulkner <(E-Mail Removed)> 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.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-23-2005
Joe Van Dyk wrote:
> On 12/22/05, Edward Faulkner <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Re: well-defined terminology versus generally accepted terminology regarding pointers and arrays Paul C++ 63 04-24-2011 12:42 PM
exec in a nested function yields an error TP Python 1 01-13-2009 03:37 PM
Correct Terminology Another Computer Support 13 02-29-2008 02:44 AM
Problem with method that starts process, yields pid then yields return code x1 Ruby 11 12-06-2005 01:30 AM



Advertisments