5 ms·
Ruby is a language that encourages developing internal DSL's for a lot of problems. Having different syntaxes for the same thing gives you more flexibility in d
by philwelch 16y ago
Ruby is a language that encourages developing internal DSL's for a lot of problems. Having different syntaxes for the same thing gives you more flexibility in defining DSL's. For instance, the "f[:ruby]" form lets you define DSL's that intermix procs and hashes. The "f.(:ruby)" form is probably the new standard shorthand, but the explicit "f.call(:ruby)" is clearly there for backwards compatibility.
If you want a language where there's only one way to do it, I think there's a language with that exact motto.
- carbon8 16y ago"If you want a language where there's only one way to do it, I think there's a language with that exact motto." Be that as it may, in Ruby you should also follow conventions, write readable code, use the most obvious choice when possible and avoid being unnecessarily clever. Most of the core principles that Python promotes should also be heeded when writing Ruby code. Ruby gives you the flexibility to choose different options, but that doesn't mean that the flexibility should be abused.
- philwelch 16y agoConventions vary from DSL to DSL. The flexibility doesn't have to be used by the application programmer to be useful. Think of how gemfiles or Rspec would look without optional parentheses in method calls. Even though parens are usually the way to go, sometimes you want different conventions in your DSL.
- oewolf 16y agoAnd it isn't abuse until it is used in a specific abusive way. Having the options available to you isn't abusing it.
- silentbicycle 16y agoHow do all of these DSLs interact in close proximity? Ruby seems to do DSLs via a lenient lexer, rather than an explicit "quoting" mechanism, which means there isn't a way to locally specify a context for its idioms.
- philwelch 16y agoIt doesn't turn out to be a big problem since you don't generally mix idioms in the same file. In a Rails app, it's pretty easy to figure out which files are evaluated by which DSL. If you're using HAML, anything in app/views with a .haml extension is HAML. Anything under /spec is Rspec. Capfile, Gemfile, and Rakefile are self explanatory. routes.rb uses the Rails routing DSL.
- silentbicycle 16y agoAll right. Thanks for the polite and detailed response.