Skip to content

Instantly share code, notes, and snippets.

@theotherzach
Last active September 21, 2016 18:35
Show Gist options
  • Save theotherzach/8f077ca97a726d602a553ac253dee821 to your computer and use it in GitHub Desktop.
Save theotherzach/8f077ca97a726d602a553ac253dee821 to your computer and use it in GitHub Desktop.
a = [{ slug: "foo"}, { slug: "bar" }]
b = a.sort_by { |e| e[:slug] }
a.push({ slug: "baz" })
result_1 = b.find { |e| e[:slug] == "baz" }
# Predict which branch executes
if (result_1.nil?)
p "Elements pushed into array a DO NOT appear in array b"
else
p "Elements pushed into array a DO appear in array b"
end
foo = b.find { |e| e[:slug] == "foo" }
foo[:marked] = true
result_2 = a.find { |e| e[:marked] }
# Predict which branch executes
if (result_2.nil?)
p "Mutating one array's elements HAS NO effect on the other's"
else
p "Mutating one array's elements CAN HAVE an effect on the other"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment