How to access class variables in ruby ​​enabled modules?

I need to know whether it is possible to include included ruby ​​modules in accessing class variables. Let's say:

require 'bar' class Foo @i_am_important Bar.do_stuff end Module Bar def Bar.do_stuff @i_am_important.stuff... end end 

Is there a way to do the work above?

edit: improved example, edit2: fixed problem

I just changed my approach: Bar became its own class and passed in "i_am_important", which was passed during initialization. It may not be the best solution, but it works at last. Thanks for the help.

+5
source share
3 answers

You can enable the module inside the class to access, for example

  module MyModule @@my_val = 4 end class MyClass include MyModule value = @@my_val end 
0
source

Why do you want to use a variable through the class and module gates? I think there is a way:

 module Bar def do_stuff puts im_am_important end end class Foo include Bar def im_am_important 100 end end Foo.new.do_stuff # => 100 
0
source

What about:

 #foo.rb @var module My_foo var = @var def My_foo.my_method(var) puts(var) end end 

 #bar.rb require 'foo' class Bar extend My_foo @important_var = "bla" My_foo.my_method(@important_var) end 

ruby bar.rb => bla

0
source

Source: https://habr.com/ru/post/1383030/


All Articles