Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby replace words in a string

# Replace first value a return new string
sentence = 'pop poper'
sentence.sub('pop', 'stop')
=> 'stop poper'
puts sentence
=> 'pop poper'

# Replace all matches and return new string
sentence = 'pop poper'
sentence.gsub('pop', 'stop')
=> 'stop stoper'
puts sentence
=> 'pop poper'

# Replace first value and update existing string
sentence = 'pop poper'
sentence.sub!('pop', 'stop')
=> 'stop poper'
puts sentence
=> 'stop poper'

# Replace all matches and update string
sentence = 'pop poper'
sentence.gsub!('pop', 'stop')
=> 'stop stoper'
puts sentence
=> 'stop stoper'
Comment

substring replace in ruby

user_input = "mooventhan" 
user_input.gsub!(/moo/, "hel")
print user_input
Comment

replace strring by another string ruby

myString = "Welcome to JavaScript!"

myString["JavaScript"]= "Ruby"

puts myString
=> "Welcome to Ruby!"
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby array has element 
Ruby :: Your Ruby version is 2.7.0, but your Gemfile specified 2.7.1 
Ruby :: ruby exponent 
Ruby :: ruby 2 decimal 
Ruby :: ruby each char with index 
Ruby :: ruby case when multiple conditions 
Ruby :: ruby connect database 
Ruby :: edit file terminal mac 
Ruby :: httparty SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate) (OpenSSL::SSL::SSLError) 
Ruby :: parse xml ruby 
Ruby :: ruby create csv 
Ruby :: ruby generate uuid 
Ruby :: ruby map array 
Ruby :: rails array count occurrences of elements 
Ruby :: read xls file in ruby 
Ruby :: ruby for each continue 
Ruby :: rails 10 times do 
Ruby :: ruby remove value from array 
Ruby :: infinite loop ruby 
Ruby :: sendgrid ruby on rails 
Ruby :: ruby constructors 
Ruby :: ruby string format 
Ruby :: sentry send error manually ruby 
Ruby :: Rails checkbox checked/unchecked values 
Ruby :: find_by column name rails route 
Ruby :: ruby on rails collapse array 
Ruby :: ruby global property from object 
Ruby :: ruby convert array to set 
Ruby :: how to make rails 
R :: r remove rows where value is 0 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =