Project

General

Profile

Actions

Bug #10997

closed

String in Hash 'prepend', '<<' bug

Added by hiphapis (Johan Kim) about 9 years ago. Updated about 9 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
[ruby-core:<unknown>]

Description

i used with Rails console

Only String is great

original = "a" #=> "a"
clone = original.clone #=> "a"
clone.prepend("prepend_") #=> "prepend_a"
clone #=> "prepend_a"
original #=> "a"

but String in Hash then get some issue

original = { a: "a" } #=> {:a=>"a"}
clone = original.clone #=> {:a=>"a"}
clone[:a].prepend("prepend_") #=> "prepend_a"
clone #=> {:a=>"prepend_a"}
original #=> {:a=>"prepend_a"}

i expect original to {:a=>"a"}
and when you use literal then works

original = { a: "a" } #=> {:a=>"a"}
clone = original.clone #=> {:a=>"a"}
clone[:a] = "prepend_#{clone[:a]}" #=> "prepend_a"
clone #=> {:a=>"prepend_a"}
original #=> {:a=>"a"}

<< has same issue too.

Actions #1

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected

Hash#clone does not clone the elements, so the value is shared.
In the last example, you assign different object.

Actions

Also available in: Atom PDF

Like0
Like0