mirror of https://github.com/rails/rails
Added Byte operations to Numeric, so 5.5.megabytes + 200.kilobytes #461 [Marcel Molina]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@393 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
fe8fb574c0
commit
876a1a1fa3
|
@ -1,3 +1,5 @@
|
|||
* Added Byte operations to Numeric, so 5.5.megabytes + 200.kilobytes #461 [Marcel Molina]
|
||||
|
||||
* Fixed that Dependencies.reload can't load the same file twice #420 [Kent Sibilev]
|
||||
|
||||
* Added Fixnum#ago/until, Fixnum#since/from_now #450 [bitsweat]
|
||||
|
|
|
@ -1 +1 @@
|
|||
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
|
||||
Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
|
||||
|
|
|
@ -6,27 +6,3 @@ def silence_warnings
|
|||
$VERBOSE = old_verbose
|
||||
end
|
||||
end
|
||||
|
||||
class Hash
|
||||
# Return a new hash with all keys converted to symbols.
|
||||
def symbolize_keys
|
||||
inject({}) do |options, (key, value)|
|
||||
options[key.to_sym] = value
|
||||
options
|
||||
end
|
||||
end
|
||||
|
||||
# Destructively convert all keys to symbols.
|
||||
def symbolize_keys!
|
||||
keys.each do |key|
|
||||
unless key.is_a?(Symbol)
|
||||
self[key.to_sym] = self[key]
|
||||
delete(key)
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
alias_method :to_options, :symbolize_keys
|
||||
alias_method :to_options!, :symbolize_keys!
|
||||
end
|
||||
|
|
|
@ -1,7 +1,38 @@
|
|||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../../lib/core_ext/hash_ext'
|
||||
require File.dirname(__FILE__) + '/../../lib/core_ext/hash'
|
||||
|
||||
class HashExtTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@strings = { 'a' => 1, 'b' => 2 }
|
||||
@symbols = { :a => 1, :b => 2 }
|
||||
@mixed = { :a => 1, 'b' => 2 }
|
||||
end
|
||||
|
||||
def test_methods
|
||||
h = {}
|
||||
assert_respond_to h, :symbolize_keys
|
||||
assert_respond_to h, :symbolize_keys!
|
||||
assert_respond_to h, :to_options
|
||||
assert_respond_to h, :to_options!
|
||||
end
|
||||
|
||||
def test_symbolize_keys
|
||||
assert_equal @symbols, @symbols.symbolize_keys
|
||||
assert_equal @symbols, @strings.symbolize_keys
|
||||
assert_equal @symbols, @mixed.symbolize_keys
|
||||
|
||||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
|
||||
end
|
||||
|
||||
def test_symbolize_keys!
|
||||
assert_equal @symbols, @symbols.dup.symbolize_keys!
|
||||
assert_equal @symbols, @strings.dup.symbolize_keys!
|
||||
assert_equal @symbols, @mixed.dup.symbolize_keys!
|
||||
|
||||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! }
|
||||
end
|
||||
|
||||
def test_assert_valid_keys
|
||||
assert_nothing_raised do
|
||||
{ :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
||||
|
@ -11,4 +42,4 @@ class HashExtTest < Test::Unit::TestCase
|
|||
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,35 +17,3 @@ class MiscTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class HashOptionsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@strings = { 'a' => 1, 'b' => 2 }
|
||||
@symbols = { :a => 1, :b => 2 }
|
||||
@mixed = { :a => 1, 'b' => 2 }
|
||||
end
|
||||
|
||||
def test_methods
|
||||
h = {}
|
||||
assert_respond_to h, :symbolize_keys
|
||||
assert_respond_to h, :symbolize_keys!
|
||||
assert_respond_to h, :to_options
|
||||
assert_respond_to h, :to_options!
|
||||
end
|
||||
|
||||
def test_symbolize_keys
|
||||
assert_equal @symbols, @symbols.symbolize_keys
|
||||
assert_equal @symbols, @strings.symbolize_keys
|
||||
assert_equal @symbols, @mixed.symbolize_keys
|
||||
|
||||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
|
||||
end
|
||||
|
||||
def test_symbolize_keys!
|
||||
assert_equal @symbols, @symbols.dup.symbolize_keys!
|
||||
assert_equal @symbols, @strings.dup.symbolize_keys!
|
||||
assert_equal @symbols, @mixed.dup.symbolize_keys!
|
||||
|
||||
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue