📄 12 - undefining a method.rb
字号:
class RandomizingArray < Array def <<(e) insert(rand(size), e) end def [](i) super(rand(size)) endenda = RandomizingArray.newa << 1 << 2 << 3 << 4 << 5 << 6 # => [6, 3, 4, 5, 2, 1]# That was fun; now let's get some of those entries back.a[0] # => 1a[0] # => 2a[0] # => 5#No, seriously, a[0].a[0] # => 4#It's a madhouse! A madhouse!a[0] # => 3#That does it!class RandomizingArray remove_method('[]')enda[0] # => 6a[0] # => 6a[0] # => 6# But the overridden << operator still works randomly:a << 7 # => [6, 3, 4, 7, 5, 2, 1]#---class RandomizingArray remove_method(:length)end# NameError: method `length' not defined in RandomizingArrayclass RandomizingArray undef_method(:length)endRandomizingArray.new.length # NoMethodError: undefined method `length' for []:RandomizingArrayArray.new.length # => 0#---my_array = Array.newdef my_array.random_dump(number) number.times { self << rand(100) }endmy_array.random_dump(3)my_array.random_dump(2)my_array # => [6, 45, 12, 49, 66]# That's enough of that.class << my_array remove_method(:random_dump)endmy_array.random_dump(4)# NoMethodError: undefined method `random_dump' for [6, 45, 12, 49, 66]:Array#---class OneTimeContainer def initialize(value) @use_just_once_then_destroy = value end def get_value remove_instance_variable(:@use_just_once_then_destroy) endendobject_1 = OneTimeContainer.new(6)object_1.get_value# => 6object_1.get_value# NameError: instance variable @use_just_once_then_destroy not definedobject_2 = OneTimeContainer.new('ephemeron')object_2.get_value# => "ephemeron"#---class MyClass remove_instance_variable(:@use_just_once_then_destroy) end# NameError: instance variable @use_just_once_then_destroy not defined#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -