# Using parenthesis/barackets in ruby

**URL:** <https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039>\
**Category:** Ruby\
**Created:** [June 21, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039 "2006-06-21T16:07:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 21, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/1 "2006-06-21T16:07:52Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 21, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/2 "2006-06-21T16:27:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 21, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/3 "2006-06-21T16:46:36Z")

</div>

On Jun 21, 2006, at 9:25 AM, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 21, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/4 "2006-06-21T20:23:50Z")

</div>

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., ((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.

---

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 21, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/5 "2006-06-21T17:26:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jv2222](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jv2222](https://www.ruby-forum.com/u/jv2222)\
**Post date:** [June 22, 2006, 12:34am UTC](https://www.ruby-forum.com/t/using-parenthesis-barackets-in-ruby/64039/6 "2006-06-22T00:34:22Z")

</div>

On Wed, 21 Jun 2006 20:21:28 +0200, Matthew S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
> > 
> > - 
> > 1.

=\> 2

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

Dominik
