Created
August 17, 2013 07:01
-
-
Save svineet/6255659 to your computer and use it in GitHub Desktop.
Method -chaining implementation of [VerEx](http://verbalexpressions.github.io/) in Ruby. The other [implementation](https://github.com/ryan-endacott/verbal_expressions) uses DSL. This is just a emperiment. May have a lot of bugs, and is not by any means complete.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a program trying to implement Verbal Expressions | |
# See this for more info - http://verbalexpressions.github.io/ | |
def VerEx | |
return VerExClass.new | |
end | |
class VerExClass | |
@regex = "" # The main regex variable | |
def intitialize reg | |
@regex = reg | |
end | |
def final_regex | |
return @regex | |
end | |
def add value | |
@regex << value | |
end | |
def start_of_line | |
if @regex == nil | |
@regex = "^" | |
else | |
self.add("^") | |
end | |
return self | |
end | |
def end_of_line | |
self.add("$") | |
return self | |
end | |
def find value | |
self.add("(" + Regexp.escape(value) + ")") | |
return self | |
end | |
def then value | |
return self.find Regexp.escape(value) | |
end | |
def maybe value | |
self.add("(" + Regexp.escape(value) + ")?") | |
return self | |
end | |
def anything | |
self.add("(?:.*)") | |
return self | |
end | |
def anything_but value | |
self.add("([^" + Regexp.escape(value) + "]*)") | |
return self | |
end | |
def something | |
self.add("(?:.+)") | |
return self | |
end | |
def something_but value | |
self.add("(?:[^" + Regexp.escape(value) + "]+)") | |
return self | |
end | |
def line_break | |
self.add("(?:(?:\n)|(?:\r\n))") | |
return self | |
end | |
def tab | |
self.add("\t") | |
end | |
def match matching_string | |
return Regexp.compile(@regex).match(matching_string) | |
end | |
end | |
# Tests - | |
if __FILE__ == $0 | |
reg2 = VerEx(). | |
start_of_line. | |
find('http'). | |
maybe('s'). | |
find('://'). | |
maybe('www.'). | |
anything_but(' '). | |
end_of_line() | |
if (reg2.match "https://www.google.com") | |
puts "Matched" | |
else | |
puts "Not matched" | |
end | |
puts reg2.final_regex | |
puts "^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$" | |
if %r ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$ .match "https://google.com" | |
puts "True" | |
end | |
end |
:) May I ask how?
Well, here goes:
- tabs for indentation instead of 2 spaces
- explicit return statements
- prefixing all method calls with
self.
- no parentheses around method arguments (both in definition and calls)
- misunderstanding of class vs instance variables (lines 9 and 12)
- explicit getter method rather than
attr_accessor
(line 15) - clumsy var initialization (lines 24-28)
- unnecessary parenthesis after method call without arguments (line 105)
Regexp.compile
rather thanRegexp.new
regexp.match(string)
rather thanstring =~ regexp
%r (regex)
rather than/(regex)/
The code works fine, mind, it's just not idiomatic Ruby. :) Also note that some people might disagree with some of the things I point out, mainly the last three points which are based more in personal preference than the others. They're still the norm though, in my experience.
See my fork: https://gist.github.com/DouweM/6269004. Points 1 through 9 and 11 have been addressed. 10 could be addressed by subclassing Regexp
instead of making this a new class entirely. This is the approach taken by the main Ruby VerEx project.
Interesting....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice job, but I have to say I enjoy how obvious it is that you're not actually a Ruby developer :)