Using parenthesis/barackets in ruby

Hi,

I’m new to Ruby, and have just started playing with it - nice.

I note that one big complaint from a-lot of folks is the lack of
parenthesis/barackets.

Old habits die hard - for some of us :slight_smile:

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…

#---------------------------------------------------
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 (’.’)
#---------------------------------------------------

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.

Cheers,
JV

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 ‘.’

Gary W.

On Jun 21, 2006, at 9:25 AM, [email protected] wrote:

Since you asked… The following is much more readable to me than your
example.

Minor updates to satisfy my own readability scale:

def print_dir(path)
Dir.foreach(path) do |file_name|

if File.directory? file_name
puts “[dir] #{file_name}”

  else

puts " #{file_name}"

  end

end
end
print_dir ‘.’

James Edward G. II

On Jun 21, 2006, at 15:07, Justin Vincent wrote:

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))…

matthew smillie.

example.

puts " #{file_name}"

  end

end
end
print_dir ‘.’

James Edward G. II

There is no need for control structures anyway:


def Dir.listing path
prefix = {‘directory’ => '[dir] ‘, ‘file’ => ’ ‘}
glob("#{path}/*").map{|file| prefix[File.ftype file] + file}
end
puts Dir.listing(’.’)

cheers

Simon

On Wed, 21 Jun 2006 20:21:28 +0200, Matthew S.
[email protected] wrote:

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.

But parens can act as block delimiters in Ruby:

a = (
?> p 1

p 2
3
)
1
2
=> 3

a
=> 3

Or:

a = (p 1; p 2; 3)
1
2
=> 3

Or even:

p((p 1; p 2; 3))
1
2
3
=> nil

Ruby evaluates all expressions inside the parens and the result of the
complete expression is the result of the last inner expression.

This behaviour can also produce (maybe) surprising results:

(1 +
?> 2)
=> 3

but

(1

=> 2

The second expression is evaluated as (1; +2), while the first is
evaluated as (1 + 2) (as expected).

Dominik

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs