ruby - a more elegant way to do this? -
Sorry I have not made the title very clear, but I can not honestly work on how to better my question ; If someone can think of a better title, please change it in all ways.
Take a look at the following example:
module Fu DF method (value) Bar.instance_exec (self.method (value)) End class bar def self.method (value ) The print value is the end-end end that is the only way to create bar method foo namespace Is available within the bar without the need to call or is there any more beautiful way to do this? Maybe method_milk can work, but again, a little cumbersome. Edit: Forward Example
Module Fu Class SomeClassController & lt; At the end of the ApplicationController def index method (@User.Id) Bar.method (@ user.id) I am creating an engine but I do not have to do this every time I want to use the method which is available everywhere, the main namespace ( Foo in this example), as in the case of more utility methods, but they have the engine < There is a need to be done within the scope of / code>. EDIT: Exits I was not using the code correctly
Module Fu DEF method (value) Return Bar.instance_exec (value) {| Wei | (Value + 59) return self.method (value)} End class bar def self.method (value) return end end method inside the block of instance_exec Returns out of it so that def method inside module foo will return the correct value. Edit: I know that this is kind of unclear but trust me when I say that instance_exec is what I was searching for, he and Example_exec takes actions outside its execution block
The answer to my problem is as follows That uses a rail engine to add more sensible contexts as an example
engine. Rb:
module Fu class engine class Some class def method (value) return (value + 59) end and end other actions. Rb
module Foo def method (value) return Engine.instance_exec (value) {| V | Return Self :: SomeClass.method (v)} End End Engine_Controller.rb
Module Fu Square EngineController & LT; ApplicationController.rb def index @ user.some_field = method (parameter [: user_input]) end end end I think that should work well, I know all the use of For the top method at least DIF index, , I'm not sure, but the main point of the question has been answered within other actions. RB
Comments
Post a Comment