# Self.class in view rhtml

**URL:** <https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745>\
**Category:** Rails\
**Created:** [August 10, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745 "2007-08-10T12:40:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/1 "2007-08-10T12:40:16Z")

</div>

Hi all, very much new to Ruby and RoR - a very steep learning curve  
but exciting stuff!

I’m just playing around with some things - I have a LoginController  
which has an index.rhtml view file. If I insert \<%= self.inspect %\>  
into this I get LOADS of not-very-readable stuff, so I tried \<%=  
self.class %\> but this just gives me something like “#\<Class:  
0x4864ecc\>” - why doesn’t this class have a name?

Am I right in thinking that ‘self’ in a view is the calling  
controller?

Thanks for any help!  
–rob

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/2 "2007-08-10T12:59:21Z")

</div>

\<%= self.class.superclass %\> gives ActionView::Base.

self in a view is not calling the controller.

-Jatinder

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/3 "2007-08-10T13:01:21Z")

</div>

Thanks Jatinder,

That makes more sense for it to be a view object.  
Do you know why the actual class seems to have no name?

–rob

On 8/10/07, Jatinder S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 8/10/07, [rob desbois] [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> >

–  
Rob Desbois  
Eml: removed\_email\_address@domain.invalid  
Tel: 01452 760631  
Mob: 07946 705987  
“There’s a whale there’s a whale there’s a whale fish” he cried, and the  
whale was in full view.  
…Then ooh welcome. Ahhh. Ooh mug welcome.

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/4 "2007-08-10T13:46:28Z")

</div>

Great thanks for that; I wasn’t aware you could create anonymous classes  
in  
Ruby.  
–rob

On 8/10/07, Jatinder S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > 0x4864ecc\>" - why doesn’t this class have a name?
> > 
> > “There’s a whale there’s a whale there’s a whale fish” he cried, and the  
> > whale was in full view.  
> > …Then ooh welcome. Ahhh. Ooh mug welcome.
> 
> >

–  
Rob Desbois  
Eml: removed\_email\_address@domain.invalid  
Tel: 01452 760631  
Mob: 07946 705987  
“There’s a whale there’s a whale there’s a whale fish” he cried, and the  
whale was in full view.  
…Then ooh welcome. Ahhh. Ooh mug welcome.

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/5 "2007-08-10T13:42:26Z")

</div>

No clue about the thought behind the actual class having no name.

Following is a snippet from  
actionpack-1.13.3\lib\action\_controller/base.rb  
[http://pastie.caboo.se/86581](http://pastie.caboo.se/86581)

view\_class(extending ActionView:Base) gets associated with a controller.

-Jatinder

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/6 "2007-08-10T13:50:28Z")

</div>

Try \<%= controller.inspect %\>

On 8/10/07, [rob desbois] [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I right in thinking that ‘self’ in a view is the calling  
> controller?
> 
> Thanks for any help!  
> –rob
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/7 "2007-08-10T14:48:38Z")

</div>

On 8/10/07, [rob desbois] [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I right in thinking that ‘self’ in a view is the calling  
> controller?

self.inspect is probably inspecting things like the request, response,  
and controller objects. There may be some duplication, because  
controller.inspect probably shows the request and response objects  
too.

The view classes are created dynamically with the necessary helpers  
mixed in.

[http://dev.rubyonrails.org/browser/tags/rel\_1-2-3/actionpack/lib/action\_controller/base.rb#L1028](http://dev.rubyonrails.org/browser/tags/rel_1-2-3/actionpack/lib/action_controller/base.rb#L1028)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![SSrob\_desbois](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SSrob\_desbois](https://www.ruby-forum.com/u/SSrob_desbois)\
**Post date:** [August 10, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/self-class-in-view-rhtml/111745/8 "2007-08-10T15:08:42Z")

</div>

Hi –

On Fri, 10 Aug 2007, Rob Desbois wrote:

> Thanks Jatinder,
> 
> That makes more sense for it to be a view object.

Of course, that raises the question: how can this view object share  
instance variables with the controller object, since instance  
variables by definition are always the property of self, and there are  
two different selfs here?

I know you didn’t ask, but the answer is kind of interesting 🙂

The controller’s instance variables have to be explicitly transferred  
to the view:

def assign\_variables\_from\_controller  
@assigns.each { |key, value| instance\_variable\_set(“@#{key}”,  
value) }  
end

where @assigns is the controller’s instance variables.

So the whole thing where, say, @user is “shared” between the  
controller and the view is based on this sleight of hand. It’s almost  
the opposite of what instance variables, as such, are actually for,  
but it provides a very nice sense of continuity and connection between  
the controller code and the view.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
