Sebastian
Sebastian

Reputation: 2204

Swap part of a string in Ruby

What's the easiest way in Ruby to interchange a part of a string with another value. Let's say that I have an email, and I want to check it on two domains, but I don't know which one I'll get as an input. The app I'm building should work with @gmail.com and @googlemail.com domains.

Example:

swap_string '[email protected]' # >>[email protected]
swap_string '[email protected]' # >>[email protected]

Upvotes: 1

Views: 517

Answers (5)

Azarantara
Azarantara

Reputation: 537

If you're looking to substitute a part of a string with something else, gsub works quite well.

Link to Gsub docs

It lets you match a part of a string with regex, and then substitute just that part with another string. Naturally, in place of regex, you can just use a specific string.

Example:

"[email protected]".gsub(/@gmail/, '@googlemail')

is equal to

[email protected]

In my example I used @gmail and @googlemail instead of just gmail and googlemail. The reason for this is to make sure it's not an account with gmail in the name. It's unlikely, but could happen.

Don't match the .com either, as that can change depending on where the user's email is.

Upvotes: 6

user2719890
user2719890

Reputation:

You can try with Ruby gsub :

eg:

  "[email protected]".gsub("gmail.com","googlemail.com");

As per your need of passing a string parameter in a function this should do:

def swap_mails(str)

if str =~ /gmail.com$/

   str.sub('gmail.com','googlemail.com');

else

   str.sub('googlemail.com','gmail.com');

end 

end


swap_mails "[email protected]" //[email protected]

swap_mails "[email protected]" ////[email protected]

Upvotes: 1

Kyle Decot
Kyle Decot

Reputation: 20815

String has a neat trick up it's sleeve in the form of String#[]:

def swap_string(string, lookups = {})
  string.tap do |s|
    lookups.each { |find, replace| s[find] = replace and break if s[find] }
  end
end

# Example Usage

lookups = {"googlemail.com"=>"gmail.com", "gmail.com"=>"googlemail.com"}

swap_string("[email protected]", lookups) # => [email protected]
swap_string("[email protected]", lookups) # => [email protected]

Allowing lookups to be passed to your method makes it more reusable but you could just as easily have that hash inside of the method itself.

Upvotes: 0

limekin
limekin

Reputation: 1944

My addition :

def swap_domain str
  str[/.+@/] + [ 'gmail.com', 'googlemail.com' ].detect do |d|
    d != str.split('@')[1]
  end
end

swap_domain '[email protected]' 
#=> [email protected]
swap_domain '[email protected]' 
#=> [email protected]

And this is bad code, imo.

Upvotes: 0

shivam
shivam

Reputation: 16506

Assuming googlemail.com and gmail.com are the only two possibilities, you can use sub to replace a pattern with given replacement:

def swap_string(str)
   if str =~ /gmail.com$/
     str.sub("gmail.com","googlemail.com")
   else
     str.sub("googlemail.com","gmail.com")
   end
end

swap_string '[email protected]'
# => "[email protected]"

swap_string '[email protected]'
# => "[email protected]"

Upvotes: 1

Related Questions