Is there any reason why I should not be doing this?
What is it actually doing from a code/processing point of view?
If there are no issues, I would like to work with ruby this way because
it helps me to visualize my code blocks, and I also find it useful
because my text editor can match brackets and easily highlight code
blocks.
On Jun 21, 2006, at 10:07 AM, Justin Vincent wrote:
(
Is there any reason why I should not be doing this?
Since you asked… The following is much more readable to me than your
example. That is why I wouldn’t throw in all the extra space and
parens.
Note, I didn’t get rid of all the parens that Ruby would allow me to,
just some of them. I doubt the extra parsing time introduced by the
spacing
and parens would be problematic but the extra bugs introduced by
having to
match all the extra parens would probably kill my productivity.
def print_dir(path)
Dir.foreach(path) do |file_name|
if File.ftype(file_name) == ‘directory’
print “[dir] #{file_name}\n”
else
print " #{file_name}\n"
end
end
end
print_dir ‘.’
I was curious to see if ruby could be programmed with
parenthesis/barackets even though it isn’t supposed to.
To my suprise this worked…
Is there any reason why I should not be doing this?
I think the other posters pretty aptly demonstrated the readability
aspect. On that front, I’ll point out that you’ve got an extra set
of parens around your if/else block: ((if…else…end)).
What is it actually doing from a code/processing point of view?
The parens in your Ruby code aren’t acting as block delimiters,
they’re just parenthesising expressions. Looking at it that way, it
shouldn’t be surprising that your code worked, as long as the
expressions were all in the right place (which they were). What may
be surprising is exactly how some expressions work in Ruby - the fact
that the if…else…end block returns a value is a little odd to
some people.
For a specific example, take conditionals:
Java: if (cond)… Ruby: if cond…
The parens around the condition are required by Java. In Ruby,
they’re not required. Both languages allow you to parenthesise
expressions, and parens don’t always change the result of an
expression; e.g., ((53)+2) == (53)+2 == 5*3+2. So, the following Ruby:
if (a && b)…
Is similar to doing this in Java:
if ((a && b))…