# Dynamically Create Singleton Method

**URL:** <https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855>\
**Category:** Ruby\
**Created:** [December 9, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855 "2006-12-09T14:11:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/1 "2006-12-09T14:11:18Z")

</div>

I’m not sure how to explain what I’m looking for so I will show you:

# this works, but not quite the functionality I need

def simulate\_cgi\_upload  
class \<\< cgi\_file = Tempfile.new(‘simulate-cgi’)  
def content\_type() ‘image/jpeg’ end  
end  
cgi\_file  
end

# this does NOT work, but has the functionality I need

# i.e.: dynamically defining content\_type’s return value using

# the argument from simulate\_cgi\_upload method.

def simulate\_cgi\_upload(mime\_type = ‘image/jpeg’)  
class \<\< cgi\_file = Tempfile.new(‘simulate-cgi’)  
def content\_type() mime\_type end  
end  
cgi\_file  
end

-pachl

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/2 "2006-12-09T14:27:21Z")

</div>

Hi –

On Sat, 9 Dec 2006, pachl wrote:

> end  
> Try this (or something like it in case I’ve mistyped anything):

def simulate\_cgi\_upload(mime\_type = ‘image/jpeg’)  
cgi\_file = Tempfile.new(‘simulate-cgi’)  
(class \<\< cgi\_file; self; end).class\_eval do  
define\_method(“content\_type”) { mime\_type }  
end  
cgi\_file  
end

David

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/3 "2006-12-09T15:25:25Z")

</div>

On Dec 9, 6:26 am, removed\_email\_address@domain.invalid wrote:

> > end  
> > endTry this (or something like it in case I’ve mistyped anything):
> 
> def simulate\_cgi\_upload(mime\_type = ‘image/jpeg’)  
> cgi\_file = Tempfile.new(‘simulate-cgi’)  
> (class \<\< cgi\_file; self; end).class\_eval do  
> define\_method(“content\_type”) { mime\_type }  
> end  
> cgi\_file  
> end

Excactly what I was looking for; compact and fairly elegant.

Thanks for the very quick response David and good morning to you.

-pachl

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/4 "2006-12-09T16:23:34Z")

</div>

pachl wrote:

> end  
> Well, you need a closure, which is not the case in what you would  
> like. The following code seems to do what you’re looking for, just adapt  
> it:

def a(a)  
b = Object.new  
c = Module.new  
c.send(:define\_method,:a) do # needed as define\_method is private  
return a  
end  
b.extend©  
end

a = a(‘biniou’)  
p a.a # -\> “biniou”

Cheers,

```
Vince
```

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/5 "2006-12-09T18:05:42Z")

</div>

On Sat, Dec 09, 2006 at 10:10:09PM +0900, pachl wrote:  
} I’m not sure how to explain what I’m looking for so I will show you:  
}  
} # this works, but not quite the functionality I need  
}  
} def simulate\_cgi\_upload  
} class \<\< cgi\_file = Tempfile.new(‘simulate-cgi’)  
} def content\_type() ‘image/jpeg’ end  
} end  
} cgi\_file  
} end  
}  
}  
} # this does NOT work, but has the functionality I need  
} # i.e.: dynamically defining content\_type’s return value using  
} # the argument from simulate\_cgi\_upload method.  
}  
} def simulate\_cgi\_upload(mime\_type = ‘image/jpeg’)  
} class \<\< cgi\_file = Tempfile.new(‘simulate-cgi’)  
} def content\_type() mime\_type end  
} end  
} cgi\_file  
} end

For what I think you’re trying to do, try this:

def simulate\_cgi\_upload(mime\_type = ‘image/jpeg’)  
cgi\_file = Tempfile.new(‘simulate-cgi’)  
(class \<\< cgi\_file; self; end).send(:attr\_accessor, :content\_type)  
cgi\_file.content\_type = mime\_type  
cgi\_file  
end

} -pachl  
–Greg

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [December 9, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/dynamically-create-singleton-method/78855/6 "2006-12-09T18:39:47Z")

</div>

Hi –

On Sat, 9 Dec 2006, pachl wrote:

> > > cgi\_file  
> > > cgi\_file  
> > > Excactly what I was looking for; compact and fairly elegant.
> 
> Thanks for the very quick response David and good morning to you.

Good morning 🙂

It would be more elegant if we had Kernel#singleton\_class – then you  
wouldn’t have to do the little workaround. Fingers crossed…

David
