Magic method instantiation

  • Thread starter ArtÅ«ras Å lajus
  • Start date
A

Artūras Šlajus

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?
 
D

Daniel Sheppard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top