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

--
Posted via http://www.ruby-forum.com/.

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 Wright

路路路

On Jun 21, 2006, at 10:07 AM, Justin Vincent wrote:

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?

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., ((5*3)+2) == (5*3)+2 == 5*3+2. So, the following Ruby:
   if (a && b)...
Is similar to doing this in Java:
   if ((a && b))...

matthew smillie.

路路路

On Jun 21, 2006, at 15:07, Justin Vincent 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 Gray II

路路路

On Jun 21, 2006, at 9:25 AM, gwtmp01@mac.com 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.

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)

=> 2

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

Dominik

路路路

On Wed, 21 Jun 2006 20:21:28 +0200, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> wrote:

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

There is no need for control structures anyway:

路路路

-----Original Message-----
From: James Edward Gray II [mailto:james@grayproductions.net]
Sent: Wednesday, June 21, 2006 4:43 PM
To: ruby-talk ML
Subject: Re: Using parenthesis/barackets in ruby

On Jun 21, 2006, at 9:25 AM, gwtmp01@mac.com 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 Gray II

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

cheers

Simon