Skip to content

Instantly share code, notes, and snippets.

@spacemunkay
Last active August 29, 2015 14:06
Show Gist options
  • Save spacemunkay/bd64926586df8f7278a6 to your computer and use it in GitHub Desktop.
Save spacemunkay/bd64926586df8f7278a6 to your computer and use it in GitHub Desktop.
Accessor confusion with Array Union
class MyClass
attr_accessor :values, :uniq_values
def initialize(value)
self.uniq_values = ['default_value']
self.values = ['default_value']
copy_value(value)
add_value(value)
end
def copy_value(value)
uniq_values |= value
end
def add_value(value)
values << value
end
def run
puts "unique_values: #{uniq_values}"
puts "values: #{values}"
end
end
obj = MyClass.new('poop')
obj.run
# Expect 'unique_values' and 'values' to be the same
# OUTPUT:
# unique values: ["default_value"]
# values: ["default_value", "poop"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment