What is the use of ruby ​​Hash.replace or Array.replace?

I always see a replacement in the Array and Hash documentation, and I always think this is weird.

I am sure I have done something like this many times:

a = [:a, :b, :c, :d]

...

if some_condition
    a = [:e, :f]
end

But I never thought of using this instead:

a = [:a, :b, :c, :d]

...

if some_condition
    a.replace [:e, :f]
end

I guess this is the intended use. Does it really save memory or have some other benefit, or is it just a style?

+3
source share
3 answers

I think the intended use is to change the array in place that was passed to the method. For instance:

def m(a)
    a.replace(%w[a b])
end

a = %w[x y z]
m(a)
# a is now ['a', 'b']

Without replaceyou, you will need to do something like this:

def m(a)
    a.clear
    a << 'a' # or use .push of course
    a << 'b'
end

replace , (, , ), ( ! ) . ( ), , , , , , .

+5

a = [: e,: f] a.replace [: e,: f],

:

1.

a = [:a, :b, :c, :d]
a = [:e, :f]

:

ruby ​​--dump = insns test.rb

== disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] a
0000 trace            1                                               (   1)
0002 duparray         [:a, :b, :c, :d]
0004 setdynamic       a, 0
0007 trace            1                                               (   2)
0009 duparray         [:e, :f]
0011 dup
0012 setdynamic       a, 0
0015 leave

2.

a = [:a, :b, :c, :d]
a.replace([:e, :f])

:

ruby ​​--dump = insns test.rb

== disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] a
0000 trace            1                                               (   1)
0002 duparray         [:a, :b, :c, :d]
0004 setdynamic       a, 0
0007 trace            1                                               (   2)
0009 getdynamic       a, 0
0012 duparray         [:e, :f]
0014 send             :replace, 1, nil, 0, <ic:0>
0020 leave

, , , , rb_ary_replace.

VALUE
rb_ary_replace(VALUE copy, VALUE orig)
{
rb_ary_modify_check(copy);
orig = to_ary(orig);
if (copy == orig) return copy;

if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX)
{
    VALUE *ptr;
    VALUE shared = 0;

    if (ARY_OWNS_HEAP_P(copy))
    {
        xfree(RARRAY_PTR(copy));
    }
    else if (ARY_SHARED_P(copy))
    {
        shared = ARY_SHARED(copy);
        FL_UNSET_SHARED(copy);
    }
    FL_SET_EMBED(copy);
    ptr = RARRAY_PTR(orig);
    MEMCPY(RARRAY_PTR(copy), ptr, VALUE, RARRAY_LEN(orig));
    if (shared)
    {
        rb_ary_decrement_share(shared);
    }
    ARY_SET_LEN(copy, RARRAY_LEN(orig));
}
else
{
    VALUE shared = ary_make_shared(orig);
    if (ARY_OWNS_HEAP_P(copy))
    {
        xfree(RARRAY_PTR(copy));
    }
    else
    {
        rb_ary_unshare_safe(copy);
    }
    FL_UNSET_EMBED(copy);
    ARY_SET_PTR(copy, RARRAY_PTR(orig));
    ARY_SET_LEN(copy, RARRAY_LEN(orig));
    rb_ary_set_shared(copy, shared);
}
return copy; }
+6
a = [:a, :b, :c, :d]
b = [:x, :y, :z]
a.replace(b)

a.object_id == b.object_id
=> false

a = [:a, :b, :c, :d]
b = [:x, :y, :z]
a = b

a.object_id == b.object_id
=> true

a = [:a, :b, :c, :d]
c = a
b = [:x, :y, :z]
a.replace(b)
p c # => [:x, :y, :z]

against

a = [:a, :b, :c, :d]
c = a
b = [:x, :y, :z]
a = b
p c # => [:a, :b, :c, :d]

This does not exactly answer your question.

+1
source

All Articles