Skip to content

Commit

Permalink
Ruby 2.4: take advantage of String#unpack1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Mar 2, 2018
1 parent 6a02962 commit 4b42c7e
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion actionpack/test/controller/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setup
end

safe, unsafe = %w(: @ & = + $ , ;), %w(^ ? # [ ])
hex = unsafe.map { |char| "%" + char.unpack("H2").first.upcase }
hex = unsafe.map { |char| "%" + char.unpack1("H2").upcase }

@segment = "#{safe.join}#{unsafe.join}".freeze
@escaped = "#{safe.join}#{hex.join}".freeze
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def attribute(name, type = Type::Value.new, **options)
private

def define_method_attribute=(name)
safe_name = name.unpack("h*".freeze).first
safe_name = name.unpack1("h*".freeze)
ActiveModel::AttributeMethods::AttrNames.set_name_cache safe_name, name

generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/type/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def to_s
alias_method :to_str, :to_s

def hex
@value.unpack("H*")[0]
@value.unpack1("H*")
end

def ==(other)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module ClassMethods # :nodoc:
# Making it frozen means that it doesn't get duped when used to
# key the @attributes in read_attribute.
def define_method_attribute(name)
safe_name = name.unpack("h*".freeze).first
safe_name = name.unpack1("h*".freeze)
temp_method = "__temp__#{safe_name}"

ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module ClassMethods # :nodoc:
private

def define_method_attribute=(name)
safe_name = name.unpack("h*".freeze).first
safe_name = name.unpack1("h*".freeze)
ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name
sync_with_transaction_state = "sync_with_transaction_state" if name == primary_key

Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/testing/isolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def run_in_isolation(&blk)
write.close
result = read.read
Process.wait2(pid)
result.unpack("m")[0]
result.unpack1("m")
end
end

Expand Down Expand Up @@ -98,7 +98,7 @@ def run_in_isolation(&blk)
nil
end

return tmpfile.read.unpack("m")[0]
return tmpfile.read.unpack1("m")
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions activesupport/test/key_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def setup
# key would break.

expected = "b129376f68f1ecae788d7433310249d65ceec090ecacd4c872a3a9e9ec78e055739be5cc6956345d5ae38e7e1daa66f1de587dc8da2bf9e8b965af4b3918a122"
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64).generate_key("some_salt").unpack("H*").first
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64).generate_key("some_salt").unpack1("H*")

expected = "b129376f68f1ecae788d7433310249d65ceec090ecacd4c872a3a9e9ec78e055"
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64).generate_key("some_salt", 32).unpack("H*").first
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64).generate_key("some_salt", 32).unpack1("H*")

expected = "cbea7f7f47df705967dc508f4e446fd99e7797b1d70011c6899cd39bbe62907b8508337d678505a7dc8184e037f1003ba3d19fc5d829454668e91d2518692eae"
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64, iterations: 2).generate_key("some_salt").unpack("H*").first
assert_equal expected, ActiveSupport::KeyGenerator.new("0" * 64, iterations: 2).generate_key("some_salt").unpack1("H*")
end
end

Expand Down

0 comments on commit 4b42c7e

Please sign in to comment.