ryan-endacott / verbal_expressions

Make difficult regular expressions easy! Ruby port of the awesome VerbalExpressions repo - https://github.com/jehna/VerbalExpressions
MIT License
571 stars 26 forks source link

DSL vs. method chaining? #4

Open joem opened 11 years ago

joem commented 11 years ago

This isn't so much an 'issue' as it simply 'something to consider':

I noticed that this is one of the only (if not the only) implementation listed on the organization page that doesn't use method chaining for the start_of_line(), find(), maybe(), etc. methods...

I wonder if it might be better to follow the original's method chaining strategy instead of this DSL approach? I'm pretty sure there are things possible with method chaining that you can't do with the DSL (although admittedly I'm not sure if any of it's actually useful). Also, it might make it easier to translate and keep up with any possible changes to the other versions if it were more closely following their implementations.

What's your take?

jgabrielygalan commented 11 years ago

Not that I'm advocating one way over the other, but using method chaining instead of using a block would allow to use reserved words such as "then", "or" and so on.

DouweM commented 11 years ago

Using a DSL inside a block is more Ruby-like though.

joem commented 11 years ago

DSLs have indeed become an inherent part of Ruby culture and I don't want to sound like I'm DSL-bashing, but I'm not sure if I'd agree that a DSL inside a block is more Ruby-like than method chaining. Using DSLs is a programming pattern that the community has adopted due to the ease of implementation in the language, whereas method chaining is a built-in feature of the language.

ryan-endacott commented 11 years ago

I did originally use the DSL because it felt more Rubyist, but I think that the decision could be revisited.

@jgabrielygalan, that's another valid point I hadn't considered. It would be nice to have the reserved words.

@joem, As far as keeping up with the other versions, most of the implementation stays the same (besides the reserved words). After the initial DSL setup, everything was ported line-by-line. With a few slight modifications, it could be made to work with chaining or the DSL, but I'm not sure that's what we should do.

The DSL also offers some extra benefits. For example, it makes the capture syntax possible. Without it, you would have to create an entirely new VerEx object for nesting. There may be another way to get around that by messing with the scope of evaluation, but nothing immediately comes to mind.

I feel like the DSL is cleaner and fits better for a Ruby library, but that's just my personal opinion.

svineet commented 11 years ago

Hey, just for fun, I tried implementing a method chaining VerEx Ruby package. It's only one file and not by any means complete, May have many bugs, but still passes the URL testing example on the README. Here it is.

ryan-endacott commented 11 years ago

@saivineet89, very cool! You actually may be able to tweak the existing library to make it work with chaining and then everything else will work the same.

svineet commented 11 years ago

@ryan-endacott , Thanks :) I tweaked the library now.

svineet commented 11 years ago

Now both DSL and method chaining are supported(I've only tested the URL test on README and it works :)