I’m having a bit of a problem with Ruby playing nice when it comes to
combining an eval, a combination of lines in a HereDoc, and a regular
expression. Consider this code:
a = 42
puts “yes” if a =~ /\d{2}/ => “yes”
eval(“puts ‘yes’ if a =~ /\d{2}/”) => nil
eval(‘puts “yes” if a =~ /\d{2}/’) => “yes”
OK, so now I realize I should just encapsulate my eval statement inside
a single quote to make my regular expression process correctly. Enter
HereDoc:
a = 42
eval <<-STR
puts “hi” if a =~ /\d{2}/
STR
Doing that code in the console makes it quite obvious that HereDoc
encapsulates things inside double quotes, which would break the regular
expression. My question is whether there is an alternative that will
allow me to include regular expressions into my statement and actually
have them process.
Obviously for such a short snippet of code, I can put everything inside
a single-lined, single-quoted string and be done with it. My problem is
that I’m defining a method within my eval statement that spans 3 or 4
lines, and readability would be absolutely shot if I had to add in a
bunch of semi-colons and escape characters and such.
Thanks,
Adam