Skip to content

Instantly share code, notes, and snippets.

@peteroyle
Created April 5, 2013 01:31
Show Gist options
  • Save peteroyle/5315918 to your computer and use it in GitHub Desktop.
Save peteroyle/5315918 to your computer and use it in GitHub Desktop.
Example of Ruby's behavior when modifying properties/values of objects/hashes passed as parameters
This test passes:
it "should tell me how params are passed" do
# that new car smell
car = Car.new
car.colour = "blue"
# this car smells like hash
car_hash = {'colour' => 'blue'}
# both cars were created blue yeah?
car.colour.should eq('blue')
car_hash['colour'].should eq('blue')
# now we paint them red
make_it_red(car)
set_value_to_red(car_hash)
# they're read right?
car.colour.should eq('red')
car_hash['colour'].should eq('red')
# not still blue? Not even a little bit?
car.colour.should_not eq('blue')
car_hash['colour'].should_not eq('blue')
end
def make_it_red(car)
car.colour = 'red'
end
def set_value_to_red(hash)
hash['colour'] = 'red'
end
class Car
attr_accessor :colour
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment