On Thu, Feb 21, 2013 at 1:40 PM, Joel Pearson <[email protected]> wrote:
> I thought I could avoid the issue of splitting the data into multiple
> outputs by creating copies of the class, but apparently I was wrong:
>
> irb(main):002:0> m = RubyExcel.new.load [['a','b']]
> => columns: 2, rows: 1, values: 2
>
> irb(main):003:0> a = m.clone
>
> irb(main):004:0> a['A1'] = nil
>
> irb(main):005:0> puts a
>         b
>
> irb(main):006:0> puts m
>         b
>
> What am I doing wrong? Is @data inside m not being duplicated along with
> the class instance?

#clone and #dup are _always_ shallow copies.  If you want to change
that you need to override them, e.g.

class Excel
  def dup
    super.tap do |copy|
      %w{@table foo}.each  {|iv| copy.instance_variable_set(iv,
instance_variable_get(iv).dup)}
    end
  end
end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to