n = 5
def num
puts n
end
為什么是錯的的?
我知道全局和局部基本原理。
局部變量在函數(shù)里只能使用,在外部是不存在。
但是在python或js里使用這樣方式可以執(zhí)行的!
還是只有ruby要在函數(shù)使用變量需要重新宣告。
請問各位可以解釋嗎?
謝謝
人生最曼妙的風景,竟是內(nèi)心的淡定與從容!
def opens a new scope
n = 5
local_variables # => [:n, :_]
def num
local_variables # => []
end
If you want to access n, you need to use flattening scope
define_method :num do
puts n
end
@n = 5
def num
puts @n
end
I am also a beginner, let’s see what other experts say.
But it can be executed using this method in python or js!
Read-only in Python
The following is a guess as to why Ruby did this?
In Ruby, the proc generated by def
生成的方法只能訪問全局變量。define_method
生成的方法 和 -> (param) {}
或 lambda { |param| }
has a closure and can access outer variables.
I guess it’s because Ruby thinks that the most common situation is that you don’t need to access outer variables, so def
doesn’t give you access. This has the advantage:
function
header, which is very verbose. As for lambdas with closures, that’s normal. Is it okay to use lambdas without closures?
As for define_method
, I guess it’s because
define_method
Because it is so-called metaprogramming, there is a greater chance of using outer variables. (Originally, using metaprogramming means being lazy and typing less. If you can’t use outer variables and have to pass them in as parameters, it will be exhausting.) So I use closed loops. Bag. define_method
have closures. define_method
除了 define_method (symbol) { |param| }
,還可以寫成 define_method(symbol, method)
,其中 method
比較常用的就是傳入一個 lambda,既然傳入 lambda,如果本身 define_method
In addition to define_method (symbol) { |param| }
, it can also be written as define_method(symbol, method)
, among which method
is more commonly used to pass Enter a lambda. Since lambda is passed in, if it does not have a closure, it will be very inconsistent, so let it do so. @Andrew_375683 is right.
Mainly it’s a matter of scope
But here is a simple way to solve it, just turn n into a function
def n
5
end
def num
p n
end
num
This question is very popular. I just went there and a few people answered me when I came back.
You think the n in your code is a global variable, but unfortunately it’s not
irb(main):006:0> x =10
=> 10
irb(main):009:0> defined? x
=> "local-variable"
If you need to modify it, just add $ as a prefix:
irb(main):010:0> $x = 1
=> 1
irb(main):011:0> defined? $x
=> "global-variable"
In the beautiful ruby ??style, $ is so eye-catching. The Ruby boss knows that global variables are indispensable, but he is also afraid of abuse, so he plays a little trick;). It involves the knowledge of ruby ??variables scope. The advantages of ruby ??are complex and unique.
@andrewzhyl Well said, like it.
Make a note: The difference between def and def_method: def is a keyword used to define a method; def_method is a method, and its function is to generate a new method. The code in def is completely isolated from the context (only @ and (beautiful variables); and define_method is more open and willing to meet friends without any modification.