# Calling a class method from an extended object

**URL:** <https://www.ruby-forum.com/t/calling-a-class-method-from-an-extended-object/84241>\
**Category:** Ruby\
**Created:** [February 2, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/calling-a-class-method-from-an-extended-object/84241 "2007-02-02T17:55:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbeausoleil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@fbeausoleil](https://www.ruby-forum.com/u/fbeausoleil)\
**Post date:** [February 2, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/calling-a-class-method-from-an-extended-object/84241/1 "2007-02-02T17:55:54Z")

</div>

Hi all !

I’m on Rails, and I have a general Ruby question. Here’s a part of my  
code:

class MockControllerBase; end

module AuthenticatedSystem  
def self.included(base)  
base.send :include, AuthenticatedSystem::InstanceMethods

```
base.send :extend, AuthenticatedSystem::ClassMethods

```

end

module ClassMethods  
def required\_permissions(options={})  
logger.debug  
{“AuthenticatedSystem#required\_permissions(#{options.inspect})”}  
end  
end

module InstanceMethods  
def authorized?  
false  
end  
end  
end

class AuthenticatedSystemAuthorizationTest \< Test::Unit::TestCase  
def setup  
class \<\< @controller = MockControllerBase.new  
include AuthenticatedSystem  
end  
end

def test\_wide\_open\_lets\_any\_action\_go\_through  
@controller.class.required\_permissions true

```
@params[:action] = "create"
assert @controller.authorized?

```

end  
end

I cannot seem to call #required\_permissions after it was included.  
How should I access it ?

Both  
@controller.class.required\_permissions true

and  
@controller.required\_permissions true

raise a NoMethodError, with a different object.

Should I refactor, or do things differently ?

Thanks !

---

<div class="post-metadata">

**Author:** ![fbeausoleil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@fbeausoleil](https://www.ruby-forum.com/u/fbeausoleil)\
**Post date:** [February 2, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/calling-a-class-method-from-an-extended-object/84241/2 "2007-02-02T18:11:56Z")

</div>

Hi –

On Sat, 3 Feb 2007, Francois B. wrote:

> base.send :extend, AuthenticatedSystem::ClassMethods  
> def authorized?  
> end  
> How should I access it ?
> 
> Both  
> @controller.class.required\_permissions true
> 
> and  
> @controller.required\_permissions true
> 
> raise a NoMethodError, with a different object.

You’ve included the module in the singleton class of @controller,  
which is neither @controller nor @controller.class. So  
required\_permissions is a method of what would be  
@controller.singleton\_class if this method existed:

class Object  
def singleton\_class  
class \<\< self  
self  
end  
end  
end

🙂

> Should I refactor, or do things differently ?

I would do it differently, because I’ve never been a fan of the  
InstanceMethods/ClassMethods thing. I prefer just to include or  
extend separate modules as needed and where needed. But I know it’s  
very popular.

David

---

<div class="post-metadata">

**Author:** ![fbeausoleil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@fbeausoleil](https://www.ruby-forum.com/u/fbeausoleil)\
**Post date:** [February 2, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/calling-a-class-method-from-an-extended-object/84241/3 "2007-02-02T18:35:01Z")

</div>

Hi !

2007/2/2, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I would do it differently, because I’ve never been a fan of the  
> InstanceMethods/ClassMethods thing. I prefer just to include or  
> extend separate modules as needed and where needed. But I know it’s  
> very popular.

Here’s what I did instead:  
class AuthenticatedSystemAuthorizationTest \< Test::Unit::TestCase  
class Controller \< MockControllerBase  
include AuthenticatedSystem  
end

def setup  
@controller = Controller.new  
end  
end

Thank you, David, for your help !
