Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Magic method instantiation

Reply
Thread Tools

Magic method instantiation

 
 
Artūras Šlajus
Guest
Posts: n/a
 
      11-05-2006
I have this method in module:

def text_field_tag(*args)
args.push(nil) if args.length == 1

args.push({}) if args.length == 2
args.last['class'] ||= 'text_field_tag' if args.last.kind_of? Hash
super(*args)
end

It works fine, the problem is that I need to define lots of these
methods.

I tried using:

def class_extendor(*args)
args.each do |arg|
define_method(name) do |*args|
args.push(nil) if args.length == 1

args.push({}) if args.length == 2
args.last['class'] ||= name if args.last.kind_of? Hash
super(*args)
end
end
end

class_extendor "text_field_tag", "text_field_tag2"

but it doesn't work (no wonders why...)

Anyway, ideas how to implement that?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Daniel Sheppard
Guest
Posts: n/a
 
      11-05-2006
> def class_extendor(*args)
> args.each do |arg|
> define_method(name) do |*args|
> args.push(nil) if args.length =3D=3D 1
>=20
> args.push({}) if args.length =3D=3D 2
> args.last['class'] ||=3D name if args.last.kind_of? Hash
> super(*args)
> end
> end
> end
>=20
> class_extendor "text_field_tag", "text_field_tag2"
>=20
> but it doesn't work (no wonders why...)


I would guess because you're using the variable args twice - the fact
that you've used it on the outside (as the parameter to your method)
means that it has a wider scope. But without knowing what your expected
output and actual output are, I can't be bothered trying to read your
mind further.


 
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
Preprocessor magic to expand template instantiation ? mathieu C++ 8 08-06-2009 10:13 PM
Preprocessor magic to expand template instantiation (2) mathieu C++ 1 08-06-2009 10:25 AM
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM



Advertisments