KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
But goodness alone is never enough. A hard cold wisdom is required, too, for goodness to accomplish good. Goodness without... – Robert Heinlein
← WHEDONesque: Firefly theme based song.Neat comparison of Jython and Rhino, with lots of sample code →

Daily link icon Thursday, July 14, 2005

How do you take a reference to a function in Ruby and Groovy?

Ruby question: In Ruby, you can call no arg methods with no parentheses, and you need special syntax to take a reference to the method. What's that syntax? I've looked all over and can't find it. Same goes for Groovy.

← WHEDONesque: Firefly theme based song.Neat comparison of Jython and Rhino, with lots of sample code →

Comments XML gif

quackeroo wrote:

Can you provide pseudo code for clarification? Not sure what you are wanting.

∴ quackeroo | 14-Jul-2005 8:51pm est | #7889

quackeroo wrote:

I'm assuming you want a function pointer. In Ruby this is accomplished with the method method and the call method. From the PickAxe 2nd ed., page 543:


def square(n)
  n*n
end

meth = self.method(:square)

meth.call(9)            -> 81

Not sure how to do this in Groovy.

∴ quackeroo | 14-Jul-2005 9:10pm est | #7890

Keith (http://keithdevens.com/) wrote:

quackeroo, thanks, that might be it. What I really want to know how to do is something like:

def foo()
  print "whatever"
end

bar(&foo)

Where bar would normally expect a block or a Proc object. Does this make sense?

Keith | 16-Jul-2005 2:41pm est | http://keithdevens.com/ | #7899

quackeroo wrote:

If bar's definition uses the & for the last parameter, then that parameter will take a block and turn it into a Proc object:

def foo()
  print "whatever"
end

def bar(&block)
  block.call()
end

Since you need to pass a block to bar, I suppose you could pass a block with foo in it like so:

bar { foo }

# which should give the same as
bar { print "whatever" }
∴ quackeroo | 17-Jul-2005 5:37am est | #7905

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 8 posts

Recent comments XML

new⇒URL design

http://groups.google.com/group/cool​ndex/web/asian-girl-sucking-to-blac​k-man...

derek: Oct 12, 12:13pm

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.169s.

(Used 8 db queries)

mobile phone