# Making a duck

**URL:** <https://rubytalk.org/t/making-a-duck/18817>\
**Category:** ruby-talk\
**Created:** [6 June 2005 16:00 UTC](https://rubytalk.org/t/making-a-duck/18817 "2005-06-06T16:00:23Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![gabriele\_renzi5](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gabriele\_renzi5](https://rubytalk.org/u/gabriele_renzi5)\
**Post date:** [7 June 2005 19:10 UTC](https://rubytalk.org/t/making-a-duck/18817/21 "2005-06-07T19:10:26Z")

</div>

Eric Mahurin ha scritto:

> > Eric Mahurin wrote:
> > 
> > > > At Tue, 7 Jun 2005 11:59:47 +0900,  
> > > > Eric Mahurin wrote in [ruby-talk:144750]:
> > > > 
> > > > > Is there an advantage to having a separate Behavior class
> > > > 
> > > > as
> > > > 
> > > > > opposed the solution I had: making a singleton Object
> > > > 
> > > > directly?
> > > > 
> > > > To allow sharing same behavior.
> > > 
> > > I'm not sure how much application this would have over the  
> > > conventional class definition approach.
> > > 
> > > At first I didn't think this would work because I thought
> > 
> > you
> > 
> > > wouldn't be able to create any instance variables using  
> > > define\_method. I thought any @ variables in a proc would
> > 
> > refer
> > 
> > > to the @ variable in the original context, but
> > 
> > define\_method
> > 
> > > apparently rebinds them to the object.
> > 
> > I always remind myself that the binding of "self" changes -  
> > even for each  
> > method invocation. This explains pretty good why this works  
> > as it should.
> 
> define\_method, instance\_eval, class\_eval, module\_eval, and  
> maybe others seem to have this special ability - rebind the  
> meaning of self (but not locals) for a Proc.

I think of self as a dynamic variable, outside of lexical scope  
and that makes me think..

> This brings us  
> back to the topic I talked about earlier - unbind/rebind procs.  
> It would be nice if we could do the same thing to a Proc that  
> these methods can do internally

... would dynamic variables be enough? Christian Neukirchen has a nifty implementation of them on his blog.

\<snip\>

> BTW, I don't see the value that class\_eval and module\_eval  
> provide over instance\_eval. classes and modules can be treated  
> like instances just like any other object.

if you define a method in a class\_eval/module\_eval block on object X ruby will define it in instances of X while if you use instance\_eval you'd define the method on the object X

> **···**
>
> > --- Robert Klemme \<bob.news@gmx.net\> wrote:
> > 
> > > > --- nobu.nokada@softhome.net wrote:

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [7 June 2005 18:11 UTC](https://rubytalk.org/t/making-a-duck/18817/22 "2005-06-07T18:11:18Z")

</div>

> \> define\_method, instance\_eval, class\_eval, module\_eval, and  
> \> maybe others seem to have this special ability - rebind the  
> \> meaning of self (but not locals) for a Proc. This brings  
> us  
> \> back to the topic I talked about earlier - unbind/rebind  
> procs.  
> \> It would be nice if we could do the same thing to a Proc  
> that  
> \> these methods can do internally:  
> \>  
> \> aProc.rebind\_self(obj) -\> aNewProc # rebind what self is  
> \>  
> \> With this, "obj.instance\_eval(&proc)" would be equivalent  
> to  
> \> "proc.rebind\_self(obj).call".
> 
> Why do you want rebind if the other approach is much simpler?
> 
> #instance\_eval \*always\* rebinds self (and only self).

because you can get a handle on that rebound Proc. You might  
want to pass it around or whatever.

> \> Other useful rebindings may be:  
> \>  
> \> aProc.rebind\_locals(binding) -\> aNewProc  
> \> aProc.rebind\_all(binding) -\> aNewProc  
> \> # replace local variables with their current values  
> \> aProc.unbind\_locals -\> aNewProc
> 
> When do you think will unbind\_locals be useful?

As a replacement for many string evals - which are ugly,  
inefficient, and possibly dangerous. Many (most?) times that  
you need to eval a string it is because you need to pull in  
some local variables to help define the string to be evaled.  
Here is the first example of a string eval in the 1.8 library I  
found:

for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LI OPTION tr th td ]  
&nbsp;&nbsp;&nbsp;&nbsp;methods += \<\<-BEGIN + nO\_element\_def(element) + \<\<-END  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{element.downcase}(attributes = {})  
&nbsp;&nbsp;&nbsp;&nbsp;BEGIN  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;END  
end  
eval(methods)

This could be replaced by:

for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LI OPTION tr th td ]  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method(element.downcase.to\_sym ,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc { |attributes={}|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nO\_element\_def(element)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.unbind\_locals # replace element with constant  
&nbsp;&nbsp;&nbsp;&nbsp;)  
end

Much cleaner, huh?

> A proc  
> typically needs some  
> of the variables bound. As for the rebindings, I would  
> prefer a general  
> mechanism to transfer state from one binding to another.  
> Then one could  
> implement all your rebind\* methods in terms of that general  
> mechanism plus  
> do more. Alternatively one could think about conversion  
> methods Binding \<-\>  
> Hash.

Transferring locals might be pretty easy, but transferring the  
meaning of self would be more difficult, I think. At least  
without making a new Binding (and then you'd still need a way  
to rebind it to the original proc).

> \> BTW, I don't see the value that class\_eval and module\_eval  
> \> provide over instance\_eval. classes and modules can be  
> treated  
> \> like instances just like any other object.
> 
> class\_eval and instance\_eval are not equivalent:
> 
> \>\> class Foo;end  
> =\> nil  
> \>\> Foo.class\_eval do  
> ?\> def bar() "bar" end  
> \>\> end  
> =\> nil  
> \>\> Foo.new.bar  
> =\> "bar"  
> \>\> Foo.instance\_eval do  
> ?\> def bax() "bax" end  
> \>\> end  
> =\> nil  
> \>\> Foo.new.bax  
> NoMethodError: undefined method `bax' for #\<Foo:0x10179ee0\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):12

Interesting. I assumed that since  
class\_eval/instance\_eval/module\_eval all returned the same  
"self" that they did the same thing. The only difference I see  
is in "def \<method\> ...". With class\_eval, it defines instance  
methods and with instance\_eval, it defines class methods.  
That's kind of strange. Some kind of magic is going on here  
other than the changing of self.

I was hoping that instance\_eval could be used to define class  
methods using #define\_method, but #define\_method does the same  
with both - defines instance methods. Anybody know of an  
equivalent to #define\_method for making class methods? I  
couldn't figure out any way to define a class method from a  
proc - just out of curiosity.

> **···**
>
> --- Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do you Yahoo!?  
> Yahoo! Mail - Helps protect you from nasty viruses.  
> [http://promotions.yahoo.com/new\_mail](http://promotions.yahoo.com/new_mail)

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [7 June 2005 20:09 UTC](https://rubytalk.org/t/making-a-duck/18817/23 "2005-06-07T20:09:24Z")

</div>

From what I saw, this didn't seem much different than another  
space for global variables. The main ability I was wanting was  
to be able to replace local variables with their current value  
in a proc. I think this would make the ugly \*eval(string)  
methods rarely needed. I just looked through the stdlib and  
found almost every occurence of \*eval(string) and found almost  
every one looks kind of like this:

name = ...  
var = ...  
eval("def #{name} .... #{var} .... end")

This would work:

name = ...  
var = ...  
define\_method(name.to\_sym,proc{.... var ....}.unbind)

assuming that Proc#unbind replaced "var" with its current  
value.

A define\_class\_method method would also be useful. Otherwise  
there would be no equivalent to the above when doing eval("def  
self.#{name} .... end").

> **···**
>
> --- gabriele renzi \<surrender\_it@remove-yahoo.it\> wrote:
> 
> > Eric Mahurin ha scritto:  
> > \> This brings us  
> > \> back to the topic I talked about earlier - unbind/rebind  
> > procs.  
> > \> It would be nice if we could do the same thing to a Proc  
> > that  
> > \> these methods can do internally
> > 
> > ... would dynamic variables be enough? Christian Neukirchen  
> > has a nifty  
> > implementation of them on his blog.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Discover Yahoo!  
> Find restaurants, movies, travel and more fun for the weekend. Check it out!  
> [http://discover.yahoo.com/weekend.html](http://discover.yahoo.com/weekend.html)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [8 June 2005 07:50 UTC](https://rubytalk.org/t/making-a-duck/18817/24 "2005-06-08T07:50:28Z")

</div>

Eric Mahurin wrote:

> > > define\_method, instance\_eval, class\_eval, module\_eval, and  
> > > maybe others seem to have this special ability - rebind the  
> > > meaning of self (but not locals) for a Proc. This brings us  
> > > back to the topic I talked about earlier - unbind/rebind procs.  
> > > It would be nice if we could do the same thing to a Proc that  
> > > these methods can do internally:
> > > 
> > > aProc.rebind\_self(obj) -\> aNewProc # rebind what self is
> > > 
> > > With this, "obj.instance\_eval(&proc)" would be equivalent to  
> > > "proc.rebind\_self(obj).call".
> > 
> > Why do you want rebind if the other approach is much simpler?
> > 
> > #instance\_eval \*always\* rebinds self (and only self).
> 
> because you can get a handle on that rebound Proc. You might  
> want to pass it around or whatever.

I see. Although I don't have a use case for this at hand and in fact  
never missed that. But that might be just my personal experience.

> > > Other useful rebindings may be:
> > > 
> > > aProc.rebind\_locals(binding) -\> aNewProc  
> > > aProc.rebind\_all(binding) -\> aNewProc  
> > > # replace local variables with their current values  
> > > aProc.unbind\_locals -\> aNewProc
> > 
> > When do you think will unbind\_locals be useful?
> 
> As a replacement for many string evals - which are ugly,  
> inefficient, and possibly dangerous. Many (most?) times that  
> you need to eval a string it is because you need to pull in  
> some local variables to help define the string to be evaled.  
> Here is the first example of a string eval in the 1.8 library I  
> found:
> 
> for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LI OPTION tr th td ]  
> &nbsp;&nbsp;&nbsp;&nbsp;methods += \<\<-BEGIN + nO\_element\_def(element) + \<\<-END  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{element.downcase}(attributes = {})  
> &nbsp;&nbsp;&nbsp;&nbsp;BEGIN  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;END  
> end  
> eval(methods)
> 
> This could be replaced by:
> 
> for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LI OPTION tr th td ]  
> &nbsp;&nbsp;&nbsp;&nbsp;define\_method(element.downcase.to\_sym ,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc { |attributes={}|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nO\_element\_def(element)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.unbind\_locals # replace element with constant  
> &nbsp;&nbsp;&nbsp;&nbsp;)  
> end
> 
> Much cleaner, huh?

Not really (at least to my eyes). Also, there are some issues:

- I don't know what nO\_element\_def does exactly, but it will have to be  
rewritten to not create a string with ruby code

- Your version might be less efficient.

- There might be subtle differences because "element" is pulled into the  
closure

- Also, unbind\_locals will remove "element" from the procs bindings

> > A proc  
> > typically needs some  
> > of the variables bound. As for the rebindings, I would  
> > prefer a general  
> > mechanism to transfer state from one binding to another.  
> > Then one could  
> > implement all your rebind\* methods in terms of that general  
> > mechanism plus  
> > do more. Alternatively one could think about conversion  
> > methods Binding \<-\>  
> > Hash.
> 
> Transferring locals might be pretty easy, but transferring the  
> meaning of self would be more difficult, I think. At least  
> without making a new Binding (and then you'd still need a way  
> to rebind it to the original proc).

Why do you think that self is special? If there is a general mechanism to  
transfer state (i.e. bindings) into a binding, any variable can be  
rebound. I imagine something like

proc.binding.bind(:self =\> whatever, :foo =\> "bar")  
eval("self", proc.binding) # -\> whatever  
eval("foo", proc.binding) # -\> "bar"

\<snip/\>

> Interesting. I assumed that since  
> class\_eval/instance\_eval/module\_eval all returned the same  
> "self" that they did the same thing. The only difference I see  
> is in "def \<method\> ...". With class\_eval, it defines instance  
> methods and with instance\_eval, it defines class methods.  
> That's kind of strange. Some kind of magic is going on here  
> other than the changing of self.

Definitely.

> I was hoping that instance\_eval could be used to define class  
> methods using #define\_method, but #define\_method does the same  
> with both - defines instance methods. Anybody know of an  
> equivalent to #define\_method for making class methods? I  
> couldn't figure out any way to define a class method from a  
> proc - just out of curiosity.

Since a class method is just an instance method of the class:

> > class Foo;end

=\> nil

> > class \<\<Foo  
> > &nbsp;&nbsp;define\_method(:bar) {"bar"}  
> > end

=\> #\<Proc:0x10185860@(irb):7\>

> >

?\> Foo.bar  
=\> "bar"

> >

Thanks for the interesting exchange!

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > --- Robert Klemme \<bob.news@gmx.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [8 June 2005 16:30 UTC](https://rubytalk.org/t/making-a-duck/18817/25 "2005-06-08T16:30:53Z")

</div>

> \>\> When do you think will unbind\_locals be useful?  
> \>  
> \> As a replacement for many string evals - which are ugly,  
> \> inefficient, and possibly dangerous. Many (most?) times  
> that  
> \> you need to eval a string it is because you need to pull in  
> \> some local variables to help define the string to be  
> evaled.  
> \> Here is the first example of a string eval in the 1.8  
> library I  
> \> found:  
> \>  
> \> for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
> \> LI OPTION tr th td ]  
> \> methods += \<\<-BEGIN + nO\_element\_def(element) + \<\<-END  
> \> def #{element.downcase}(attributes = {})  
> \> BEGIN  
> \> end  
> \> END  
> \> end  
> \> eval(methods)  
> \>  
> \> This could be replaced by:  
> \>  
> \> for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
> \> LI OPTION tr th td ]  
> \> define\_method(element.downcase.to\_sym ,  
> \> proc { |attributes={}|  
> \> nO\_element\_def(element,attributes)  
> \> }.unbind\_locals # replace element with constant  
> \> )  
> \> end  
> \>  
> \> Much cleaner, huh?
> 
> Not really (at least to my eyes). Also, there are some  
> issues:
> 
> - I don't know what nO\_element\_def does exactly, but it will  
> have to be  
> rewritten to not create a string with ruby code

Yep. I wasn't paying attention very well. Here is its  
definition (this is from cgi.rb, BTW):

def nOE\_element\_def(element, append = nil)  
&nbsp;&nbsp;&nbsp;&nbsp;s = \<\<-END  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\<#{element.upcase}" + attributes.collect{|name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next unless value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" " + CGI::escapeHTML(name) +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if true == value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'="' + CGI::escapeHTML(value) + '"'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.to\_s + "\>"  
&nbsp;&nbsp;&nbsp;&nbsp;END  
&nbsp;&nbsp;&nbsp;&nbsp;s.sub!(/\Z/, " +") \<\< append if append  
&nbsp;&nbsp;&nbsp;&nbsp;s  
end  
def nO\_element\_def(element)  
&nbsp;&nbsp;&nbsp;nOE\_element\_def(element, \<\<-END)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield.to\_s + "\</#{element.upcase}\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;END  
end

Here would be the evaluating (instead of generating) version:

def nOE\_element\_def(element,attributes)  
&nbsp;&nbsp;&nbsp;&nbsp;"\<#{element.upcase}" + attributes.collect{|name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next unless value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" " + CGI::escapeHTML(name) +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if true == value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'="' + CGI::escapeHTML(value) + '"'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;}.to\_s + "\>"  
end  
def nO\_element\_def(element,attributes)  
&nbsp;&nbsp;&nbsp;&nbsp;nOE\_element\_def(element,attributes) +  
&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield.to\_s + "\<#{element.upcase}\>"  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> - Your version might be less efficient.

If you flatten the hierarchy, you should be able to get the  
same efficiency:

for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LI OPTION tr th td ]  
&nbsp;&nbsp;define\_method(element.downcase.to\_sym ,  
&nbsp;&nbsp;&nbsp;&nbsp;proc { |attributes={}|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\<#{element.upcase}" + attributes.collect{|name, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next unless value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" " + CGI::escapeHTML(name) +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if true == value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'="' + CGI::escapeHTML(value) + '"'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.to\_s + "\>" +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield.to\_s + "\<#{element.upcase}\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;}.unbind\_locals # replace element with constant  
&nbsp;&nbsp;)  
end

> - There might be subtle differences because "element" is  
> pulled into the  
> closure
> 
> - Also, unbind\_locals will remove "element" from the procs  
> bindings

I'm assuming that unbind\_locals will simply replace any local  
variables (external to the proc) with what their current value  
is: element will become "HTML", "HEAD", "BODY", etc.

> \>\> A proc  
> \>\> typically needs some  
> \>\> of the variables bound. As for the rebindings, I would  
> \>\> prefer a general  
> \>\> mechanism to transfer state from one binding to another.  
> \>\> Then one could  
> \>\> implement all your rebind\* methods in terms of that  
> general  
> \>\> mechanism plus  
> \>\> do more. Alternatively one could think about conversion  
> \>\> methods Binding \<-\>  
> \>\> Hash.  
> \>  
> \> Transferring locals might be pretty easy, but transferring  
> the  
> \> meaning of self would be more difficult, I think. At least  
> \> without making a new Binding (and then you'd still need a  
> way  
> \> to rebind it to the original proc).
> 
> Why do you think that self is special? If there is a general  
> mechanism to  
> transfer state (i.e. bindings) into a binding, any variable  
> can be  
> rebound. I imagine something like
> 
> proc.binding.bind(:self =\> whatever, :foo =\> "bar")  
> eval("self", proc.binding) # -\> whatever  
> eval("foo", proc.binding) # -\> "bar"

I was assuming that with local variables, you just be moving  
the value from one binding to another - not aliasing. The  
problem is that you can't assign to "self". If you really  
could do the above, then this:

binding.bind(:self =\> whatever)

would be equivalent to:

self = whatever

I don't that would be a trivial thing to implement. But, if  
this "bind" created a new binding (i.e. non-destructive instead  
of destructive), it would seem more feasible:

whateverBinding = binding.bind(:self =\> whatever)  
whateverBinding.self.object\_id==whatever.object\_id

But then you are back to where you started - you still have a  
bind the proc to a different binding (probably returning a new  
proc).

> \> Anybody know of an  
> \> equivalent to #define\_method for making class methods? I  
> \> couldn't figure out any way to define a class method from a  
> \> proc - just out of curiosity.
> 
> Since a class method is just an instance method of the class:
> 
> \>\> class Foo;end  
> =\> nil  
> \>\> class \<\<Foo  
> \>\> define\_method(:bar) {"bar"}  
> \>\> end

Thanks! That works, but I wanted the proc to have visibility  
to the local variables. I think this would be more useful:

class Foo;end  
(class\<\<Foo;self;end).send(:define\_method,:bar) {"bar"}

> Thanks for the interesting exchange!

and thanks for your ideas.

... back to my original topic - making a duck. For my stuff, I  
think I've decided to have 2 ways of doing it:

# make a duck using methods from obj  
myDuck = obj.duck(newSym, oldSym, ...)

# make a duck using arbitrary procs  
myDuck = duck(methSym =\> methProc, ...)

One of the applications I'm looking at now is for a method that  
looks like this:

Cursor#scan(value)

where value is String/Array like. All it needs is to respond  
to #[int] which should return an object that responds to #==.  
Although this #scan may look like it just matches to a verbatim  
String/Array, you could also do something like this to match to  
some digits:

# make == look like ===  
digit = (?0..?9).duck(:==,:===)  
# match to a string of 4 digits  
# Proc responds to like a String/Array  
cursor.scan(proc{|i|(i\<4)? digit : nil})

Do people do things like this with duck-typing? Or are most  
just all talk. Doing something like the above is where I see  
the power.

> **···**
>
> --- Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Discover Yahoo!  
> Use Yahoo! to plan a weekend, have fun online and more. Check it out!  
> [http://discover.yahoo.com/](http://discover.yahoo.com/)

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [9 June 2005 04:32 UTC](https://rubytalk.org/t/making-a-duck/18817/26 "2005-06-09T04:32:21Z")

</div>

Hi,

At Thu, 9 Jun 2005 01:30:53 +0900,  
Eric Mahurin wrote in [ruby-talk:144892]:

> # make == look like ===  
> digit = (?0..?9).duck(:==,:===)  
> # match to a string of 4 digits  
> # Proc responds to like a String/Array  
> cursor.scan(proc{|i|(i\<4)? digit : nil})

It would be called as `alias', I guess.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [9 June 2005 13:46 UTC](https://rubytalk.org/t/making-a-duck/18817/27 "2005-06-09T13:46:29Z")

</div>

I don't care too much what the method names are. Below is the  
implementation I was thinking. This gives you 3 ways to make a  
"duck" (a minimal object for a duck-typed argument of a  
method):

digit = (?0..?9).duck(:==,:include?,:to\_s,:inspect)  
alpha = Object.duck(size: proc{26}, :== =\>  
(?a..?z).method(:===) )  
test = Object.duck(:hi) {puts "hello world!"}

Any chance of getting something like this in the standard lib?  
Your "Behavior" implementation is fine too.

class Object  
&nbsp;&nbsp;&nbsp;&nbsp;def duck(\*new\_old)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj = Object.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = (class\<\<obj;self;end)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until new\_old.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new,old = new\_old.slice!(0,2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.send(:define\_method,new,&self.method(old))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;def self.duck(methods,&block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj = self.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = (class\<\<obj;self;end)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.send(:define\_method,methods,&block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methods.each { |name,proc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.send(:define\_method,name,&proc)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> --- nobuyoshi nakada \<nobuyoshi.nakada@ge.com\> wrote:
> 
> > Hi,
> > 
> > At Thu, 9 Jun 2005 01:30:53 +0900,  
> > Eric Mahurin wrote in [ruby-talk:144892]:  
> > \> # make == look like ===  
> > \> digit = (?0..?9).duck(:==,:===)  
> > \> # match to a string of 4 digits  
> > \> # Proc responds to like a String/Array  
> > \> cursor.scan(proc{|i|(i\<4)? digit : nil})
> > 
> > It would be called as `alias', I guess.
> > 
> > --  
> > Nobu Nakada
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Discover Yahoo!  
> Use Yahoo! to plan a weekend, have fun online and more. Check it out!  
> [http://discover.yahoo.com/](http://discover.yahoo.com/)

[Previous page](https://rubytalk.org/t/making-a-duck/18817.md?page=1)
