Apply modification only to substring in Ruby -
i have string of form "award.x_initial_value.currency" , camelize except leading "x_" result of form: "award.x_initialvalue.currency". current implementation is:
a = "award.x_initial_value.currency".split(".") b = a.map{|s| s.slice!("x_")} a.map!{|s| s.camelize(:lower)} a.zip(b).map!{|x, y| x.prepend(y.to_s)}
i not happy since it's neither fast nor elegant , performance key since applied large amounts of data. googled couldn't find anything.
there faster/better way of achieving this?
since "performance key" skip overhead of activesupport::inflector
, use regular expression perform "camelization" yourself:
a = "award.x_initial_value.currency" a.gsub(/(?<!\bx)_(\w)/) { $1.capitalize } #=> "award.x_initialvalue.currency"
Comments
Post a Comment