在类中定义一个方法,用于创建类的实例

时间:2016-06-07 06:07:54

标签: ruby class hashmap

我正在编写一个模仿 <....> private missionAnnouncedSource = new Subject<string>(); private missionConfirmedSource = new Subject<string>(); // Observable string streams missionAnnounced$ = this.missionAnnouncedSource.asObservable(); missionConfirmed$ = this.missionConfirmedSource.asObservable(); <....> 班级的班级MyHashSet。集合的元素包含为读取Set的哈希项。以下是它的定义方式:

{element => true}

最后一个方法class MyHashSet attr_accessor :store def initialize @store = {} end def insert(el) @store.merge!(el => true) end def include?(el) @store[el] end def delete(el) @store.select! {|key,value| key != el} end def to_a @store.keys end def self.union(set) result=MyHashSet.new result.store=(self.store).merge(set.store) result end end 应该是这样的,如果我输入命令:

union

然后尝试计算

set1=MyHashSet.new
set2=MyHashSet.new

set1.insert("Mark Hamill")
set1.insert("Harrison Ford")
set1.insert("Anthony Daniels")

set2.insert("Ewan McGregor")
set2.insert("Natalie Portman")
set2.insert("Anthony Daniels")

我应该得到一个set3=set1.union(set2) 变量,这是set3的一个实例,使其MyHashSet为:

store

但是,如果我尝试运行此操作,则会收到{"Mark Hamill"=>true, "Harrison Ford"=>true, "Anthony Daniels"=>true, "Ewan McGregor"=>true, "Natalie Portman"=>true} 错误消息:

undefined method

我不明白为什么Ruby不选择这种方法。

2 个答案:

答案 0 :(得分:0)

您必须像实例方法一样使用它而不是类方法。您可以使用self.class.new

MyHashSet.new内容 像这样

class MyHashSet
  attr_accessor :store
  def initialize
    @store = {}
  end

  def insert(el)
    @store.merge!(el => true)
  end

  def include?(el)
    @store[el]
  end

  def delete(el)
    @store.select! {|key,value| key != el}
  end

  def to_a
    @store.keys
  end

  def union(set)
    result = self.class.new
    result.store = self.store.merge(set.store)
    result
  end
end

输出

set1=MyHashSet.new
# => #<MyHashSet:0x1efea79 @store={}>
set2=MyHashSet.new
# => #<MyHashSet:0x34c75a @store={}>

set1.insert("Mark Hamill")
# => {"Mark Hamill" => true}
set1.insert("Harrison Ford")
# => {"Mark Hamill" => true, "Harrison Ford" => true}
set1.insert("Anthony Daniels")
# => {"Mark Hamill" => true, "Harrison Ford" => true, "Anthony Daniels" => true}

set2.insert("Ewan McGregor")
# => {"Ewan McGregor" => true}
set2.insert("Natalie Portman")
#=> {"Ewan McGregor" => true, "Natalie Portman" => true}
set2.insert("Anthony Daniels")
# => {"Ewan McGregor" => true, "Natalie Portman" => true, "Anthony Daniels" => true}

set3 = set1.union(set2)
# => #<MyHashSet:0x1c7cbad @store={"Mark Hamill"=>true, "Harrison Ford"=>true, "Anthony Daniels"=>true, "Ewan McGregor"=>true, "Natalie Portman"=>true}>

顺便说一句:

也许您可以修改initialize方法,以便更好地使用

def initialize(store = nil)
  @store = store || {}
end

在此之后,您可以轻松地致电union

def union(set)
  self.class.new(self.store.merge(set.store))
end

所以你的最终分类看起来像这样

class MyHashSet
  attr_accessor :store

  def initialize(store = nil)
    @store = store || {}
  end

  def insert(el)
    @store.merge!(el => true)
  end

  def include?(el)
    @store[el]
  end

  def delete(el)
    @store.select! { |key, value| key != el }
  end

  def to_a
    @store.keys
  end

  def union(set)
    self.class.new(self.store.merge(set.store))
  end
end

答案 1 :(得分:-1)

我发现代码可以正常工作,如果我删除&#34; self。&#34;在&#34; union&#34;:

之前
class MyHashSet
  attr_accessor :store
  def initialize
    @store = {}
  end

  def insert(el)
    @store.merge!(el => true)
  end

  def include?(el)
    @store[el]
  end

  def delete(el)
    @store.select! {|key,value| key != el}
  end

  def to_a
    @store.keys
  end

  def union(set)
    result=MyHashSet.new
    result.store=(self.store).merge(set.store)
    result
  end
end

我想这是因为该方法是在类的实例上而不是在类本身上调用的。