# How to create "def method(item)= (value)"?

**URL:** <https://rubytalk.org/t/how-to-create-def-method-item-value/46211>\
**Category:** ruby-talk\
**Created:** [30 April 2008 12:22 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211 "2008-04-30T12:22:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 12:22 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/1 "2008-04-30T12:22:30Z")

</div>

Hi, is it not possible to create a method to use in this way?

&nbsp;&nbsp;&nbsp;my\_object.param("tag") = "new\_value"

How to define that method? I've tryed something as:

&nbsp;&nbsp;def param(name)= (value)  
&nbsp;&nbsp;&nbsp;&nbsp;@params[name] = value  
&nbsp;&nbsp;end

but obviously I get error:

&nbsp;&nbsp;SyntaxError: compile error  
&nbsp;&nbsp;syntax error, unexpected '='

Is there some way? Thanks a lot.

> **···**
>
> --  
> Iñaki Baz Castillo  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [30 April 2008 12:50 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/2 "2008-04-30T12:50:14Z")

</div>

Iñaki Baz Castillo wrote:

> Hi, is it not possible to create a method to use in this way?
> 
> &nbsp;&nbsp;&nbsp;my\_object.param("tag") = "new\_value"
> 
> How to define that method? I've tryed something as:
> 
> &nbsp;&nbsp;def param(name)= (value)  
> &nbsp;&nbsp;&nbsp;&nbsp;@params[name] = value  
> &nbsp;&nbsp;end
> 
> but obviously I get error:
> 
> &nbsp;&nbsp;SyntaxError: compile error  
> &nbsp;&nbsp;syntax error, unexpected '='
> 
> Is there some way? Thanks a lot.

Untested, and probably highly inelegant:

def param key, value  
~ @params["#{key.to\_sym}"] = value  
end

Should produce:

> object.params my\_key, 'my value'

=\> @params[:my\_key] =\> 'my value'

While not the interface you desire, it is the output you want.

(May be useful as a Rails helper, but otherwise?)

Or, you could use an optionhash to create your method:  
,[http://www.lukeredpath.co.uk/2006/7/27/using-ruby-hashes-as-keyword-arguments-with-easy-defaults&gt](http://www.lukeredpath.co.uk/2006/7/27/using-ruby-hashes-as-keyword-arguments-with-easy-defaults&gt);

Something like:

def param(options = {})  
~ @params[options.key] = options[options.key]  
end

should look like

> object.param 'key' =\> 'value'

=\> @params['key'] =\> 'value'

You could sexy that up with Hash#merge:

def param(options = {})  
~ @params = @params.merge options  
end

(probably more efficient than the first variant, too.)

Hope that helps.

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ - You know you've been hacking too long when...  
...you want to wash your hair and think: awk -F"/neck" '{ print $1 }' |  
shower

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [30 April 2008 14:45 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/3 "2008-04-30T14:45:07Z")

</div>

def initialize  
&nbsp;&nbsp;@params={}  
end

def param  
&nbsp;&nbsp;@params  
end

my\_object.param["tag"]="new value"

If you need some sort of validation, you can have param return some other  
class that defines and = and does the necessary validation, and you  
can even set up an easy method for defining the param attribute to do all  
of that if need be.

> **···**
>
> On Wed, 30 Apr 2008 07:22:30 -0500, Iñaki Baz Castillo wrote:
> 
> > Hi, is it not possible to create a method to use in this way?
> > 
> > &nbsp;&nbsp;&nbsp;my\_object.param("tag") = "new\_value"
> > 
> > How to define that method? I've tryed something as:
> > 
> > &nbsp;&nbsp;def param(name)= (value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@params[name] = value  
> > &nbsp;&nbsp;end
> > 
> > but obviously I get error:
> > 
> > &nbsp;&nbsp;SyntaxError: compile error  
> > &nbsp;&nbsp;syntax error, unexpected '='
> > 
> > Is there some way? Thanks a lot.
> 
> --  
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 April 2008 12:59 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/4 "2008-04-30T12:59:27Z")

</div>

Hi --

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> Iñaki Baz Castillo wrote:  
> \> Hi, is it not possible to create a method to use in this way?  
> \>  
> \> my\_object.param("tag") = "new\_value"  
> \>  
> \> How to define that method? I've tryed something as:  
> \>  
> \> def param(name)= (value)  
> \> @params[name] = value  
> \> end  
> \>  
> \> but obviously I get error:  
> \>  
> \> SyntaxError: compile error  
> \> syntax error, unexpected '='  
> \>  
> \> Is there some way? Thanks a lot.  
> \>
> 
> Untested, and probably highly inelegant:
> 
> def param key, value  
> ~ @params["#{key.to\_sym}"] = value  
> end
> 
> Should produce:
> 
> \> object.params my\_key, 'my value'  
> =\> @params[:my\_key] =\> 'my value'

"#{key.to\_sym}" is always going to produce a string, never a symbol.  
I'm not sure the OP wants to constrain the keys to be symbols anyway.  
Also, you're calling params; I think you mean param.

So:

&nbsp;&nbsp;&nbsp;def param(key,value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@params[key] = value  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;object.param(my\_key, "my\_value")

Also untested, due to laziness 🙂

You could also do:

&nbsp;&nbsp;&nbsp;def param=(key,value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@params[key] = value  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;object.param[key] = value

Of course, then one might as well do:

&nbsp;&nbsp;&nbsp;attr\_reader :params

on the object, and them:

&nbsp;&nbsp;&nbsp;object.params[key] = value

David

> **···**
>
> On Wed, 30 Apr 2008, Phillip Gawlowski wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 13:05 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/5 "2008-04-30T13:05:47Z")

</div>

> Untested, and probably highly inelegant:
> 
> def param key, value  
> ~ @params["#{key.to\_sym}"] = value  
> end
> 
> Should produce:
> 
> \> object.params my\_key, 'my value'  
> =\> @params[:my\_key] =\> 'my value'
> 
> While not the interface you desire, it is the output you want.

Yes, that exactly what I do, but I don't like it, since it's not  
intuitive. In fact I call the method "set\_param(name, value)". But it  
makes the code look like Java.. brrrrr.... XD

> \> object.param 'key' =\> 'value'  
> =\> @params['key'] =\> 'value'
> 
> You could sexy that up with Hash#merge:
> 
> def param(options = {})  
> ~ @params = @params.merge options  
> end
> 
> (probably more efficient than the first variant, too.)

Interesting, and thanks for the useful link. 🙂

> **···**
>
> 2008/4/30, Phillip Gawlowski \<cmdjackryan@googlemail.com\>:
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 14:56 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/6 "2008-04-30T14:56:04Z")

</div>

But I don't want that since it allows any self modification way  
(.strip! .chomp! ...) and I just want to allow =

Thanks.

> **···**
>
> 2008/4/30, Ken Bloom \<kbloom@gmail.com\>:
> 
> > def initialize  
> > &nbsp;&nbsp;@params={}  
> > end
> > 
> > def param  
> > &nbsp;&nbsp;@params  
> > end
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [30 April 2008 13:10 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/7 "2008-04-30T13:10:32Z")

</div>

David A. Black wrote:

> > \> object.params my\_key, 'my value'  
> > =\> @params[:my\_key] =\> 'my value'
> 
> "#{key.to\_sym}" is always going to produce a string, never a symbol.

\*facepalm\* Right. String interpolation. Too clever for my own good, is  
what I am..

> I'm not sure the OP wants to constrain the keys to be symbols anyway.

Well, given that Rails uses the params hash, with Symbols as keys, I  
took a guess. 😉

Also, it is a bit of a habit on my part to use Symbols, instead of  
strings or so.

> Also, you're calling params; I think you mean param.

Yes, indeed. Typo on my part (I cought it the other two chances..).

> You could also do:
> 
> &nbsp;&nbsp;def param=(key,value)  
> &nbsp;&nbsp;&nbsp;&nbsp;@params[key] = value  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;object.param[key] = value

Oh, I'm gonna steal that idiom if you don't mind. 😉

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ "My ethicator machine must've had a built-in moral compromise  
~ spectral release phantasmatron! I'm a genius!" --- Calvin

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 13:11 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/8 "2008-04-30T13:11:07Z")

</div>

> You could also do:
> 
> &nbsp;&nbsp;def param=(key,value)  
> &nbsp;&nbsp;&nbsp;&nbsp;@params[key] = value  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;object.param[key] = value

That's not valid, a method cannot contain :

> def hola() end

SyntaxError: compile error  
syntax error, unexpected '[', expecting '\n' or ';'  
def hola() end

> Of course, then one might as well do:
> 
> &nbsp;&nbsp;attr\_reader :params
> 
> on the object, and them:
> 
> &nbsp;&nbsp;object.params[key] = value

Yes, but that is not valid for me since I need to exec some extra code  
so I need to create an abstraction layer to access @params.

Thanks.

> **···**
>
> 2008/4/30, David A. Black \<dblack@rubypal.com\>:
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [30 April 2008 13:14 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/9 "2008-04-30T13:14:05Z")

</div>

Iñaki Baz Castillo wrote:

> Yes, that exactly what I do, but I don't like it, since it's not  
> intuitive. In fact I call the method "set\_param(name, value)". But it  
> makes the code look like Java.. brrrrr.... XD

Not commenting on the Java part (the naming in Java is usually rather  
well done, IMO), but I agree, the solution isn't all too elegant because  
of it's lack of intuitive assignment.

Usually, I use something like that, when I am too lazy to create an  
optionhash based solution.

\*adds that to TODO list for a current project\*

> Interesting, and thanks for the useful link. 🙂

You are welcome. 🙂

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Do you know why the fairy sits at the top of the Xmas tree ?

~ One year, Santa had one tree left over after giving everybody one, and  
when one of his fairy helpers asked what she should do with it, he told her.

~ That`s why.

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [30 April 2008 13:25 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/10 "2008-04-30T13:25:04Z")

</div>

Why not just access param?

class Foo  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@param = {}  
&nbsp;&nbsp;end  
&nbsp;&nbsp;attr\_accessor :param  
end

x = Foo.new  
x.param['key'] = 'bar'

If you do need to override it, the Hash assignment method is []=  
So I think you can write:  
def []=(key,value)  
...

Cheers

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [1 May 2008 05:10 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/11 "2008-05-01T05:10:06Z")

</div>

In that case, do as I wrote further on and have the param function return  
a proxy that edits the params array in only the ways you want.

class ParamsProxy  
&nbsp;&nbsp;def initialize hash  
&nbsp;&nbsp;&nbsp;&nbsp;#thanks to duck typing hash can be an Array instead, if you'd like  
&nbsp;&nbsp;&nbsp;&nbsp;@hash=hash  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def key  
&nbsp;&nbsp;&nbsp;&nbsp;#with .dup here, mutating operations on the returned value  
&nbsp;&nbsp;&nbsp;&nbsp;#will not affect the values stored in the @hash  
&nbsp;&nbsp;&nbsp;&nbsp;#note that even attr\_reader isn't this careful about the  
&nbsp;&nbsp;&nbsp;&nbsp;#objects that it returns, and so if you want behavior  
&nbsp;&nbsp;&nbsp;&nbsp;#like attr\_reader, then omit the .dup here  
&nbsp;&nbsp;&nbsp;&nbsp;@hash[key].dup  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def = key,value  
&nbsp;&nbsp;&nbsp;&nbsp;@hash[key]=value  
&nbsp;&nbsp;end  
end

class MyClass  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@params={}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def param  
&nbsp;&nbsp;&nbsp;&nbsp;ParamsProxy.new @params  
&nbsp;&nbsp;end  
end

I presented this in rough outline before, without code, because there are  
many ways one could want to dress this up -- having some kind of  
validation code, or having the object.param[key]=value update something  
else entirely, or shoving the whole thing behind a nice attr\_accessor-  
like interface. I don't have the time now to think through all of the  
possibilities involved to come up with a general solution, nor do I have  
a clear idea of exactly what your other constraints are. But start here,  
be creative, and let us know exactly what you do come up with.

--Ken

> **···**
>
> On Wed, 30 Apr 2008 09:56:04 -0500, Iñaki Baz Castillo wrote:
> 
> > 2008/4/30, Ken Bloom \<kbloom@gmail.com\>:
> > 
> > > def initialize  
> > > &nbsp;&nbsp;@params={}  
> > > end
> > > 
> > > def param  
> > > &nbsp;&nbsp;@params  
> > > end
> > 
> > But I don't want that since it allows any self modification way (.strip!  
> > .chomp! ...) and I just want to allow =
> > 
> > Thanks.
> 
> --  
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 13:26 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/12 "2008-04-30T13:26:22Z")

</div>

I'm sorry, but as I've replied in this thread that method naming  
(using ) is invalid in Ruby ☹

> **···**
>
> 2008/4/30, Phillip Gawlowski \<cmdjackryan@googlemail.com\>:
> 
> > \> You could also do:  
> > \>  
> > \> def param=(key,value)  
> > \> @params[key] = value  
> > \> end  
> > \>  
> > \> object.param[key] = value
> > 
> > Oh, I'm gonna steal that idiom if you don't mind. 😉
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 13:40 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/13 "2008-04-30T13:40:33Z")

</div>

Because I need to give value to an attribute of a class containing the  
Hash @params, so the only way (AFAIK) is by creating an abstraction  
layer and handling the Hash @params via current class methods. This is  
my case:

class Uri

&nbsp;&nbsp;# Atributes:  
&nbsp;&nbsp;@modified =\> true/false  
&nbsp;&nbsp;@params =\> Hash

&nbsp;&nbsp;&nbsp;attr\_accessor :modified

&nbsp;&nbsp;&nbsp;#what I want but it's not possible:  
&nbsp;&nbsp;&nbsp;def param(name)= (value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@params[name] = value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@modified = true  
&nbsp;&nbsp;&nbsp;end

end

I can't do it by extending Hash class since inside Hash class I can't  
modify @modified attribute.  
Also note there are more ways than "=" to modify a Hash value:  
&nbsp;&nbsp;@params[name].strip! , .chomp! , .downcase! ......  
and I don't want to redefine all of them so I just want to allow "="  
and when it's called @modifed must be true.

Hope I've explained why attr\_accessor is not useful for me in this case.

Thanks a lot.

> **···**
>
> 2008/4/30, Chris Hulan \<chris.hulan@gmail.com\>:
> 
> > Why not just access param?
> > 
> > class Foo  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@param = {}  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;attr\_accessor :param  
> > end
> > 
> > x = Foo.new  
> > x.param['key'] = 'bar'
> > 
> > If you do need to override it, the Hash assignment method is =  
> > So I think you can write:  
> > def =(key,value)  
> > ...
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 April 2008 13:46 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/14 "2008-04-30T13:46:51Z")

</div>

Hi --

> **···**
>
> On Wed, 30 Apr 2008, Iñaki Baz Castillo wrote:
> 
> > 2008/4/30, David A. Black \<dblack@rubypal.com\>:
> > 
> > > You could also do:
> > > 
> > > &nbsp;&nbsp;def param=(key,value)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@params[key] = value  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;object.param[key] = value
> > 
> > That's not valid, a method cannot contain :
> 
> Yikes. Sorry. I'm being stupid. (I think there was some real code of  
> which that was a wrong version, but now I can't remember what I  
> meant....)
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 April 2008 13:49 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/15 "2008-04-30T13:49:50Z")

</div>

Hi --

> \> You could also do:  
> \>  
> \> def param=(key,value)  
> \> @params[key] = value  
> \> end  
> \>  
> \> object.param[key] = value
> 
> Oh, I'm gonna steal that idiom if you don't mind. 😉

Sorry -- it's wrong. I think I've remembered what I meant to write.  
This time, let's go all out and try the code! 🙂

object = Object.new  
class \<\< object  
&nbsp;&nbsp;&nbsp;attr\_accessor :params  
end  
object.params = {}

def object.=(key,value)  
&nbsp;&nbsp;&nbsp;@params[key] = value  
end

object[1] = 2

p object.params # { 1 =\> 2 }

Too bad -- I kind of liked my garbled rubbish version 🙂

David

> **···**
>
> On Wed, 30 Apr 2008, Phillip Gawlowski wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [1 May 2008 14:13 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/16 "2008-05-01T14:13:57Z")

</div>

Thanks a lot. Very useful information 🙂

> **···**
>
> El Jueves, 1 de Mayo de 2008, Ken Bloom escribió:
> 
> > On Wed, 30 Apr 2008 09:56:04 -0500, Iñaki Baz Castillo wrote:  
> > \> 2008/4/30, Ken Bloom \<kbloom@gmail.com\>:  
> > \>\> def initialize  
> > \>\> @params={}  
> > \>\> end  
> > \>\>  
> > \>\> def param  
> > \>\> @params  
> > \>\> end  
> > \>  
> > \> But I don't want that since it allows any self modification way (.strip!  
> > \> .chomp! ...) and I just want to allow =  
> > \>  
> > \> Thanks.
> > 
> > In that case, do as I wrote further on and have the param function return  
> > a proxy that edits the params array in only the ways you want.
> > 
> > class ParamsProxy  
> > &nbsp;&nbsp;def initialize hash  
> > &nbsp;&nbsp;&nbsp;&nbsp;#thanks to duck typing hash can be an Array instead, if you'd like  
> > &nbsp;&nbsp;&nbsp;&nbsp;@hash=hash  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;def key  
> > &nbsp;&nbsp;&nbsp;&nbsp;#with .dup here, mutating operations on the returned value  
> > &nbsp;&nbsp;&nbsp;&nbsp;#will not affect the values stored in the @hash  
> > &nbsp;&nbsp;&nbsp;&nbsp;#note that even attr\_reader isn't this careful about the  
> > &nbsp;&nbsp;&nbsp;&nbsp;#objects that it returns, and so if you want behavior  
> > &nbsp;&nbsp;&nbsp;&nbsp;#like attr\_reader, then omit the .dup here  
> > &nbsp;&nbsp;&nbsp;&nbsp;@hash[key].dup  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;def = key,value  
> > &nbsp;&nbsp;&nbsp;&nbsp;@hash[key]=value  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class MyClass  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@params={}  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def param  
> > &nbsp;&nbsp;&nbsp;&nbsp;ParamsProxy.new @params  
> > &nbsp;&nbsp;end  
> > end
> > 
> > I presented this in rough outline before, without code, because there are  
> > many ways one could want to dress this up -- having some kind of  
> > validation code, or having the object.param[key]=value update something  
> > else entirely, or shoving the whole thing behind a nice attr\_accessor-  
> > like interface. I don't have the time now to think through all of the  
> > possibilities involved to come up with a general solution, nor do I have  
> > a clear idea of exactly what your other constraints are. But start here,  
> > be creative, and let us know exactly what you do come up with.
> 
> --  
> Iñaki Baz Castillo

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [30 April 2008 13:33 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/17 "2008-04-30T13:33:30Z")

</div>

Iñaki Baz Castillo wrote:

> > \> You could also do:  
> > \>  
> > \> def param=(key,value)  
> > \> @params[key] = value  
> > \> end  
> > \>  
> > \> object.param[key] = value
> > 
> > Oh, I'm gonna steal that idiom if you don't mind. 😉
> 
> I'm sorry, but as I've replied in this thread that method naming  
> (using ) is invalid in Ruby ☹

Sort of. As a singleton method, you get the syntax error. But not if you  
define as a method:

irb(main):001:0\> def =(key,value)  
irb(main):002:1\> end  
=\> nil

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ "It's great to have a friend who appreciates an earnest discussion of  
ideas." -Calvin

> **···**
>
> > 2008/4/30, Phillip Gawlowski \<cmdjackryan@googlemail.com\>:

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 14:04 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/18 "2008-04-30T14:04:26Z")

</div>

Oh, that seems really interesting, but unfortunatelly I can't  
unerstand at all what yuo do there.  
Specially I don't understand the meaning of first two lines:

&nbsp;&nbsp;object = Object.new  
&nbsp;&nbsp;class \<\< object

What does that "class" mean??

Thanks a lot.

> **···**
>
> 2008/4/30, David A. Black \<dblack@rubypal.com\>:
> 
> > object = Object.new  
> > class \<\< object  
> > &nbsp;&nbsp;attr\_accessor :params  
> > end  
> > object.params = {}
> > 
> > def object.=(key,value)  
> > &nbsp;&nbsp;@params[key] = value  
> > end
> > 
> > object[1] = 2
> > 
> > p object.params # { 1 =\> 2 }
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [30 April 2008 14:15 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/19 "2008-04-30T14:15:06Z")

</div>

Create your own Hash subclass?

def MyBag

&nbsp;&nbsp;def initialize(parent)  
&nbsp;&nbsp;&nbsp;&nbsp;@parent = parent  
&nbsp;&nbsp;&nbsp;&nbsp;@theBag = {}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def =(k,v)  
&nbsp;&nbsp;&nbsp;&nbsp;@theBag[k] = v  
&nbsp;&nbsp;&nbsp;&nbsp;@parent.modified = true  
&nbsp;&nbsp;end

&nbsp;&nbsp;def (k)  
&nbsp;&nbsp;&nbsp;&nbsp;@theBag[k]  
&nbsp;&nbsp;end  
end

class Uri  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@params = MyBag.new(self) #need to do this in initialize,  
otherwise self is not the instance  
&nbsp;&nbsp;&nbsp;&nbsp;@modified = false  
&nbsp;&nbsp;end  
&nbsp;&nbsp;attr\_accessor :modified, :params

end

Not tested, and there is most likely a more elegant way to delegate to  
Hash, but you get what you pay for 9^)

> **···**
>
> On Apr 30, 9:40 am, Iñaki Baz Castillo \<i...@aliax.net\> wrote:
> 
> > 2008/4/30, Chris Hulan \<chris.hu...@gmail.com\>:
> > 
> > \> Why not just access param?
> > 
> > \> class Foo  
> > \> def initialize  
> > \> @param = {}  
> > \> end  
> > \> attr\_accessor :param  
> > \> end
> > 
> > \> x = Foo.new  
> > \> x.param['key'] = 'bar'
> > 
> > \> If you do need to override it, the Hash assignment method is =  
> > \> So I think you can write:  
> > \> def =(key,value)  
> > \> ...
> > 
> > Because I need to give value to an attribute of a class containing the  
> > Hash @params, so the only way (AFAIK) is by creating an abstraction  
> > layer and handling the Hash @params via current class methods. This is  
> > my case:
> > 
> > class Uri
> > 
> > &nbsp;&nbsp;# Atributes:  
> > &nbsp;&nbsp;@modified =\> true/false  
> > &nbsp;&nbsp;@params =\> Hash
> > 
> > &nbsp;&nbsp;&nbsp;attr\_accessor :modified
> > 
> > &nbsp;&nbsp;&nbsp;#what I want but it's not possible:  
> > &nbsp;&nbsp;&nbsp;def param(name)= (value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@params[name] = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@modified = true  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > I can't do it by extending Hash class since inside Hash class I can't  
> > modify @modified attribute.  
> > Also note there are more ways than "=" to modify a Hash value:  
> > &nbsp;&nbsp;@params[name].strip! , .chomp! , .downcase! ......  
> > and I don't want to redefine all of them so I just want to allow "="  
> > and when it's called @modifed must be true.
> > 
> > Hope I've explained why attr\_accessor is not useful for me in this case.
> > 
> > Thanks a lot.
> > 
> > --  
> > Iñaki Baz Castillo  
> > \<i...@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 April 2008 13:41 UTC](https://rubytalk.org/t/how-to-create-def-method-item-value/46211/20 "2008-04-30T13:41:39Z")

</div>

Yes, but I need "def some\_name" 🙂

> **···**
>
> 2008/4/30, Phillip Gawlowski \<cmdjackryan@googlemail.com\>:
> 
> > Sort of. As a singleton method, you get the syntax error. But not if you  
> > define as a method:
> > 
> > irb(main):001:0\> def =(key,value)  
> > irb(main):002:1\> end  
> > =\> nil
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

[Next page](https://rubytalk.org/t/how-to-create-def-method-item-value/46211.md?page=2)
