On Apr 12, 3:52 am, skt <[email protected]> wrote:
> 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.
>
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
> Thanks for help/pointers.
>
> -S
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---