# Extending a class

**URL:** <https://www.ruby-forum.com/t/extending-a-class/141153>\
**Category:** Ruby\
**Created:** [June 22, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/extending-a-class/141153 "2008-06-22T22:57:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [June 22, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/extending-a-class/141153/1 "2008-06-22T22:57:50Z")

</div>

Hi All

I’m trying to add methods to a class like:

class Person  
def refresh  
p “refresh”  
end  
def dup  
P “DUP”  
end

TIMED\_M = [:refresh, :dup]  
TIMED\_M.each do |method|  
p method  
alias\_method :"#{method}\_without\_timing", method

```
define_method :"{#method}_with_timing" do
  p "timing"
  start_time = Time.now.to_f
  returning(self.send(:"#{method}_without_timing")) do
    end_time = Time.now.to_f
    puts "#{method}: #{"%.3f" % (end_time - start_time)} s."
  end
end

```

end  
end

class \<\< Person  
def start\_trace  
TIMED\_M.each do |method|  
p “x”  
alias\_method method, :"#{method}\_with\_timing"  
end  
end  
end  
p = Person.new  
p.refresh  
Person.start\_trace  
p.refresh

For some unknown reason I get the following error:  
:refresh  
:dup  
“refresh”  
./t2.rb:31:in `start\_trace’: uninitialized constant Class::TIMED\_METHODS  
(NameError)  
from ./t2.rb:39

Any suggestions why I get this error ?

thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [June 22, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/extending-a-class/141153/2 "2008-06-22T23:30:26Z")

</div>

Hi –

On Mon, 23 Jun 2008, Luca S. wrote:

> end  
> end\_time = Time.now.to\_f  
> alias\_method method, :"#{method}\_with\_timing"  
> :dup  
> “refresh”  
> ./t2.rb:31:in `start\_trace’: uninitialized constant Class::TIMED\_METHODS  
> (NameError)  
> from ./t2.rb:39
> 
> Any suggestions why I get this error ?

Here’s a boiled-down version:

irb(main):001:0\> class C; X = 1; end  
=\> 1  
irb(main):002:0\> class \<\< C; X; end  
NameError: uninitialized constant Class::X

The reason is that there’s no constant X in the singleton class of C.

I also suspect that what’s above is not actually cut-and-pasted, since  
you don’t actually use “TIMED\_METHODS” anywhere 🙂

David

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [June 23, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/extending-a-class/141153/3 "2008-06-23T20:16:56Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Mon, 23 Jun 2008, Luca S. wrote:
> 
> > end  
> > end\_time = Time.now.to\_f  
> > alias\_method method, :"#{method}\_with\_timing"  
> > :dup  
> > “refresh”  
> > ./t2.rb:31:in `start\_trace’: uninitialized constant Class::TIMED\_METHODS  
> > (NameError)  
> > from ./t2.rb:39
> > 
> > Any suggestions why I get this error ?
> 
> Here’s a boiled-down version:
> 
> irb(main):001:0\> class C; X = 1; end  
> =\> 1  
> irb(main):002:0\> class \<\< C; X; end  
> NameError: uninitialized constant Class::X
> 
> The reason is that there’s no constant X in the singleton class of C.
> 
> I also suspect that what’s above is not actually cut-and-pasted, since  
> you don’t actually use “TIMED\_METHODS” anywhere 🙂
> 
> David

TIMED\_M is used:

class \<\< Person  
def start\_trace  
TIMED\_M.each do |method|  
p “x”  
alias\_method method, :"#{method}\_with\_timing"  
end  
end

Here ‘method’ is aliased using TIMED\_M
