`defn`: defining functions inside of modules

23 views
Skip to first unread message

Brian Marick

unread,
Jan 12, 2012, 8:31:31 PM1/12/12
to rubyf...@googlegroups.com
My thinking about classes and modules these days is that they're organization and documentation tools. I want to program using hashes instead of custom objects. But I also want to say things like "That hash there? It's "shaped" like a Zipper, so there are certain functions that you should know about if you want to work with it." You can find those functions in the ZipperShaped module.

A problem with that is that Ruby modules are all about providing constants and methods to the classes that include them. But I want real functions (lambdas), not methods.

The `defn` method within Stunted lets you add functions to modules in a way that mades them as usable as methods.

https://github.com/marick/stunted/wiki/Defn

-----
Brian Marick, Artisanal Labrador
Now working at http://path11.com
Contract programming in Ruby and Clojure
Occasional consulting on Agile


Steve Klabnik

unread,
Jan 17, 2012, 3:49:04 PM1/17/12
to rubyf...@googlegroups.com
> But I want real functions (lambdas), not methods.

Why are Proc-like objects a problem?

Robert D Pitts

unread,
Jan 17, 2012, 3:51:41 PM1/17/12
to rubyf...@googlegroups.com
I think it's the inconvenience/kludginess of unbinding the method, no?

Steve Klabnik

unread,
Jan 17, 2012, 3:58:48 PM1/17/12
to rubyf...@googlegroups.com
I guess what I mean is 'why is the binding a problem?'

Brian Marick

unread,
Jan 17, 2012, 5:07:48 PM1/17/12
to rubyf...@googlegroups.com

On Jan 12, 2012, at 7:31 PM, Brian Marick wrote:

> A problem with that is that Ruby modules are all about providing constants and methods to the classes that include them. But I want real functions (lambdas), not methods.


On Jan 17, 2012, at 2:49 PM, Steve Klabnik wrote:

> Why are Proc-like objects a problem?


On Jan 17, 2012, at 2:51 PM, Robert D Pitts wrote:

> I think it's the inconvenience/kludginess of unbinding the method, no?

On Jan 17, 2012, at 2:58 PM, Steve Klabnik wrote:

> I guess what I mean is 'why is the binding a problem?'

Not sure I understand these questions. Here's a reason I want `include MyModule` to give me lambdas, not just methods and constants. Suppose I want to provide a pile of lambdas to use in higher-order ways. Modules are my chosen (for now) way to group piles of things, but Ruby assumes those things are either constants or methods. So to make lambdas accessible via modules, I make methods that return those lambdas. `defn` is just a shorthand way of doing that. So, for example, I can do this:

ruby-1.9.2-p290 :006 > module M
ruby-1.9.2-p290 :007?> include Stunted::Defn
ruby-1.9.2-p290 :008?> defn :add, -> a, b { a + b }
ruby-1.9.2-p290 :009?> end

ruby-1.9.2-p290 :010 > include M

ruby-1.9.2-p290 :016 > add2 = add.curry.(2)
ruby-1.9.2-p290 :017 > add2.(3)
=> 5

Robert D Pitts

unread,
Jan 17, 2012, 6:26:24 PM1/17/12
to rubyf...@googlegroups.com
Right, and the point I was trying to make is that while this is achievable via method unbinding, using the defn method makes for a more pleasing/natural/unobtrusive way to do so.

Was just trying to answer Steve's question. Perhaps not well enough though ;)
Reply all
Reply to author
Forward
0 new messages