From: Johann Hibschman Date: 2000-07-19T10:38:17-07:00 Subject: [ruby-talk:04131] Re: Function objects. was: What are you using Ruby for? Dave Thomas writes: > It looks like my previous reply to this may have been lost, so I'm > re-posting it. I got it via email and have been thinking about it. > One of the things about functions is that referencing one in a pure OO > language requires that you at some point bind it to an object. A > function on its own means nothing. Take the humble '+' operator, for > instance. Send it to a number, and you get addition; send it to a > string, concatenation. This does seem a bit weird to me, though. A function is a function is a function. It's a mapping from a domain space to a range space. As such it's as real as anything. The weirdness arises for me because most languages have some syntactical sugar to make these nicer to deal with; in Ruby it looks like I'm best off explicitly declaring them as a class and using them that way. > Then the code might look something like: > calc = Calculator.new > register_function("f1", calc.method(:f1), "computer it") > register_function("g1", calc.method(:g1), "computer other") This doesn't help me generate new functions from old ones, like the variable-changes I did earlier with g_from_f. It does encapsulate the functions well, but at the cost of the slight strangeness of using a class simply to provide a namespace. In any case, this is wandering far astray from the original point. I think the conclusion is that I can't do things quite as simply as I can in languages where functions are first-class objects, which is not surprising. This isn't necessarily a bad thing, since some of the sneaky tricks used to do that may be very hard to read. (Like just about any Scheme program that uses call-with-current-continuation.) --J -- Johann Hibschman johann@physics.berkeley.edu