Home  >  Q&A  >  body text

ruby -- 全局和局部问题

n = 5
def num
  puts n
end

为什么是错的的?
我知道全局和局部基本原理。
局部变量在函数里只能使用,在外部是不存在。

但是在python或js里使用这样方式可以执行的!
还是只有ruby要在函数使用变量需要重新宣告。

请问各位可以解释吗?
谢谢

黄舟黄舟2761 days ago752

reply all(6)I'll reply

  • 黄舟

    黄舟2017-04-22 09:01:49

    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
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-22 09:01:49

    @n = 5
    def num
        puts @n
    end
    

    I am also a beginner, let’s see what other experts say.

    reply
    0
  • 迷茫

    迷茫2017-04-22 09:01:49

    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:

    • 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 the function header, 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 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.
    • It is sometimes inconvenient that the
    • method definition cannot have closures, but it is usually not necessary, so let the longer 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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-22 09:01:49

    @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
    

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-22 09:01:49

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-22 09:01:49

    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.

    reply
    0
  • Cancelreply