Local variables for a class in ruby

I noticed that the following code is syntactically correct:

class Foo bar = 3 end 

Now I know that instance variables are accessed by @ and class variables @@ , but I could not understand where bar is stored in this case or how to access it.

How can I find the bar area?

+5
source share
4 answers

The body of a class in Ruby is just Ruby executable code. These are really local variables (no need for quotation) and follow the "regular" rules, which are local variables. You can access them in the body of the class. If you literally want the area in which bar defined, you can use Kernel.binding :

 class Foo bar = 42 @@scope = binding def self.scope @@scope end end Foo.scope.local_variables # => [:bar] Foo.scope.local_variable_get(:bar) # => 42 

Note: using def changes the scope, so they will not be visible inside the methods defined with def .

+7
source

It is available from the same class.

 class Foo bar = 3 bar # => 3 end 

It is lexically limited, so it is accessible from within the block:

 class Foo bar = 3 pr = ->{p bar} pr.call # => 3 end 

but it is not available even in the same class when the class body was closed:

 class Foo bar = 3 end class Foo bar # => error end 

and cannot be accessed from the method definition:

 class Foo bar = 3 def baz; bar end new.baz # => error end 
+4
source

The bar variable will be available until the class definition is closed. It will not be available inside the methods you define.

You can try running the code in irb:

 $ irb irb(main):001:0> class Test irb(main):002:1> bar = 1 irb(main):003:1> puts bar irb(main):004:1> end 1 => nil irb(main):005:0> puts bar NameError: undefined local variable or method `bar' for main:Object from (irb):5 from /usr/bin/irb:11:in `<main>' irb(main):006:0> class Test irb(main):007:1> puts bar irb(main):008:1> end NameError: undefined local variable or method `bar' for Test:Class from (irb):7:in `<class:Test>' from (irb):6 from /usr/bin/irb:11:in `<main>' irb(main):009:0> 

Check for instance in methods:

 irb(main):018:0> class Test irb(main):019:1> bar = 1 irb(main):020:1> def test irb(main):021:2> puts bar irb(main):022:2> end irb(main):023:1> end => :test irb(main):024:0> a = Test.new => #<Test:0x00000000f447a0> irb(main):025:0> a.test NameError: undefined local variable or method `bar' for #<Test:0x00000000f447a0> from (irb):21:in `test' from (irb):25 from /usr/bin/irb:11:in `<main>' 

Check for the presence in the class methods:

 irb(main):026:0> class Test irb(main):027:1> bar = 1 irb(main):028:1> def self.test irb(main):029:2> puts bar irb(main):030:2> end irb(main):031:1> end => :test irb(main):032:0> Test.test NameError: undefined local variable or method `bar' for Test:Class from (irb):29:in `test' from (irb):32 from /usr/bin/irb:11:in `<main>' 
+2
source

You can make it a constant and use its instance and class methods:

 class Foo Bar = 3 def local_bar(param = Bar) param end end p Foo.new.local_bar #=> 3 
0
source

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


All Articles