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 bydef生成的方法只能访问全局变量。 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, sodefdoesn’t give you access. This has the advantage:
Won’t mess with outer variables unintentionally like js. In order to avoid this in JavaScript, all variables used in the body must be undefined in thefunctionheader, which is very verbose.
No need to create closures, saving overhead.
As for lambdas with closures, that’s normal. Is it okay to use lambdas without closures?
As fordefine_method, I guess it’s because
define_methodBecause 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.
It is sometimes inconvenient that the
method definition cannot have closures, but it is usually not necessary, so let the longerdefine_methodhave closures.
define_method除了define_method (symbol) { |param| },还可以写成define_method(symbol, method),其中method比较常用的就是传入一个 lambda,既然传入 lambda,如果本身define_methodIn addition todefine_method (symbol) { |param| }, it can also be written asdefine_method(symbol, method), among whichmethodis 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.
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.
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.
def opens a new scope
If you want to access n, you need to use flattening scope
I am also a beginner, let’s see what other experts say.
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 becausedefine_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 todefine_method (symbol) { |param| }
, it can also be written asdefine_method(symbol, method)
, among whichmethod
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 turnninto a function
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
If you need to modify it, just add $ as a prefix:
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.