# Magic method instantiation

**URL:** <https://www.ruby-forum.com/t/magic-method-instantiation/74374>\
**Category:** Ruby\
**Created:** [November 5, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/magic-method-instantiation/74374 "2006-11-05T19:58:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 5, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/magic-method-instantiation/74374/1 "2006-11-05T19:58:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 7, 2006, 11:04am UTC](https://www.ruby-forum.com/t/magic-method-instantiation/74374/2 "2006-11-07T11:04:43Z")

</div>

> 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.
