I’m solving a Ruby Quiz, to be precise #4. It requires to Monkey Patch the Regexp class and add a build() method to it, that does the following:
lucky = Regexp.build(3, 7)
"7" =~ lucky # => true
"13" =~ lucky # => false
"3" =~ lucky # => true
I’m pretty much there, Here’s what I’ve got so far…
> lucky = Regexp.build(3, 7)
> "7" =~ lucky => 0
> "13" =~ lucky => nil
> "3" =~ lucky => 0
As you can see, my only problem here is that I’m not returning a true or false, but 0 and nil instead.
Could you guys please help me getting an idea of how to override the =~ operator to accomplish this? (If this is the way to go) or let me know what is the right thing to do.
Thanks in advance.
There’s a Ruby idiom for turning something into a boolean,
!!.nilandfalseare falsey, negating them yieldstrue, which in turn negates tofalse. Any truthy value first gets turned into afalseand then becomestrue:It’s hard to give you more hints without seeing your code, but it seems like putting a
!!in at the right spot will solve your problem.