# Can't get an Array function to be visible inside ApplicationHelper

**URL:** <https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317>\
**Category:** Rails\
**Created:** [April 12, 2009, 4:53am UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317 "2009-04-12T04:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skt](https://www.ruby-forum.com/u/skt)\
**Post date:** [April 12, 2009, 4:53am UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317/1 "2009-04-12T04:53:28Z")

</div>

Folks,

I seem to be missing something basic here.

I am trying to call a function from my application layout that will  
return me a hash of the roles for the user currently signed in. Here  
is the simplified code I have in ApplicationHelper module

class Array  
def to\_h  
Hash[\*enum\_with\_index.to\_a.flatten]  
end  
end

def get\_cur\_user\_roles  
[“school\_admin”, “General2”].to\_h  
end

I call get\_cur\_user\_roles from my layout file. When I bring up the web  
page I get an error that says

undefined method `to\_h’ for [“school\_admin”, “General2”]:Array

However, if I capture the same code in a test.rb file (adding a  
require ‘enumerator’ at the top of the file for the enum\_with\_index  
function) and run it with “ruby test.rb” it seems to work fine.

Any thoughts on why the code above is not seeing the Array instance  
method to\_h in the ApplicaitonHelper? I also tried changing it to  
class method by changing the declaration to self.to\_h but get the same  
error. Somehow the to\_h method is not visible to the [“school\_admin”,  
“General2”] Array and I don’t understand why. I have tried restarting  
the server but get the same error.

Thanks for help/pointers.

-S

---

<div class="post-metadata">

**Author:** ![skt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skt](https://www.ruby-forum.com/u/skt)\
**Post date:** [April 12, 2009, 9:53am UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317/2 "2009-04-12T09:53:13Z")

</div>

On Apr 12, 3:52 am, skt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Hash[*enum_with_index.to_a.flatten]
> 
> ```
> 
> undefined method `to\_h’ for [“school\_admin”, “General2”]:Array  
> the server but get the same error.

Because you write that inside the ApplicationHelper module, this is  
creating a new ApplicationHelper::Array class with those 2 methods. In  
you test.rb it’s not nested in a module. If you wrote your array  
extensions outside of the module you shouldn’t have this problem.  
(Personally I would put extensions to core classes in files in lib/  
and require them from initializers rather than dumping them in  
ApplicationHelper. I would also be wary of adding too many quite  
specialised methods to Array.

Fred

---

<div class="post-metadata">

**Author:** ![skt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skt](https://www.ruby-forum.com/u/skt)\
**Post date:** [April 12, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317/3 "2009-04-12T19:37:38Z")

</div>

> Because you write that inside the ApplicationHelper module, this is  
> creating a new ApplicationHelper::Array class with those 2 methods. In  
> you test.rb it’s not nested in a module. If you wrote your array  
> extensions outside of the module you shouldn’t have this problem.  
> (Personally I would put extensions to core classes in files in lib/  
> and require them from initializers rather than dumping them in  
> ApplicationHelper. I would also be wary of adding too many quite  
> specialized methods to Array.

Fred - I agree with the points you make about having a separate file in  
lib and being careful about adding too many specialized functions to  
core classes like Array. However I am not very clear on why adding a  
method to Array class inside ApplicationHelper module creates a new  
ApplicationHelper::Array class as opposed to opening the existing core  
Array class and adding the to\_h method to it. As I understood it, you  
could open a class anywhere and add methods to it. Sounds like there are  
caveats to this that I am not very clear on - would appreciate your help  
in clarifying that.

Thanks,  
-S

---

<div class="post-metadata">

**Author:** ![skt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skt](https://www.ruby-forum.com/u/skt)\
**Post date:** [April 13, 2009, 9:17am UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317/4 "2009-04-13T09:17:39Z")

</div>

> If you wanting this to instead refer to the top level Bar class then  
> you can write ::Bar, or in your case ::Array.

Thanks Fred - that was very helpful.

---

<div class="post-metadata">

**Author:** ![skt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skt](https://www.ruby-forum.com/u/skt)\
**Post date:** [April 12, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/cant-get-an-array-function-to-be-visible-inside-applicationhelper/165317/5 "2009-04-12T21:25:52Z")

</div>

On 12 Apr 2009, at 18:37, Sj Tib wrote:

> caveats to this that I am not very clear on - would appreciate your  
> help  
> in clarifying that.

You can reopen a class from anywhere. The way modules & scoping work  
in ruby means

module Foo  
class Bar  
end  
end

defines a new Foo::Bar class (and it’s important that you can do this,  
so that you can namespace your classes without trampling on other  
stuff).

If you wanting this to instead refer to the top level Bar class then  
you can write ::Bar, or in your case ::Array.

Fred
