[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03951] Re: Pluggable functions and blocks

From: Dave Thomas <Dave@...>
Date: 2000-07-12 13:31:47 UTC
List: ruby-talk #3951
Aleksi Niemel<aleksi.niemela@cinnober.com> writes:

>   class Foo
>     def Foo.foo
>       puts "foo"
>     end
>     def do_foo( foo = proc {Foo.foo} )
>       foo.call
>     end
>   end
> 
>   def my_foo
>     puts "bar"
>   end
> 
>   Foo.new.do_foo
>   Foo.new.do_foo( proc {my_foo} ) # I'd like to say do_foo( my_foo )
> 
> This seems to work, outputting 'foo\nbar\n' as expected. It's tedious
> anyway, and tediousness will turn into error-prone code later. The main
> problem is to have explicit proc wrappings over functions.

You could just associate a block with the call instead. That makes the 
call syntax a lot neater.

   class Foo
     def Foo.foo
       puts "foo"
     end
     def do_foo(&foo)
       foo = proc { Foo.foo } unless foo
       foo.call
     end
   end
 
   def my_foo
     puts "bar"
   end
 
   f = Foo.new
   f.do_foo
   f.do_foo  { my_foo }

I'll look through the rest later. All these foo's are hurting my eyes.


Dave


In This Thread

Prev Next