Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding code/Extending methods after they have been written?

Reply
Thread Tools

Adding code/Extending methods after they have been written?

 
 
Marc Heiler
Guest
Posts: n/a
 
      01-26-2008
Hi there,

Let's say i have 10 methods. Each one looks like so:

def p(some_input)
print some_input
# do some more stuff...
end

Some months after I wrote this, I realized that I need to
handle blocks given to these methods. (Need is the wrong
word, a better word would be i *want to* because
Ruby allows you to)

I do it like so currently:

if block_given?
yield
some_mandatory_finalizer_method
end

The thing actually is... The last 4 lines of code look
like repetitive boilerplate code to me. The way I solved it
was by copy pasting this segment into each method, but
it kinda does not feel clean. (I have even put these
four lines of code into one line, so that I have
an easier time to remove this code if i can think of a
cleaner solution)

Does anyone else of you know of a way to add "code rules"
to some methods? Something like this would be cool:

def add_boilerplate_code
if block_given?
yield
some_mandatory_finalizer_method # this is always the same method
end
end

METHODS_THAT_NEED_TO_BE_EXTENDED = %w( p q r s t u v w x y z )
METHODS_THAT_NEED_TO_BE_EXTENDED.each do |my_method|
my_method.add_boilerplate_code
end


Thankful for any tips!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      01-26-2008
> Does anyone else of you know of a way to add "code rules"
> to some methods? Something like this would be cool:


Looks like a good use case for Aspect oriented programming.
AOP frameworks provide mechanisms to do exactly that.
There are (at least) two AOP frameworks on RubyForge:
aspectr and aquarium.

Otherwise it's fairly easy to create such a facility
from scratch via alias_method.

 
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
Flickr: Have They Been Having UserID Problems? (PeteCresswell) Digital Photography 1 05-24-2010 03:47 PM
ATTN: I have a problem concerning my C drive after installing office 2003 PLEASE HELP (all attchments have been scanned) (0/6) Twitch Computer Support 21 01-02-2004 08:29 PM
Re: ATTN: I have a problem concerning my C drive after installing office 2003 PLEASE HELP (all attchments have been scanned) (1/6) Trax Computer Support 1 01-01-2004 02:49 AM
Re: I have a problem concerning my C drive after installing office 2003 PLEASE HELP (all attchments have been scanned) (6/6) ICee Computer Support 0 12-31-2003 06:23 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments