Skip to content

Instantly share code, notes, and snippets.

@banister
Forked from judofyr/valid_ruby.rb
Created November 17, 2011 15:20
Show Gist options
  • Save banister/1373385 to your computer and use it in GitHub Desktop.
Save banister/1373385 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