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 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

···

--
Posted via http://www.ruby-forum.com/.

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

but I want

        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

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")'.

···

--
   WBR, Peter Zotov.

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.

···

Am 07.09.2012 12:32, schrieb Ferdous ara:

but I want

         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

--
<https://github.com/stomar/&gt;

Peter Zotov 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

···

--
Posted via http://www.ruby-forum.com/\.