Cant change the string

s=hostname
cluster_name=s.match(/(\w+?)-\w+?-(\d+)/).captures.join.upcase

puts cluster_name

Result : BD55

but I want , if hostname starts with BD ,then change it to DK, if
hostname starts with CH,(CH57) change it to SY(SY57)

    if host_name =~ /^BD/

host_name.gsub(“BD”,“DK”)
puts host_name
end

so if hostname is BD, it will change it to DK (this number can be
change anytime)
So End result will be

DK55

but its not working

Can any one please help me

Ferdous ara писал 07.09.2012 14:32:

DK55

but its not working

Generally, Ruby methods tend to return a new object rather than
mutating
an existing one. Mutating methods often have a bang postfix, i.e.
“gsub!”.
This is not enforced by Ruby language itself, but is a useful
convention.

So, you need to use `host_name.gsub!(“BD”, “DK”)’.

Peter Z. wrote in post #1075034:

Ferdous ara писал 07.09.2012 14:32:

DK55

but its not working

Generally, Ruby methods tend to return a new object rather than
mutating
an existing one. Mutating methods often have a bang postfix, i.e.
“gsub!”.
This is not enforced by Ruby language itself, but is a useful
convention.

So, you need to use `host_name.gsub!(“BD”, “DK”)’.

thanks that work perfce

Am 07.09.2012 12:32, schrieb Ferdous ara:

So End result will be

DK55

but its not working

Can any one please help me

Tip: form a habit of using irb to find out what is “not working”:

1.9.3p194 :001 > host_name = ‘BD55’
=> “BD55”
1.9.3p194 :002 > host_name.gsub(“BD”,“DK”)
=> “DK55”
1.9.3p194 :003 > puts host_name
BD55
=> nil

… and you can see that host_name.gsub(…) returns the expected
modified string, but host_name is unchanged.