# Mixing define\_function and class methods

**URL:** <https://www.ruby-forum.com/t/mixing-define-function-and-class-methods/117972>\
**Category:** Ruby\
**Created:** [October 22, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/mixing-define-function-and-class-methods/117972 "2007-10-22T22:16:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garret\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Garret\_K](https://www.ruby-forum.com/u/Garret_K)\
**Post date:** [October 22, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/mixing-define-function-and-class-methods/117972/1 "2007-10-22T22:16:43Z")

</div>

Hi,  
I’m sure if I’m missing something fundamental, but when you run the  
code below with comments intact it won’t execute, claiming that:

…/test.rb:13:in `something': undefined method`some\_function’ for  
#\<B:0x2b9c102564a8\> (NoMethodError)  
from …/test.rb:30

When the comments are removed, the code will run. The thing I can’t  
understand is why, when the comments are in-place, the call to  
some\_function from within an instance of B doesn’t work. Can anyone  
point me in the right direction?

Thanks,  
Garret

class A  
def self.some\_function(param)  
puts “param: #{param}”  
end

# def some\_function(\*args)

# puts self

# B.some\_function(\*args)

# end

```
def self.register(name, &block)
    define_method(name) do |*args|
        some_function("thing")
        block.call(args)
    end
end

def initialize
    puts "I'm a #{self.class}"
end

```

end

class B \< A  
register :something do |\*args|  
some\_function(“something”)  
end  
end

b = B.new  
b.something

---

<div class="post-metadata">

**Author:** ![Garret\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Garret\_K](https://www.ruby-forum.com/u/Garret_K)\
**Post date:** [October 22, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/mixing-define-function-and-class-methods/117972/2 "2007-10-22T22:47:08Z")

</div>

On 10/22/07, Garret K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> some\_function from within an instance of B doesn’t work. Can anyone  
> point me in the right direction?

I’m not sure what you’re actually trying to accomplish, so I can’t  
really fix it but.

> class A  
> def self.some\_function(param)  
> puts “param: #{param}”  
> end

Okay, now you’ve got a class method named some\_function defined in A.

> # def some\_function(\*args)
> 
> # puts self
> 
> # B.some\_function(\*args)
> 
> # end

If you uncomment this it defines an instance method in A also called  
some\_function.

> ```
> def self.register(name, &block)
> define_method(name) do |*args|
> some_function("thing")
> block.call(args)
> end
> end
> 
> ```

This will define an instance method in A or a subclass, which is named  
whatever is passed in the name argument.

The body of this method will call the instance method some\_function on  
the receiver and then evaluate the block passed into register.

> ```
> def initialize
> puts "I'm a #{self.class}"
> end
> 
> ```
> 
> end
> 
> class B \< A  
> register :something do |\*args|  
> some\_function(“something”)  
> end  
> end

Now you call the register method you defined passing :something to be  
used as the name, this effectively is as if you’d coded:

class B  
def something(\*args)  
some\_function(“thing”)  
some\_function(“something”)} # although this would really  
evaluate the block if you ever got here  
end

> b = B.new  
> b.something

WIth the code commented out, you never defined an instance method  
some\_function.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
