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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -