Will Von Wizzlepig
Will Von Wizzlepig

Reputation: 73

Handing regex string to method

Within a method, I want to dynamically evaluate the following code chunk with a regex:

if (/^[A-F][A-Z]*[^\.\*]$/).match(some_value)

The method I attempted is this:

def check(val)
  if (/^[val][A-Z]*[^\.\*]$/).match(some_value)
    puts "foo"
  else
    puts "waa"
  end
end
check("A-F")

The value I am passing in is not making it there correctly. It appears that passing a value in this fashion needs something more. Is this not something you can do with a method?

Upvotes: 0

Views: 50

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

You expected string interpolation. To do that, you need to use the interpolation syntax #{}:

def check(val)
  if (/^[#{val}][A-Z]*[^\.\*]$/).match(some_value)
    puts "foo"
  else
    puts "waa"
  end
end

Upvotes: 3

Related Questions