2016-11-27 3 views
0

Ich möchte, über einen Hash-Wert eines Feldes ändern, zum Beispiel:Wie ändert man einen Wert in einem Array über einen Hash?

arr = ['g','g','e','z'] 
positions = {1 => arr[0], 2 => arr[1]} 

positions[1] = "ee" 

Problem ist, dass die eine, die Hash geändert wird, und nicht-Array. Wenn ich mache p arr Es gibt immer noch ['g','g','e','z'] aus. Gibt es einen Weg dahin?

+0

Was genau möchten Sie erreichen? –

+0

@ MarkoAvlijaš ist im Titel. Ich möchte ein Array über einen Hash ändern. Aber wenn ich es mache, ist derjenige, der sich ändert, der Hash selbst, nicht das Array – chaosfirebit

+0

@chaosfirebit und warum willst du das tun? – Stefan

Antwort

2

Sie gehen zu müssen, eine andere Zeile Code hinzufügen zu tun, was Sie wollen:

arr = ['g','g','e','z'] 
positions = {1 => arr[0], 2 => arr[1]} 

positions[1] = "ee" 
arr[0] = positions[1] 

Eine andere Möglichkeit wäre, ein Verfahren zu machen, die das Array für Sie automatisch aktualisiert, so etwas wie folgt aus:

def update_hash_and_array(hash, array, val, index) 
    # Assume that index is not zero indexed like you have 
    hash[index] = val 
    array[index - 1] = val 
end 

update_hash_and_array(positions, arr, "ee", 1) # Does what you want 
2

Dies ist möglich, in Ihren Hash mit Procs zu kodieren.

Sie können dies ein wenig verallgemeinern, wenn Sie einen Hash generieren möchten, der jedes Array ändern kann.

def remap_arr(arr, idx) 
    (idx...arr.length+idx).zip(arr.map.with_index{|_,i| -> (val) {arr[i] = val}}).to_h 
end 

arr = [1,2,3,4,5,6] 
positions = remap_arr(arr, 1) 

positions[2].('hello') 
# arr => [1,'hello',3,4,5,6] 

positions[6].('goodbye') 
# arr => [1,'hello',3,4,5,'goodbye'] 

Aber ich bin der Hoffnung, dieses Experiment nur ein Gedanke ist, gibt es keinen Grund, die Art und Weise Array-Indizierung Verhalten zu ändern arbeitet von 1 zu starten, anstatt 0. In solchen Fällen würde man normalerweise wollen nur Offset Der Index muss mit der richtigen Array-Indizierung übereinstimmen (beginnend bei Null). Wenn das nicht ausreicht, ist es ein Zeichen, dass Sie eine andere Datenstruktur benötigen.

+0

Das ist wirklich interessant. Ich hatte keine Ahnung, dass das existierte. –

+0

@ gr1zzlybe4r heh, es ist nicht wirklich ein gemeinsames Ruby-Paradigma, soweit ich weiß ... Es ist viel mehr Javascript-y. Wie gesagt, es gibt keinen wirklichen Grund, solche Dinge in der Praxis zu tun, und selbst dann ist das Schreiben einer Methode viel klarer (wie in Ihrer Antwort). – Damon

1
#!/usr/bin/env ruby 

a = %w(q w e) 

h = { 
    1 => a[0] 
} 

puts a[0].object_id # 70114787518660 
puts h[1].object_id # 70114787518660 
puts a[0] === h[1] # true 

# It is a NEW object of a string. Look at their object_ids. 
# That why you can not change value in an array via a hash. 
h[1] = 'Z' 

puts a[0].object_id # 70114787518660 
puts h[1].object_id # 70114574058580 
puts a[0] === h[1] # false 

h[2] = a 

puts a.object_id # 70308472111520 
puts h[2].object_id # 70308472111520 
puts h[2] === a  # true 

puts a[0] === h[2][0] # true 
# Here we can change value in the array via the hash. 
# Why? 
# Because 'h[2]' and 'a' are associated with the same object '%w(q w e)'. 
# We will change the VALUE without creating a new object. 
h[2][0] = 'X' 

puts a[0]    # X 
puts h[2][0]   # X 
puts a[0] === h[2][0] # true 
Verwandte Themen