vasek 228 vasek 228 - 1 year ago 118
Ruby Question

Ruby substring downcase

Is there more appropriate way to downcase a substring than this one?:

string[a..b] = string[a..b].downcase

Maybe something like this:


For example:

string = "RubyChangedMyMind"

string[1..string.length] = string[1..string.length].downcase
# this is working but i want to know if there is more short way to do this
# (indeces may be different)
# something like string[a..b].downcase!

Thanks in advance!

Answer Source

you can make proc:

my_downcase = { |str,n,m|{ |c,index| (n..m).include?(index) ? c.downcase : c }.join}

and call it whenever you want:,range_from,range_to)

for example:

string = "dsadaDAdasdsadDADADadsadADD"
new_string =,0,11)
#=> "dsadadadasdsadDADADadsadADD"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download