需要什么方法才能有“-”? (减法) 方法与 Ruby 数组一起使用吗?

发布于 2024-08-13 13:06:37 字数 913 浏览 3 评论 0原文

如果我有两个数组 ab,那么所包含的对象应该重写什么方法,以便减法方法 - 正常工作?

eql 就足够了吗?

编辑

我正在为我的问题添加更多细节。

我定义了这个类:

class Instance
    attr_reader :id, :desc 
    def initialize( id ,  desc  )
        @id = id.strip
        @desc = desc.strip
    end

    def sameId?( other )
        @id == other.id
    end

    def eql?( other )
        sameId?( other ) and @desc == other.desc
    end

    def to_s()
        "#{id}:#{desc}"
    end
end

好吗?

然后我从不同部分填充了两个数组,我想得到差异。

a = Instance.new("1","asdasdad")
b = Instance.new("1","a")
c = Instance.new("1","a")

p a.eql?(b) #false
p b.eql?(c) #true 

x = [a,b]
y = [c]

z = x - y # should leave a because b and c "represent" the same object

但这不起作用,因为 ab 被保存在数组中。我想知道我需要在类中重写什么方法才能正常工作。

If I have two arrays a and b, what method should the object contained have to override so the subtract method - works properly?

Is it enough with eql?

EDIT

I'm adding more detail to my question.

I have this class defined:

class Instance
    attr_reader :id, :desc 
    def initialize( id ,  desc  )
        @id = id.strip
        @desc = desc.strip
    end

    def sameId?( other )
        @id == other.id
    end

    def eql?( other )
        sameId?( other ) and @desc == other.desc
    end

    def to_s()
        "#{id}:#{desc}"
    end
end

Ok?

Then I have filled my two arrays from different parts and I want to get the difference.

a = Instance.new("1","asdasdad")
b = Instance.new("1","a")
c = Instance.new("1","a")

p a.eql?(b) #false
p b.eql?(c) #true 

x = [a,b]
y = [c]

z = x - y # should leave a because b and c "represent" the same object

But this is not working, because a and b are being kept in the array. I'm wondering what method to I need to override in my class for this to work properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-08-20 13:06:37

您需要重新定义 #eql?哈希< /a> 方法。

您可以将其定义为:

def hash
    id.hash + 32 * desc.hash
end

详细信息:

查看 Ruby 1.9 中调用的内容:

    % irb
    >> class Logger < BasicObject
    >>   def initialize(delegate)
    >>     @delegate = delegate
    >>   end
    >>   def method_missing(m,*args,&blk)
    >>     ::STDOUT.puts [m,args,blk].inspect
    >>     @delegate.send(m,*args,&blk)
    >>   end
    >> end
    => nil
    >> object = Logger.new(Object.new)
    [:inspect, [], nil]
    => #<Object:0x000001009a02f0>
    >> [object] - [0]
    [:hash, [], nil]
    [:inspect, [], nil]
    => [#<Object:0x000001009a02f0>]
    >> zero = Logger.new(0)
    [:inspect, [], nil]
    => 0
    >> [zero] - [0]
    [:hash, [], nil]
    [:eql?, [0], nil]
    => []

ruby​​ 1.8.7 中也是如此

    % irb18
    >> class Logger < Object
    >>   instance_methods.each { |m| undef_method m }
    >>   def initialize(delegate)
    >>     @delegate = delegate
    >>   end
    >>   def method_missing(m,*args,&blk)
    >>     ::STDOUT.puts [m,args,blk].inspect
    >>     @delegate.send(m,*args,&blk)
    >>   end
    >> end
    (irb):2: warning: undefining `__send__' may cause serious problem
    (irb):2: warning: undefining `__id__' may cause serious problem
    => nil
    >> object = Logger.new(Object.new)
    [:inspect, [], nil]
    => #<Object:0x100329690>
    >> [object] - [0]
    [:hash, [], nil]
    [:inspect, [], nil]
    => [#<Object:0x100329690>]
    >> zero = Logger.new(0)
    [:inspect, [], nil]
    => 0
    >> [zero] - [0]
    [:hash, [], nil]
    [:eql?, [0], nil]
    => []

You need to redefine #eql? and the hash method.

You may define it as:

def hash
    id.hash + 32 * desc.hash
end

Details:

To see what's being called in Ruby 1.9:

    % irb
    >> class Logger < BasicObject
    >>   def initialize(delegate)
    >>     @delegate = delegate
    >>   end
    >>   def method_missing(m,*args,&blk)
    >>     ::STDOUT.puts [m,args,blk].inspect
    >>     @delegate.send(m,*args,&blk)
    >>   end
    >> end
    => nil
    >> object = Logger.new(Object.new)
    [:inspect, [], nil]
    => #<Object:0x000001009a02f0>
    >> [object] - [0]
    [:hash, [], nil]
    [:inspect, [], nil]
    => [#<Object:0x000001009a02f0>]
    >> zero = Logger.new(0)
    [:inspect, [], nil]
    => 0
    >> [zero] - [0]
    [:hash, [], nil]
    [:eql?, [0], nil]
    => []

The same is true in ruby 1.8.7

    % irb18
    >> class Logger < Object
    >>   instance_methods.each { |m| undef_method m }
    >>   def initialize(delegate)
    >>     @delegate = delegate
    >>   end
    >>   def method_missing(m,*args,&blk)
    >>     ::STDOUT.puts [m,args,blk].inspect
    >>     @delegate.send(m,*args,&blk)
    >>   end
    >> end
    (irb):2: warning: undefining `__send__' may cause serious problem
    (irb):2: warning: undefining `__id__' may cause serious problem
    => nil
    >> object = Logger.new(Object.new)
    [:inspect, [], nil]
    => #<Object:0x100329690>
    >> [object] - [0]
    [:hash, [], nil]
    [:inspect, [], nil]
    => [#<Object:0x100329690>]
    >> zero = Logger.new(0)
    [:inspect, [], nil]
    => 0
    >> [zero] - [0]
    [:hash, [], nil]
    [:eql?, [0], nil]
    => []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文