Saturday 15 March 2014

What method is needed to have the "-" ( subtract ) method working with Ruby arrays? -


If I have two arrays, a and b , then the method What is the need to override the object, then the recovery method works - properly?

Is it enough with eql?

Edit

I am adding more details to my question

I defined this class Is:

  class example attr_reader: id,: desc def initialize (id, desc) @id = id.strip @desc = desc.strip (other) @id == Other End DEEF EQUIL? (Other) Identical IDs (other) and @dcc == Other DCC end DEF () "# {ID}: # {descendant}"  

OK?

Then I have filled two arrays with two 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. (B) #false p b.eql? (C) #true x = [a, b] y = [c] should be left by z = x - y # because b and c are the same object "representation"  

but this Not working, because being placed in the a and b array. I am thinking that for this, I need a method to work properly in my class.

Do you have # eql? and the method needs to be redefined

You can define it as:

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

Description:

To see what is being said in Ruby 1.9:

  % Irb> & Gt; Class logger & lt; Original Object & gt; & Gt; Def start (delegate)> & Gt; @delegate = delegate & gt; & Gt; End & gt; & Gt; DF method_mission (m, * args, and black) & gt; & Gt; :: STDOUT.puts [m, args, blk] .irpect & gt; & Gt; @ Delegate.send (m, * args, and blk) & gt; & Gt; End & gt; & Gt; End = & gt; Zero & gt; & Gt; Object = logger.new (object.New) [: inspection, [], zero] => # & Lt; Object: 0x000001009a02f0 & gt; & Gt; & Gt; [Object] - [0] [: hash, [], zero] [: inspection, [], zero] => [# And Object: 0x000001009a02f0>]] gt; & Gt; Zero = logger New (0) [: Inspection, [], Zero] => 0 & gt; & Gt; [Zero] - [0] [: hash, [], zero] [: eql?, [0], zero] => []  

Ruby 1.8.7

 % irb18> & Gt; Class logger & lt; Object & gt; & Gt; Examples_imilities.each {| M | Undef_method m} & gt; & Gt; Def start (delegate)> & Gt; @delegate = delegate & gt; & Gt; End & gt; & Gt; DF method_mission (m, * args, and black) & gt; & Gt; :: STDOUT.puts [m, args, blk] .irpect & gt; & Gt; @ Delegate.send (m, * args, and blk) & gt; & Gt; End & gt; & Gt; End (IRB): 2: Warning: undefining '__send__' may be the cause of serious problem (IRB): 2: Warning: undefining` __id__ 'can be a cause of serious problem => Zero & gt; & Gt; Object = logger.new (object.New) [: inspection, [], zero] => # & Lt; Item: 0x100329690 & gt; & Gt; & Gt; [Object] - [0] [: hash, [], zero] [: inspection, [], zero] => [# And Object: 0x100329690 & gt;] & gt; & Gt; Zero = logger New (0) [: Inspection, [], Zero] => 0 & gt; & Gt; [Zero] - [0] [: hash, [], zero] [: eql?, [0], zero] => []  

No comments:

Post a Comment