What does this in Ruby do? [duplicate] - ruby

This question already has answers here:
what is "?" in ruby
(3 answers)
Closed 4 years ago.
I was looking on solution at codewars and found this piece of code.
What does the part "..?z" in Array(char.next..?z) do?
I know that ".." it is for range ie. "a".."z" but idk. what
?z do.

?z is the same as 'z'. It is a single character literal string.
From the Ruby docs:
There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding:
?a #=> "a"
?abc #=> SyntaxError
?\n #=> "\n"
?\s #=> " "
?\\ #=> "\\"
?\u{41} #=> "A"
?\C-a #=> "\x01"
?\M-a #=> "\xE1"
?\M-\C-a #=> "\x81"
?\C-\M-a #=> "\x81", same as above
?あ #=> "あ"

? will convert the given character into string,
?z means 'z'
?a means 'a'
?a..?z means 'a'..'z'

Related

RUBY split word to letters and numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to ask you for help. I have keywords in this form "AB10" and I need to split i to "AB" and "10". What is the best way?
Thank you for your help!
One could use String#scan:
def divide_str(s)
s.scan(/\d+|\D+/)
end
divide_str 'AB10' #=> ["AB", "10"]
divide_str 'AB10CD20' #=> ["AB", "10", "CD", "20"]
divide_str '10AB20CD' #=> ["10", "AB", "20", "CD"]
The regular expression /\d+|\D+/ reads, "match one or more (+) digits (\d) or one or more non-digits (\D).
Here is another way, one that does not employ a regular expression.
def divide_str(s)
digits = '0'..'9'
s.each_char.slice_when do |x,y|
digits.cover?(x) ^ digits.cover?(y)
end.map(&:join)
end
divide_str 'AB10' #=> ["AB", "10"]
divide_str 'AB10CD20' #=> ["AB", "10", "CD", "20"]
divide_str '10AB20CD' #=> ["10", "AB", "20", "CD"]
See Enumerable#slice_when, Range#cover?, TrueClass#^ and FalseClass#^.
Use split like so:
my_str.split(/(\d+)/)
To split any string on the boundary between digits and letters, use either of these 2 methods:
Use split with regex in capturing parentheses to include the delimiter, here a stretch of digits, into the resulting array. Remove empty strings (if any) using a combination of reject and empty?:
strings = ['AB10', 'AB10CD20', '10AB20CD']
strings.each do |str|
arr = str.split(/(\d+)/).reject(&:empty?)
puts "'#{str}' => #{arr}"
end
Output:
'AB10' => ["AB", "10"]
'AB10CD20' => ["AB", "10", "CD", "20"]
'10AB20CD' => ["10", "AB", "20", "CD"]
Use split with non-capturing parentheses: (?:PATTERN), positive lookahead (?=PATTERN) and positive lookbehind (?<=PATTERN) regexes to match the letter-digit and digit-letter boundaries:
strings.each do |str|
arr = str.split(/ (?: (?<=[A-Za-z]) (?=\d) ) | (?: (?<=\d) (?=[A-Za-z]) ) /x)
puts "'#{str}' => #{arr}"
end
The two methods give the same output for the cases shown.

Question mark in the beginning of line in Ruby [duplicate]

This question already has answers here:
What does the unary question mark (?) operator do?
(4 answers)
Closed 2 years ago.
Why when I use a question mark at the beginning of a line it makes it into a String?
For example:
?a
# => "a"
?1
# => "1"
?(
# => "("
?a + "b"
# => "ab"
When I use more than 1 character it raises error:
?ab
# SyntaxError (syntax error, unexpected '?')
Why it happens?
I found the answer in docs:
There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding
?a #=> "a"
?abc #=> SyntaxError
?\n #=> "\n"
?\s #=> " "
?\\ #=> "\\"
?\u{41} #=> "A"
?\C-a #=> "\x01"
?\M-a #=> "\xE1"
?\M-\C-a #=> "\x81"
?\C-\M-a #=> "\x81", same as above
?あ #=> "あ"

How do I match a 10 character sub-string starting with 'H' in a longer string with Ruby?

I have the following string:
/Users/patelc75/Documents/code/haloror/dialup/H200000787_1313406125/H200000787_1313389058_1.xml
In Ruby, how do I extract the first 10 character substring that starts with the letter H and contains 9 digits (digits only) after the H. In this above example, the substring would be H200000787
String#[] method is what you need:
str = '/Users/patelc75/Documents/code/haloror/dialup/H200000787_1313406125/H200000787_1313389058_1.xml'
puts str[/H\d{9}/] #=> H200000787
irb(main):001:0> s = "/Users/patelc75/Documents/code/haloror/dialup/H200000787_1313406125/H200000787_1313389058_1.xml"
=> "/Users/patelc75/Documents/code/haloror/dialup/H200000787_1313406125/H200000787_1313389058_1.xml"
irb(main):002:0> s =~ /H\d{9}/
=> 46
irb(main):003:0> $&
=> "H200000787"

ruby double question mark [duplicate]

This question already has answers here:
what is "?" in ruby
(3 answers)
Closed 7 years ago.
I came across this piece of ruby code:
str[-1]==??
What is the double question mark all about? Never seen that before.
Ruby 1.8 has a ?-prefix syntax that turns a character into its ASCII code value. For example, ?a is the ASCII value for the letter a (or 97). The double question mark you see is really just the number 63 (or the ASCII value for ?).
?a # => 97
?b # => 98
?c # => 99
?\n # => 10
?? # => 63
To convert back, you can use the chr method:
97.chr # => "a"
10.chr # => "\n"
63.chr # => "?"
??.chr # => "?"
In Ruby 1.9, the ?a syntax returns the character itself (as does the square bracket syntax on strings):
?? # => "?"
"What?"[-1] # => "?"
As Ryan says, the ? prefix gives you the ASCII value of a character. The reason why this is useful in this context is that when you use the index notation on a string in Ruby 1.8 the ASCII value is returned rather than the character. e.g.
irb(main):009:0> str = 'hello'
=> "hello"
irb(main):010:0> str[-1]
=> 111
so the following wouldn't test if the last character of a string was the letter 'o'
irb(main):011:0> str[-1] == 'o'
=> false
but this would:
irb(main):012:0> str[-1] == ?o
=> true
and (provided you know what the ? does!) this is slightly clearer than
irb(main):013:0> str[-1] == 111
=> true

regexp to match string1 unless preceded by string2

Using Ruby, how can I use a single regex to match all occurrences of 'y' in "xy y ay xy +y" that are NOT preceded by x (y, ay, +y)?
/[^x]y/ matches the preceding character too, so I need an alternative...
You need a zero-width negative look-behind assertion. Try /(?<!x)y/ which says exactly what you're looking for, i.e. find all 'y' not preceeded by 'x', but doesn't include the prior character, which is the zero-width part.
Edited to add: Apparently this is only supported from Ruby 1.9 and up.
Negative look-behind is not supported until Ruby 1.9, but you can do something similar using scan:
"xy y ay xy +y".scan(/([^x])(y)/) # => [[" ", "y"], ["a", "y"], ["+", "y"]]
"xy y ay xy +y".scan(/([^x])(y)/).map {|match| match[1]} # => ["y", "y", "y"]
Of course, this is much more difficult if you want to avoid much more than a single character before the y. Then you'd have to do something like:
"abby y crabby bally +y".scan(/(.*?)(y)/).reject {|str| str[0] =~ /ab/} # => [[" ", "y"], [" ball", "y"], [" +", "y"]]
"abby y crabby bally +y".scan(/(.*?)(y)/).reject {|str| str[0] =~ /ab/}.map {|match| match[1]} # => ["y", "y" "y"]
Ruby unfortunately doesn't support negative lookbehind, so you'll have trouble if you need to look for more than a single character. For just one character, you can take care of this by capturing the match:
/[^x](y)/
In PCRE, you use a negative look-behind:
(:<!x)y
Not sure if this is supported by Ruby, but you can always look up.
It can be done with negative look behind, (?<!x)y

Resources