0

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?

1
  • It's not necessary to escape . or * inside [...]. [^.*] will suffice. Commented Jul 16, 2014 at 18:39

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ah! Dang, that was easy. I did not know you could use the #{} outside of quotes. Thank you!
Oh, gotcha- gotta click the check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.