Even shorter Ampersand to_proc?

On Mon, Nov 24, 2014 at 6:49 PM, Robert K.
[email protected] wrote:

On Mon, Nov 24, 2014 at 5:58 PM, Brandon W. [email protected] wrote:

There are times when regex is wrong. This is such a time.

Why?

Brandon, I would like to learn why you think think this is not a case
for regexp. Can you please elaborate? Thank you!

Kind regards

robert

Some problem with ruby clone, it is different between one-dimensional
array and two-dimensional array.like below!

irb(main):001:0> arr_a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> arr_b = arr_a.clone
=> [1, 2, 3]
irb(main):003:0> arr_a.object_id == arr_b.object_id
=> false
irb(main):004:0> arr_b[0] = 4
=> 4
irb(main):005:0> arr_b
=> [4, 2, 3]
irb(main):006:0> arr_a
=> [1, 2, 3]
irb(main):007:0> arr_a = [[1,2,3],[4,5,6]]
=> [[1, 2, 3], [4, 5, 6]]
irb(main):008:0> arr_b = arr_a.clone
=> [[1, 2, 3], [4, 5, 6]]
irb(main):009:0> arr_a.object_id == arr_b.object_id
=> false
irb(main):010:0> arr_b[0][0] = 7
=> 7
irb(main):011:0> arr_b
=> [[7, 2, 3], [4, 5, 6]]
irb(main):012:0> arr_a
=> [[7, 2, 3], [4, 5, 6]]

one-dimensional not change original array, but two-dimensional change
the original array!
why?

Clone doesn’t clone the sub-objects that the main object refers to
unless they are simple literals (like Integers). So for your second
example, the array has references to the sub arrays and when it clones,
it just clones the values of those references, but they still refer to
the same arrays and when you change an index of the clones sub arrays,
it changes the original ones.


Sent from Mailbox

Thank u very much!

On Thu, Nov 27, 2014 at 10:44 AM, Robert K.
[email protected] wrote:

ones.

In case you want to dig a bit deeper, you’ll find more explanations
under keywords “shallow copy”, “deep copy” and “aliasing”.

Another note: it can be misleading to think in terms of
multidimensional arrays. Ruby doesn’t have those, it just has Arrays
of Arrays. On the object level, there’s no different between those and
a regular Array of any other object.

Jesus.

On Thu, Nov 27, 2014 at 4:07 AM, mac [email protected] wrote:

Thank u very much!

On Nov 27, 2014, at 11:04 AM, Raj S. [email protected] wrote:

Clone doesn’t clone the sub-objects that the main object refers to unless
they are simple literals (like Integers). So for your second example, the
array has references to the sub arrays and when it clones, it just clones
the values of those references, but they still refer to the same arrays and
when you change an index of the clones sub arrays, it changes the original
ones.

In case you want to dig a bit deeper, you’ll find more explanations
under keywords “shallow copy”, “deep copy” and “aliasing”.

Kind regards

robert