Ad
  • Default User Avatar

    I also prefer Oxford commas.

    They're nice, unambiguous, and easy on the eyes. ;)

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar
  • Custom User Avatar

    It took me a while to figure out what's happening here. In case anyone else is interested.
    Basically, any Regexp without closed parentheses will throw an exception, which Insti rescues by returning false. Tricky.

    Just punch this in your repl:

    /asdas()/
    Regexp.new("gf()((asda)asd)()sds") rescue "I won't need rescuing"
    Regexp.new("gf(sds") rescue "please rescue me!!!"
    

    The trickier part is tilde. I found the answer here - Stackoverflow & Ruby Regexp Docs very last method #~

    It's matching the Regexp to the last 'gets' prompt (only if it's valid, i.e., only if it has closed or no parenthesis). The variable $_ holds the most recent and scoped value of gets, which, in the case of #valid_parentheses is nothing, nada, nil.

    For illustration:

    def what_does_tidle_do?(string)
      print "Type something: "
      gets.chomp
      puts "Your last gets statement was, '" + $_.chomp + "', now I'll find the first index of what you typed against your regexified method argument."
      ~ Regexp.new(string)
    end
    
    > what_does_tidle_do?("cheese")
     Type something: string cheese mmmm
     Your last gets statement was, 'string cheese mmmm', now I'll find the first index of what you typed against your regexified method argument.
    => 7