Comments on: A Beginner’s Guide to Practical Syntactic Magic: the tale of Hpricot’s sudo-constructor http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/ Thu, 19 Jun 2014 09:26:37 +0000 hourly 1 By: riffraff http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-275 Thu, 21 Jun 2007 17:36:26 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-275 just for your information: some of this methods are in the core ruby too, I remeber Integer(),FLoat(),String() and Array() 🙂

]]>
By: Nathan de Vries http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-274 Tue, 19 Jun 2007 07:51:44 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-274 I’d just like to point out that doc = Hpricot.parse(xml) is also available. I prefer to use it over the doc = Hpricot(xml) syntax, myself. In fact, the former is called by the latter anyway.
I’ve written up two examples of Hpricot use over on my blog, if you’d like to have a look and vote on which style you’d be more inclined to write, and would prefer to read.

]]>
By: KeithB http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-273 Mon, 18 Jun 2007 06:13:56 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-273

Which leaves us with the ability to write two snippets of code that, while they may look nearly the same, do very different things:

Ouch! This is not a help to the reader of the code. In fact, this is the sort of thing which makes reading someone else’s Perl sutch an adventure.
There’s a balance to be struck here: I dread working with those programmers who get wrapped up in an infinite regression of wanting to “know how it works” down to the metal before they’ll do anything with a line of code; on the other hand I don’t have much time for those who want to understand every line of code in front of them without ever doing any thinking either.
It’s probably OK (although not desirable) to have to read one level down to see what a method means, so it’s not so bad that this technique produces opaque usage as such, but still: laying a trap for the reader (which is what “look nearly the same, do very different things” amounts to) seems very wrong.

]]>
By: Thomas Lockney http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-272 Fri, 15 Jun 2007 11:47:56 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-272 A couple things:
Greg: thanks for writing stuff like this. It’s always interesting to me that more developers (particularly in the ruby community) don’t dig deep into stuff like this. I recall spending weeks last year pouring over why’s camping library trying to figure out all the little tricks he used.
Yes, these kinds of tricks can make for obtuse code in some cases, but then take a look an actual camping example and it’s quite concise and legible. Even more so to my eyes than, say, a Rails or Django app.
As for the “turtles all the way down” thing, I have to agree with Benjamin that it sounds like you might have mistaken the meaning of that statement. That said, I feel the need to point out Rubinius — a very clever attempt to get at Ruby implemented in Ruby (inspired by Smalltalk, of course). And, it *will* still support this kind of syntax.

]]>
By: Kevin Teague http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-271 Wed, 13 Jun 2007 20:27:56 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-271 This is an example of the

factory method pattern
, and if _why had been writing in Java, the culture of that language might have driven him to create:

HpricotFactoryImpl(my_document)

I imagine if you are going to break the rule of methods starting with lowercase, then a factory method would be the place to do it. Or perhaps _why was just wishing that we was writing in Python when he used the ‘ClassName(constructor_args)’ syntax.

]]>
By: Chris Carter http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-270 Wed, 13 Jun 2007 08:42:37 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-270 I think that the Hpricot(foo) syntax is a good idea, if you realize that it is just an alternate constructor. Usually the short-hand alternate constructors use [], but I think making it a () method call reads better than Struct[:name, :age]…

]]>
By: Benjamin Pollack http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-269 Tue, 12 Jun 2007 21:41:39 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-269 I don’t think “turtles all the way down” means what you think it means.
Turtles all the way down means the language implementation is written in, and fully accessible from, the language. If Ruby’s lexer, parser, and compiler were all written in Ruby and available from within Ruby, running on top of a tiny virtual machine, then Ruby would have turtles all the way down, just like Smalltalk or Common Lisp (but notably not most Schemes). In a Smalltalk environment, for example, I can go to the Compiler class and see how Smalltalk code is turned into bytecode. I can then tweak it if I want, giving it new capabilities, fixing bugs, or even replacing it entirely. This isn’t a hypothetical example, either; Squeak Smalltalk has modified its compiler in the past to add traits (similar to mixins) and change the way closures work without either changing the virtual machine one whit or touching a single line of C. *That’s* turtles-all-the-way-down.
If I’m inferring correctly, you think “turtles all the way down” means a kind of single paradigm for syntax à la Smalltalk, Lisp, Self, or Io. That’s an entirely different issue. In all of those but Smalltalk, there’d be nothing prohibiting doing the “Hpricot(foo)” trick–verbaitim in Io, in fact. Even in Smalltalk, though, you could, due to its turtles-all-the-way-down approach, modify the Compiler to handle your funky new syntax, if eschewing readability for terseness really floated your boat. You could even make it support the full Ruby syntax (and there are projects underway to do just that). You have more flexibility than Ruby, not less.
The thing is, I strongly agree with James on this one: Hpricot(foo) conveys one hell of a lot less to me as a code reader than even Hpricot.parse(foo) would. I’d still have to look up what on Earth Hpricot *does*, but at least I’d know what’s going on.
Don’t get me wrong; Hpricot::Base.new(foo).parse is a horrible API, but Hpricot(foo) is just as bad, because the reader can’t divine the intention of the writer. Sure, why’s method saves a few keystrokes, but at the expense of transparency and maintainability. There’s a perfectly reasonable middle ground, and James has it.

]]>
By: James Robertson http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-268 Tue, 12 Jun 2007 20:19:38 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-268 In Smalltalk, methods can begin with capital letters; it’s just not usually done. However, all messages do need a receiver, so in Smalltalk you would have to write something like:
Parser Hpricot: someXhtml.
Seeing as the method named Hpricot is badly named – it doesn’t say anything about what the method does – I’d instead write something like:
Parser parseXhtml: someXhtml.
which is way, way more obvious for the porr follow on developer who has to read the code.
Which leaves me wondering why you think using clever syntax that obscures meaning is a good thing? I prefer to leave that the C programmers, myself…

]]>
By: Cheap Reveal http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-267 Tue, 12 Jun 2007 08:17:27 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-267 CHUNKY BACON! CHUNKY BACON! CHUNKY BACON! CHUNKY BACON! CHUNKY BACON! CHUNKY BACON! CHUNKY BACON! CHUNKY BACON!
THANKS, GREAT LITTLE TUTORIAL

]]>
By: Dr Nic http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-266 Tue, 12 Jun 2007 02:12:25 +0000 http://urbanhonking.com/ideasfordozens/2007/06/12/a_beginners_guide_to_practical/#comment-266 Damn, it never occurred to me to ask “how does ‘Hpricot(…)’ work?” Thanks for asking the question AND sussing out the solution.

]]>