Permalink
Browse files

Merge pull request #12702 from dmathieu/finetune_session_fetch

session#fetch doesn't behave exactly like Hash#fetch.
  • Loading branch information...
2 parents a021938 + 38f8872 commit bbb32d205f2391be21d40ad64b693a4c0d9aa534 @rafaelfranca rafaelfranca committed Oct 30, 2013
Showing with 8 additions and 2 deletions.
  1. +3 −1 actionpack/CHANGELOG.md
  2. +5 −1 actionpack/test/dispatch/request/session_test.rb
@@ -1,6 +1,8 @@
* Add `session#fetch` method
- fetch behaves like [Hash#fetch](http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch).
+ fetch behaves similarly to [Hash#fetch](http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch),
+ with the exception that the returned value is always saved into the session.
+
It returns a value from the hash for the given key.
If the key can’t be found, there are several options:
@@ -63,11 +63,15 @@ def test_clear
def test_fetch
session = Session.create(store, {}, {})
- session['one'] = '1'
+ session['one'] = '1'
assert_equal '1', session.fetch(:one)
+
assert_equal '2', session.fetch(:two, '2')
+ assert_equal '2', session.fetch(:two)
+
assert_equal 'three', session.fetch(:three) {|el| el.to_s }
+ assert_equal 'three', session.fetch(:three)
assert_raise KeyError do
session.fetch(:four)

0 comments on commit bbb32d2

Please sign in to comment.