Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update single value in ruby hash from super

Tags:

ruby

rspec

I'm writing tests in rspec and am trying to declare some variables with let.

describe 'my_test' do
    let(:params) {{
        :happy => 1,
        :sad => 0,
    }}

    context 'mixed' do
        let(:params) {{
            :happy => 1,
            :sad => 1,
        }}
    end
end

But then I saw how variables can be ovewritten with super, which would be convenient for long lists of params: http://myronmars.to/n/dev-blog/2013/02/rspec-2-13-is-released

So my question is, how do I overwrite just a single value in the original hash? I've tried searching but can only find ways to overwrite all values. Does anything like below exist?

let(:hash) { super().updatehash('sad', '1') }
like image 910
xiankai Avatar asked Mar 13 '14 03:03

xiankai


1 Answers

You can overload hash variables using Hash#merge

let(:hash) { super().merge(:sad => '1') }

it will overwrite any key-values in the original hash with the key-value pairs you provide in the hash to merge over it.

If you'd like a link to the doco for merge, apidock is good:

http://apidock.com/ruby/Hash/merge

like image 148
Taryn East Avatar answered Sep 21 '22 21:09

Taryn East