Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created November 17, 2011 15:20
Show Gist options
  • Save judofyr/1373383 to your computer and use it in GitHub Desktop.
Save judofyr/1373383 to your computer and use it in GitHub Desktop.
Checks if a string is valid Ruby
def valid_ruby?(str)
catch(:valid) {
eval("BEGIN{throw :valid,true};#{str}")
}
rescue SyntaxError
false
end
p valid_ruby?("123")
p valid_ruby?("'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment