Hi find the below two blocks:
Block-I
irb(main):001:0> s="acbbdd"
=> "acbbdd"
irb(main):002:0> /e/=~s
=> nil
irb(main):003:0> if /e/=~s then
irb(main):004:1* print "h"
irb(main):005:1> end
=> nil
Block-II
irb(main):001:0> s="acbbdd"
=> "acbbdd"
irb(main):006:0> if /c/=~s then
irb(main):007:1* print "h"
irb(main):008:1> end
h=> nil
irb(main):009:0>
Could you please help me to understand how =~ works in I and II block? In the first block it doesn’t match and returns nil but in the second block how nil is coming?
In
II, /c/ matches s, so theprint "h"get executed. You get an output (the string “h”) and a return value from the print statement (nil)Then the if block returns the return value of the last statement in the block, in this case nil.
run
print "h"along will give you same result.