Here is a sample code from a RSpec code:
describe Thing do
def create_thing(options)
thing = Thing.new
thing.set_status(options[:status])
thing
end
it "should do something when ok" do
thing = create_thing(:status => 'ok')
thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
...
end
end
So my confusion is mostly on this line:
thing.set_status(options[:status])
So create_thing method has an “option” parameter then we are passing status part of that parameter? Can someone explain this syntax in some easier words?
optionsis just a variable. The part you need to understand is this partYou are basically passing a
Hashto create_thing and thereforeoptionsis a hash. Then you can access the value of thestatuskey by doingoptions[:status].If the above mentioned line looked like this
optionswould be “Foo” and you could get an error trying to do something likeoptions[:status]