This question already has answers here:
Ruby modify a piece of a string
(5 answers)
Closed 9 years ago.
I have a challenging Ruby problem:
I want to convert "howdy" to "YDWOh" and "how are you" to "uoy era WOh".
which is essentially capitalize, swapcase and reverse functions. Capitalize and swapcase is only for the first word of the sentence. How do I do this when I have more than one word in the sentence?
Try the following steps:
capitalize your string
split(' ') your string into an array with words
Use each_with_index.map to go over each word in the array
Use swapcase on only the first word of the array
join(' ') the array back into a string
reverse the string
You can try the below:
a = "how are you"
p a.gsub(a.split[0...1].join(' '),a.split[0...1].join(' ').capitalize.swapcase).reverse
Output:
"uoy era WOh"
Related
This question already has answers here:
Ruby single and double quotes
(6 answers)
Closed 4 years ago.
So I have a string formatted with triple quotation marks or %q{} like this and I want to insert a variable into the string; apparently using #{variable} wouldn't work.
variable = "some string"
puts %q{
My string looks like
this and i want to show
my string here #{variable}
}
Just use uppercase "Q"
variable = "some string"
%Q{ My math #{1+1} and string #{variable}}
This question already has answers here:
Ruby global match regexp?
(3 answers)
Closed 5 years ago.
I got a string "[5, 3]" and I want to extract 5 and 3 from it using regex.
p "[5, 3]".match(/\d,\s\d/) # <MatchData "5, 3">
p "[5, 3]".match(/\d/) # <MatchData "5">
I can't extract both numbers at same time
I think you need scan instead match, try:
p "[5, 3]".scan(/\d/)
What I can say is match returns the first match as MatchData, while scan returns everything what the regex matches.
See String#scan and Regexp#match.
This question already has answers here:
How to extract URL parameters from a URL with Ruby or Rails?
(10 answers)
Closed 7 years ago.
In ruby I have string that looks like this:
"\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
how can I extract offset value from this string with regular expressions?
It doesn't satisfy your requirement to use a regex, but here is a way:
uri = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
require "uri"
URI.decode_www_form(URI(uri).query).assoc("offset").last
# => "20"
or
URI.decode_www_form(URI(uri).query).to_h["offset"]
# => "20"
Assuming offset will always be present as offset= and it will always be a numeric value
str = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
str.scan(/offset=(\d+)/)
#=> [["20"]]
This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Ruby backslash to continue string on a new line?
(3 answers)
Closed 8 years ago.
If I have this code:
a = "hi" "pie"
puts a
It will print out hipie. Does Ruby automatically combine these?
Yes. From Literals: String
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion"
#=> "concatenation"
"This string contains " "no newlines."
#=> "This string contains no newlines."
This question already has answers here:
Validate that string contains only allowed characters in Ruby
(4 answers)
Closed 2 years ago.
Given a string of a mobile phone number, I need to make sure that the given string only contains digits 0-9, (,),+,-,x, and space. How can I do it in Ruby?
Use:
/^[-0-9()+x ]+$/
E.g.:
re = /^[-0-9()+x ]+$/
match = re.match("555-555-5555")
if (/^[-\d()\+x ]+$/.match(variable))
puts "MATCH"
else
puts "Does not MATCH"
end
Use String#count:
"+1 (800) 123-4567".count("^0-9+x()\\- ").zero? # => true
"x invalid string x".count("^0-9+x()\\- ").zero? # => false