ITeye博客

自定义的ruby struct怎么重载方法

FZtree · 2019-08-23 01:00 · Rubyist · 编程语言 · Markdown

set_1 = (01..11).to_a
set_2 = (12..22).to_a
set_3 = (23..33).to_a


# 23合并,随机取出3个
  # 随机20次
  # 20个结果做Comfirm
    # 不能都属于set_2 也不能都属于set_3
set = set_2 + set_3
Comfirm = Struct.new("Comfirm", :is_legal, :data) do
  def inspect
    "#{data.inspect} Comfirmed #{is_legal}"
  end
end


for i in 1..20
  data = set.shuffle(random:Random.new(i)).take(3).sort
  result = Comfirm.new('OK !', data) 
    result.is_legal = 'xx...'   if (data | set_2).size==11
    result.is_legal = 'xxx...'   if (data | set_3).size==11
  p result
end

p set

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。