开发者

Merge a hash with the key/values of a string in ruby

I'm trying to merge a hash with the key/values of string in ruby.

i.e.

h = {:day => 4, :month => 8, :year => 2010}
s = "/my/crazy/url/:day/:month/:year"
puts s.interpolate(h)

All I'v开发者_开发百科e found is to iterate the keys and replace the values. But I'm not sure if there's a better way doing this? :)

class String
  def interpolate(e)
    self if e.each{|k, v| self.gsub!(":#{k}", "#{v}")}
  end
end

Thanks


No need to reinvent Ruby built-ins:

h = {:day => 4, :month => 8, :year => 2010}
s = "/my/crazy/url/%{day}/%{month}/%{year}"
puts s % h

(Note this requires Ruby 1.9+)


"Better" is probably subjective, but here's a method using only one call to gsub:

class String
  def interpolate!(h)
    self.gsub!(/:(\w+)/) { h[$1.to_sym] }
  end
end

Thus:

>> "/my/crazy/url/:day/:month/:year".interpolate!(h)
=> "/my/crazy/url/4/8/2010"


That doesn't look bad to me, but another approach would be to use ERB:

require 'erb'

h = {:day => 4, :month => 8, :year => 2010}
template = ERB.new "/my/crazy/url/<%=h[:day]%>/<%=h[:month]%>/<%=h[:year]%>"
puts template.result(binding)


Additional idea could be to extend String#% method so that it know how to handle Hash parameters, while keeping existing functionality:

class String
  alias_method :orig_percent, :%
  def %(e)
    if e.is_a?(Hash)
      # based on Michael's answer
      self.gsub(/:(\w+)/) {e[$1.to_sym]}
    else
      self.orig_percent e
    end
  end
end

s = "/my/%s/%d/:day/:month/:year"
puts s % {:day => 4, :month => 8, :year => 2010}
#=> /my/%s/%d/4/8/2010
puts s % ['test', 5]
#=> /my/test/5/:day/:month/:year
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜