Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > attr_writer variation

Reply
Thread Tools

attr_writer variation

 
 
Ryan Hinton
Guest
Posts: n/a
 
      01-26-2006
I am working on a nice little document-based app where I would like to
take selective action based on whether data have been modified (e.g.
prompt to save before exit). When I imagine writing a bunch of
"memberVariable=(newVar)" methods, all with the same pattern for the
guts, I think there must be a better way.

How does attr_writer do this? I tried to figure out how to do
something similar in vanilla Ruby, but I don't see a way to
programmatically define a new method. Would I have to write a C
extension for this?

Here is roughly what I want.

module AttrWriterTrackModified
def attr_writer(*syms)
syms.each do |sym|
eval("def #{sym}=(val)
if @#{sym} != val
@#{sym} = val
@modified = true
end
end;")
end
end
end

 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      01-26-2006
On Thu, 26 Jan 2006 22:15:50 -0000, Ryan Hinton <(E-Mail Removed)> wrote:

> Here is roughly what I want.
>
> module AttrWriterTrackModified
> def attr_writer(*syms)
> syms.each do |sym|
> eval("def #{sym}=(val)
> if @#{sym} != val
> @#{sym} = val
> @modified = true
> end
> end;")
> end
> end
> end
>


Maybe I'm missing something, but you're almost there. You could try this:

class Module
def attr_writer(*syms)
syms.each do |sym|
module_eval <<-EOC
def #{sym}=(val)
if @#{sym} != val
@#{sym} = val
@modified = true
end
end
EOC
end
end
end

Just use it as normal:

class TryIt
attr_reader ne, :two, :three, :modified
attr_writer ne, :two, :three
def initialize(one, two, three)
@one, @two, @three = one, two, three
@modified = false
end
end

p t = TryIt.new(1,2,3)
p t.one
p t.two
p t.three
p t.modified

p t.two = 4
p t.one
p t.two
p t.three
p t.modified

Output:

#<TryIt:0xb7facae4 @modified=false, @three=3, @two=2, @one=1>
1
2
3
false
1
4
3
true

(Hmm, I bet you could make an attr_initializer or something to cover that
initialize method too ... )

Hope that helps.

--
Ross Bamford - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Vladimir Agafonkin
Guest
Posts: n/a
 
      01-27-2006
By the way, here's a simple newbie question: what is the difference
between defining some method with class_eval in Class class and with
module_eval in Module class?

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      01-27-2006
On Fri, 27 Jan 2006 00:12:56 -0000, Vladimir Agafonkin
<(E-Mail Removed)> wrote:

> By the way, here's a simple newbie question: what is the difference
> between defining some method with class_eval in Class class and with
> module_eval in Module class?
>


Aside from the usual difference between class instance and module instance
methods, I don't think there is a difference. Module.class_eval is an
alias for Module.module_eval.

--
Ross Bamford - (E-Mail Removed)
 
Reply With Quote
 
Ryan Hinton
Guest
Posts: n/a
 
      01-27-2006
That works! But I don't want the tracking all the time. Can I put it
in a module to mix-in? I've been fiddling trying different variations,
but apparently I am missing the reason why your example works.

 
Reply With Quote
 
Ryan Hinton
Guest
Posts: n/a
 
      01-27-2006
I think I figured it out, but I'm still a little hazy so any
clarification is great. "include" merges the definition of the module
and the class, so instances of the class effectively have two
superclasses. "extend" adds the instance methods from a module to the
current object. When I call attr_writer, I am effectively calling a
method in the context of the current class being defined (self ==
MyClass). Its methods are defined in Class and/or Method, so I can
redefine attr_writer there. If I don't want to do that, I can "extend"
a class being defined with a module I write that defines and
attr_writer method. Then using attr_writer will do what I want.
However, I will need to do this in each class I want this behavior.

I am still confused by module_function. Normally the instance methods
of a module are passed on in an "include", but module_function makes a
method a module method (Mod.meth) _and_ passes it on as a private
instance method in an "include". This seems really quirky.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
attr_writer and hash aidy Ruby 2 07-14-2007 12:48 PM
using dynamic method calling with attr_writer methods Markus Hohenhaus Ruby 3 05-09-2007 10:35 PM
attr_reader + attr_writer? Joe Ruby MUDCRAP-CE Ruby 6 10-13-2006 03:20 PM
Using attr_writer? Joe Ruby 5 02-26-2006 10:06 PM
Forcing attr_writer to a type? Robert William Vesterman Ruby 4 08-28-2005 05:09 PM



Advertisments