Magic method instantiation

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?

end

class_extendor “text_field_tag”, “text_field_tag2”

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs