Skip to main content

Posts

Showing posts with the label sub

Ternary Operators, Hash and Regex

- Doing name_shuffler.rb kata and competitive_eating scoreboard.rb - More on gsub Basically, anything in [xxx] will be replaced.  "hello".gsub(/[aeiou]/, '*')                  #=> "h*ll*" the \1 means the group number. Has to come with a ([xxx]). Can be \any_number "hello".gsub(/([aeiou])/, '<\1>')             #=> "h<e>ll<o>" Using a block, sub everything  "hello".gsub(/./) {|s| s.ord.to_s + ' '}      #=> "104 101 108 108 111 " This is the use of a group name. It says to replace everything with the group name with similar words from the group. ?<groupname>[xxx] and \k<groupname>. "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}" This is to replace specifically.  'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*" - T...