technoweenie/faraday — Issue #93: Faraday affected by rails bug in ActiveSupport::SafeBuffer#gsub with block
Author: radanskoric
Created: 2011-11-11 13:14:26+00:00
State: open

This took me a few hours today to locate. I was using koala gem which uses Faraday and I was getting

"NoMethodError: undefined method `bytesize' for nil:NilClass"

The problem was in

lib/faraday/utils.rb:129:in `block in escape'

And it is being caused by

https://github.com/rails/rails/issues/3496

However, if Faraday::Utils#escape is changed from

def escape(s)
s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do
'%' << $1.unpack('H2'*bytesize($1)).join('%').tap { |c| c.upcase! }
end
end

to

def escape(s)
s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do |match|
'%' << match.unpack('H2'*bytesize(match)).join('%').tap { |c| c.upcase! }
end
end

everthing works.

Is there any particular reason why $1 is used instead of the block parameter?