# How can I call foo in Bar.foo?

**URL:** <https://www.ruby-forum.com/t/how-can-i-call-foo-in-bar-foo/190057>\
**Category:** Ruby\
**Created:** [June 12, 2010, 7:33am UTC](https://www.ruby-forum.com/t/how-can-i-call-foo-in-bar-foo/190057 "2010-06-12T07:33:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntysdd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/71e660/32.png) [@ntysdd](https://www.ruby-forum.com/u/ntysdd)\
**Post date:** [June 12, 2010, 7:33am UTC](https://www.ruby-forum.com/t/how-can-i-call-foo-in-bar-foo/190057/1 "2010-06-12T07:33:47Z")

</div>

it’s like this  
…  
module Bar  
def foo  
puts ‘hello’  
end  
def bye  
puts ‘bye’  
end  
end

def Bar.foo  
#Do something  
#I want to call foo now  
#And call bye  
#…  
end  
…

is there anything i can to to solve the problem

---

<div class="post-metadata">

**Author:** ![ntysdd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/71e660/32.png) [@ntysdd](https://www.ruby-forum.com/u/ntysdd)\
**Post date:** [June 12, 2010, 8:23am UTC](https://www.ruby-forum.com/t/how-can-i-call-foo-in-bar-foo/190057/2 "2010-06-12T08:23:40Z")

</div>

On Sat, Jun 12, 2010 at 12:33 AM, Ntys Dd [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I won’t advocate this as a solution, but it does seem to meet your  
criterion.

module Bar  
def foo  
puts ‘hello’  
end  
def bye  
puts ‘bye’  
end  
end

# Extends Bar’s singleton class with its own instance methods

p (class \<\< Bar ; self ; end).ancestors  
Bar.extend Bar  
p (class \<\< Bar ; self ; end).ancestors

# since Bar is now an ancestor of itself, we can call super from within

class methods  
def Bar.foo  
puts “doing something”  
super  
bye  
end

Bar.foo

---

<div class="post-metadata">

**Author:** ![ntysdd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/71e660/32.png) [@ntysdd](https://www.ruby-forum.com/u/ntysdd)\
**Post date:** [June 12, 2010, 11:00am UTC](https://www.ruby-forum.com/t/how-can-i-call-foo-in-bar-foo/190057/3 "2010-06-12T11:00:29Z")

</div>

On 06/12/2010 07:33 AM, Ntys Dd wrote:

> def Bar.foo  
> #Do something  
> #I want to call foo now  
> #And call bye  
> #…  
> end  
> …
> 
> is there anything i can to to solve the problem

It does not really make sense since the first #foo you define is an  
instance method while the latter is a class method. When you have a  
class you do not have an instance available so on what instance would  
you intend to invoke #foo on? It can only work the other way round: you  
have the instance method invoking the class (module) method.

module Bar  
def self.foo  
puts “module method”  
end

def foo  
puts “instance method”  
Bar.foo  
end  
end

irb(main):011:0\> o = Object.new.extend Bar  
=\> #Object:0x910a418  
irb(main):012:0\> o.foo  
instance method  
module method  
=\> nil

Of course you can also do

module Bar  
def uber\_foo  
foo  
bye  
end  
end

Kind regards

robert
