3-18/19 (自我练习)30多个《Ruby元编程》的spell(pattern)小例子。

时间:2023-03-09 01:07:09
3-18/19 (自我练习)30多个《Ruby元编程》的spell(pattern)小例子。

Spell,也称pattern,idiom


# Around Alias:从一个重新定义的方法中调用原始的,被重命名的版本。
# old_reverse是未改变的原始方法,reverse/new_reverse是改变的方法。
class String
  def new_reverse
    "x:#{old_reverse}"
  end
  alias_method :old_reverse, :reverse
  alias_method :reverse, :new_reverse
end
puts "abc".reverse    #x:cba

# Blank Slate:移除一个对象的所有方法,以便把它们转换成ghost method
class C
  def method_missing(name, *args)
    'a Ghost Method'
  end
  p ancestors      #[C, Object, Kernel, BasicObject]
end
puts C.new.to_s  #返回一个string记录了对象的类和encoding-id#<C:0x00007f9ef0006950>
class D < BasicObject
  def method_missing(name, *args)
    "a Ghost Method"
  end
end
blank_slate = D.new
puts blank_slate.to_s    #返回method_missing方法的return: a Ghost Method

# Class Extension: 通过向singleton class中加入Module来定义class method,是对象扩展的一个特例
class C; end
module M
  def my_method
    'a class method'
  end
end
+ 1) # prints "method"
# Self Yield: 把self传给当前block
class Person
  attr_accessor :name, :surname
  def initialize
    yield self
  end
end
joe = Person.new do |p|
  p.name = 'Joe'
  p.surname = 'Smith'
end
p joe #=>#<Person:0x00007fdc560042b0 @name="Joe", @surname="Smith">

Ruby中有不少方法也是自动把receiver传入块。如:instance_eval ,class_eval, Object#tap


# Singleton Method:

给一个对象定义一个单例方法,也可以extend一个模块。

class Klass
end
k = Klass.new
class << k
  def hello
      "Hello from Mod.\n"
  end
end
p k.hello

# String of code

执行一段表示Ruby代码的字符串

my_string_of_code = "1+1"
p eval(my_string_of_code) #=》2

# Symbol To Proc
# 把一个调用单个方法的块转换成一个符号,一样的写法,目的是更省事

p [1,2,3].map{|x| x.even? }

p [1,2,3].map &:even?.to_proc   #变成proc对象,然后再用&变成代码块

p [1,2,3].map(&:even?)

#=> [false, true, false]