ruby -- 全局和局部问题
黄舟
黄舟 2017-04-22 08:59:49
0
6
696
n = 5 def num puts n end

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

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

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all (6)
黄舟

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 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.
        伊谢尔伦

        @Andrew_375683 is right.
        Mainly it’s a matter of scope
        But here is a simple way to solve it, just turnninto 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.

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!