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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Iñaki Baz C. wrote:

| 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:
|
| object = Object.new
| class << object
|
| What does that “class” mean??

it’s the class keyword to define classes.

class << object
~ # stuff here
end

extends the object instance on the spot, instead of, for example,
re-creating ‘object’ / inheriting from another class (like Array), and
extending your child.


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ “When life gives you a lemon, make lemonade.” -Susie “I say, when
life gives you a lemon, wing it right back and add some lemons of your
own!” -Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgYfsUACgkQbtAgaoJTgL/bigCglTM/NCalvkJ+XWzjGPg2v2Cn
1o8AnRUAjt9JDN3NjnTf5C1AeMc6l906
=L5yX
-----END PGP SIGNATURE-----

2008/4/30, David A. Black [email protected]:

What does that “class” mean??

http://www.wobblini.net/singletons.html

Oh, have you written all this documentation just now to explain me?
thanks a lot!!
XDDDD

On Apr 30, 9:40 am, Iñaki Baz C. [email protected] wrote:

attr_accessor :param
Because I need to give value to an attribute of a class containing the
attr_accessor :modified
modify @modified attribute.
Iñaki Baz C.
[email protected]

Create your own Hash subclass?

def MyBag

def initialize(parent)
@parent = parent
@theBag = {}
end

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

def
@theBag[k]
end
end

class Uri
def initialize
@params = MyBag.new(self) #need to do this in initialize,
otherwise self is not the instance
@modified = false
end
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 Wed, 30 Apr 2008 07:22:30 -0500, Iñaki Baz C. wrote:

but obviously I get error:

SyntaxError: compile error
syntax error, unexpected ‘=’

Is there some way? Thanks a lot.

def initialize
@params={}
end

def param
@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.

2008/4/30, Chris H. [email protected]:

@theBag[k] = v
@parent.modified = true

end

Simply great!!!

I’ve learnt a LOT of Ruby magic in this thread, thanks to all for all
the solutions :slight_smile:

2008/4/30, Ken B. [email protected]:

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.

On Wed, 30 Apr 2008 09:56:04 -0500, Iñaki Baz C. wrote:

.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
def initialize hash
#thanks to duck typing hash can be an Array instead, if you’d like
@hash=hash
end
def [] key
#with .dup here, mutating operations on the returned value
#will not affect the values stored in the @hash
#note that even attr_reader isn’t this careful about the
#objects that it returns, and so if you want behavior
#like attr_reader, then omit the .dup here
@hash[key].dup
end
def []= key,value
@hash[key]=value
end
end

class MyClass
def initialize
@params={}
end

def param
ParamsProxy.new @params
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

El Jueves, 1 de Mayo de 2008, Ken B. escribió:

But I don’t want that since it allows any self modification way (.strip!
@hash=hash
@hash[key]=value
end
be creative, and let us know exactly what you do come up with.
Thanks a lot. Very useful information :slight_smile: