• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby中容易犯的错误

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
• First and foremost, run your scripts with warnings enabled (the -w command-line
  option).
• If you happen to forget a “,” in an argument list—especially to print—you can
  produce some very odd error messages.
• A parse error at the last line of the source often indicates a missing end keyword,
  sometimes quite a bit earlier.
• An attribute setter is not being called. Within a class definition, Ruby will parse
  setter= as an assignment to a local variable, not as a method call. Use the form
  self.setter= to indicate the method call.
        class Incorrect
          attr_accessor :one, :two
          def initialize
             one = 1           # incorrect - sets local variable
             self.two = 2
          end
        end
        obj = Incorrect.new
        obj.one    → nil
        obj.two    →2
• Objects that don’t appear to be properly set up may have been victims of an incor-
  rectly spelled initialize method.
        class Incorrect
          attr_reader :answer
          def initialise           # < < < spelling error
             @answer = 42
          end
        end
        ultimate = Incorrect.new
        ultimate.answer       → nil
  The same kind of thing can happen if you misspell the instance variable name.

        class Incorrect
          attr_reader :answer
          def initialize
            @anwser = 42        #<« spelling error
          end
        end
        ultimate = Incorrect.new
        ultimate.answer    → nil
• Block parameters are in the same scope as local variables. If an existing local
  variable with the same name as a block parameter exists when the block executes,
  that variable will be modified by the call to the block. This may or may not be a
  Good Thing.
        c = "carbon"
        i = "iodine"
        elements = [ c, i ]
        elements.each_with_index do |element, i|
          # do some chemistry
        end
        c   → "carbon"
        i   →1
• Watch out for precedence issues, especially when using {} instead of do/end.
        def one(arg)
          if block_given?
            "block given to 'one' returns #{yield}"
          else
            arg
          end
        end
        def two
          if block_given?
            "block given to 'two' returns #{yield}"
          end
        end
        result1 = one two {
          "three"
        }
        result2 = one two do
          "three"
        end
        puts "With braces, result = #{result1}"
        puts "With do/end, result = #{result2}"
  produces:
        With braces, result = block given to 'two' returns three
        With do/end, result = block given to 'one' returns three

• Output written to a terminal may be buffered. This means you may not see a mes-
  sage you write immediately. In addition, if you write messages to both $stdout
  and $stderr, the output may not appear in the order you were expecting. Always
  use nonbuffered I/O (set sync=true) for debug messages.
• If numbers don’t come out right, perhaps they’re strings. Text read from a file will
  be a String and will not be automatically converted to a number by Ruby. A call
  to Integer will work wonders (and will throw an exception if the input isn’t a
  well-formed integer). A common mistake Perl programmers make is
        while line = gets
          num1, num2 = line.split(/,/)
          # ...
        end
  You can rewrite this as
        while line = gets
          num1, num2 = line.split(/,/)
          num1 = Integer(num1)
          num2 = Integer(num2)
          # ...
        end
  Or, you could convert all the strings using map.
        while line = gets
          num1, num2 = line.split(/,/).map {|val| Integer(val) }
          # ...
        end
• Unintended aliasing—if you are using an object as the key of a hash, make sure it
  doesn’t change its hash value (or arrange to call Hash#rehash if it does).
        arr = [1, 2]
        hash = { arr   => "value" }
        hash[arr]       → "value"
        arr[0] = 99
        hash[arr]            nil
                        →
        hash.rehash          {[99, 2]=>"value"}
                        →
        hash[arr]            "value"
                        →
• Make sure the class of the object you are using is what you think it is. If in doubt,
  use puts my_obj.class.
• Make sure your method names start with a lowercase letter and class and constant
  names start with an uppercase letter.
• If method calls aren’t doing what you’d expect, make sure you’ve put parentheses
  around the arguments.

• Make sure the open parenthesis of a method’s parameter list butts up against the
  end of the method name with no intervening spaces.
• Use irb and the debugger.
• Use Object#freeze. If you suspect that some unknown portion of code is setting
  a variable to a bogus value, try freezing the variable. The culprit will then be
  caught during the attempt to modify the variable.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ruby -- 基础学习(三)设置中国时区时间发布时间:2022-07-14
下一篇:
一步一步学Ruby(二十):文件使用发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap