Syntax proposal

I often write things like this:

class …
def …
raise “Directory #{dir} already exists.” if FileTest.directory?(dir)
end
end

Then I see it is too long to catch in just one glimpse, and one has to
read further to find the condition that actually triggers the
raising. So I rewrite it:

if FileTest.directory?(dir) then raise "Directory #{dir} already exists." end

The condition stands, but the line is even longer. So…

if FileTest.directory?(dir)
  raise "Directory #{dir} already exists."
end

And I’ve taken three lines in a method.

It happens sometimes that I have the three styles play ping-pong among
each other, and I change from one to another over various sessions
depending on what I had for lunch and other very scientific
considerations.

I was wondering if a little syntactic sugar would help, something
that is readable, makes the condition stand, can be read in one
glimpse, and lies on a single line.

What do you think of something like this?

FileTest.directory?(dir) -> raise "Directory #{dir} already exists."    

Massimiliano

Do you accept to have 3 characters rather than 2 ?

    FileTest.directory?(dir) -> raise "Directory #{dir} already exists."

       FileTest.directory?(dir) and raise "Directory #{dir} already exits"
       
Guy Decoux

How about:

FileTest.directory?(dir) ? raise “Directory #{dir} already exists.” : nil

Not ideal. Better than nothing.

Or better, put it in a method.

def check_dir_nonexistent(dir)
if FileTest.directory?(dir)
raise "Directory #{dir} already exists."
end
end

The exception will be propagated back
to the caller.

Hal

···

----- Original Message -----
From: “Massimiliano Mirra” list@NOSPAMchromatic-harp.com.web-hosting.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, July 30, 2002 11:47 AM
Subject: Syntax proposal

I was wondering if a little syntactic sugar would help, something
that is readable, makes the condition stand, can be read in one
glimpse, and lies on a single line.

What do you think of something like this?

FileTest.directory?(dir) -> raise "Directory #{dir} already exists."

Massimiliano Mirra list@NOSPAMchromatic-harp.com wrote in message news:20020730112259.GA2127@prism.localnet

What do you think of something like this?

FileTest.directory?(dir) -> raise "Directory #{dir} already exists."    

Does this work:

raise “Directory #{dir} already exists.” if FileTest.directory?(dir)

?

~ Patrick

Thanks to you, to Martin and to Hal for your suggestions. Yes, three
characters rather than two is fine. :slight_smile:

My mind has trouble parsing it, though; it goes `well, this and that
what?’

Just out of curiosity, anyway, this is what I had came up with:

class Object
def then
yield if self
end
end

FileTest.exists?(’/etc/passwd’).then { puts “exists” }

scream = proc { puts “ayeeee!” }

FileTest.exists?(’/mnt/windows’).then &scream

Massimiliano

···

On Wed, Jul 31, 2002 at 01:53:37AM +0900, ts wrote:

Do you accept to have 3 characters rather than 2 ?

FileTest.directory?(dir) -> raise "Directory #{dir} already exists."
   FileTest.directory?(dir) and raise "Directory #{dir} already exits"

Uhm, wasn’t this the first among my examples? :slight_smile:

Massimiliano

···

On Thu, Aug 01, 2002 at 09:20:19AM +0900, Patrick May wrote:

FileTest.directory?(dir) -> raise "Directory #{dir} already exists."    

Does this work:
raise “Directory #{dir} already exists.” if FileTest.directory?(dir)
?

Massimiliano Mirra wrote:

   FileTest.directory?(dir) and raise "Directory #{dir} already exits"

My mind has trouble parsing it, though; it goes `well, this and that
what?’

I try to imagine an implied “if so” after the and.

“Put on shoes and tie shoelaces.” =>
“Put on shoes and, if so, tie shoelaces.”

···

On Wed, Jul 31, 2002 at 01:53:37AM +0900, ts wrote:


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.

This is far more transparent than making another method and passing
blocks to the evaluated value, not to mention being less to type and
faster :slight_smile:

Subvocalisation is handy occasionally, but it’s not particularly a habit
you want to get into; it’s just one more layer your reading of the code
has to go through for you to understand it, and it doesn’t always map
that well to actual code :slight_smile:

···

On Wed, Jul 31, 2002 at 01:53:37AM +0900, ts wrote:

   FileTest.directory?(dir) and raise "Directory #{dir} already
   exits"

My mind has trouble parsing it, though; it goes `well, this and that
what?’


Thomas ‘Freaky’ Hurst - freaky@aagh.net - http://www.aagh.net/

MSDOS is not dead, it just smells that way.
– Henry Spencer

Hi –

···

On Fri, 2 Aug 2002, Thomas Hurst wrote:

On Wed, Jul 31, 2002 at 01:53:37AM +0900, ts wrote:

   FileTest.directory?(dir) and raise "Directory #{dir} already
   exits"

My mind has trouble parsing it, though; it goes `well, this and that
what?’

Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.

We do have logical shortcutting somewhat of this kind in English:

One step closer and you’re dead.

So it’s sort of like:

I find out you’re a directory and there’ll be trouble.

:slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I can think in logical operations, it’s when they blend too well with
a natural looking context that I have troubles. Paradoxically, `&&'
in shell scripts looks more natural to me, because there is no other
usual meaning associated to it and thus no source of confusion.

Massimiliano

···

On Fri, Aug 02, 2002 at 01:51:37AM +0900, Thomas Hurst wrote:

   FileTest.directory?(dir) and raise "Directory #{dir} already

My mind has trouble parsing it, though; it goes `well, this and that
what?'
Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.

You’re a genius. This will probably change my perspective on it. :slight_smile:

Massimiliano

···

On Fri, Aug 02, 2002 at 02:03:43AM +0900, David Alan Black wrote:

We do have logical shortcutting somewhat of this kind in English:

One step closer and you’re dead.

Massimiliano Mirra list@NOSPAMchromatic-harp.com writes:

···

On Fri, Aug 02, 2002 at 02:03:43AM +0900, David Alan Black wrote:

One step closer and you’re dead.

You’re a genius. This will probably change my perspective on it. :slight_smile:

Nah - he’s just got family in Texas… :slight_smile: