Project1

标题: 大家来讨论一下 attr_accessor 的用法吧 [打印本页]

作者: ky52879    时间: 2012-3-11 10:41
标题: 大家来讨论一下 attr_accessor 的用法吧
近来无事,研究代码时候发下一个奇怪的现象,先看这个脚本:
RUBY 代码复制
  1. class A
  2.   attr_accessor :size
  3.   #--------------------------------------------------------------------------
  4.   # ● 初始化对像
  5.   #--------------------------------------------------------------------------
  6.   def initialize
  7.     @size = 0
  8.   end
  9.   def size
  10.     return @size / 2
  11.   end
  12. end
  13. $a = A.new
  14. $a.size += 1
  15. $a.size += 1
  16. $a.size += 1
  17. $a.size += 1
  18. $a.size += 1
  19. p $a.size

这个最后输出数字竟然是 0 {:1_29:}@ ??
这是为什么呢?
理论上应该是2.5才对啊。。

作者: orzfly    时间: 2012-3-11 11:21
把/2改成/2.0先
作者: fux2    时间: 2012-3-11 13:25
同意楼上,应该是整形和浮点的问题
作者: orzfly    时间: 2012-3-11 15:13
本帖最后由 orzfly 于 2012-3-11 15:15 编辑

@fux2@ky52879@晴兰

let's trace
RUBY 代码复制
  1. $sm=[]
  2. class Class
  3.   def sm(sym)
  4.     self.class_eval do
  5.       return if (@@__smed||=[]).include?(sym)
  6.       @@__smed.push sym
  7.       alias :"old_#{sym}" :"#{sym}"
  8.       define_method sym do |*args, &block|
  9.         $sm << [self, sym, *args]
  10.         old = $sm[-1]
  11.         old.push self.send(:"old_#{sym}", *args, &block)
  12.         old[-1]
  13.       end
  14.     end
  15.   end
  16. end
  17. $sm=[]
  18.  
  19. class A
  20.   attr_accessor :size
  21.   def initialize
  22.     @size = 0
  23.   end
  24.   def size
  25.     return @size / 2
  26.   end
  27.   sm :size
  28.   sm :size=
  29. end
  30. $a = A.new
  31. $a.size += 1
  32. $a.size += 1
  33. $a.size += 1
  34. $a.size += 1
  35. $a.size += 1
  36. p $a.size
  37. puts "\nsm result: "
  38. puts $sm.map { |j| j.join "\t" }.join "\n"


the result of /2 is
  1. sm result:
  2. #<A:0x1a4c588>        size        0
  3. #<A:0x1a4c588>        size=        1        1
  4. #<A:0x1a4c588>        size        0
  5. #<A:0x1a4c588>        size=        1        1
  6. #<A:0x1a4c588>        size        0
  7. #<A:0x1a4c588>        size=        1        1
  8. #<A:0x1a4c588>        size        0
  9. #<A:0x1a4c588>        size=        1        1
  10. #<A:0x1a4c588>        size        0
  11. #<A:0x1a4c588>        size=        1        1
  12. #<A:0x1a4c588>        size        0
复制代码
the result of /2.0 is

  1. sm result:
  2. #<A:0x169c578>        size        0.0
  3. #<A:0x169c578>        size=        1.0        1.0
  4. #<A:0x169c578>        size        0.5
  5. #<A:0x169c578>        size=        1.5        1.5
  6. #<A:0x169c578>        size        0.75
  7. #<A:0x169c578>        size=        1.75        1.75
  8. #<A:0x169c578>        size        0.875
  9. #<A:0x169c578>        size=        1.875        1.875
  10. #<A:0x169c578>        size        0.9375
  11. #<A:0x169c578>        size=        1.9375        1.9375
  12. #<A:0x169c578>        size        0.96875
复制代码
由此可知 size += 1 被翻译成了 size = size + 1 来执行




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1