Ruby 1.9 wishlist

Hey I just put together a list of stuff I totally wish Ruby had and I
think would make a lot of people’s lives better.

In the order of importance:

  1. rescue/ensure available in all blocks
    arr.each do |e|
    e.do_something
    rescue MyException
    p :doh!
    ensure
    p :every_time!
    end

It’s totally gross to have to use begin/end blocks inside of do/end.
Is there any reason why it’s not possible to do this?

  1. two line if/while statements (like c):
    if true
    p :true
    p :im_outside_of_if

I’m a huge fan of one liners like:
x if y

… but sometimes it just doesn’t fit on one line and you need to do
if x … end. Needing an extra line for that end really really
sucks. Shouldn’t we be able to do it C style and save a line?

  1. rescue a specific exception type inline
    do_something_dangerous rescue(SpecificException) p :OH_NO!
    s = (do_something_that_times_out rescue(Timeout::Error) nil)

Timeout::Error is the big reason for wanting this one since it doesn’t
inherit from StandardError. But there are many other cases in which
it would be nice too.

These next two go together and I don’t really care that much about
them.

  1. inline do/while statements:
    d = s.read dowhile d

I dislike that the inline while functions exactly like a normal while
loop, checking the while condition before the initial run. I would
prefer if it acted like a begin … end while loop, checking the
while condition after the first iteration. I don’t really care to
change how the while keyword works, but it’d be nice if a new keyword
acted this way, maybe dowhile? I haven’t thought up a good name yet.

  1. more concise (inline) syntax for begin…end
    begin ret = a.delete something end while ret <— gross
    begin { ret = a.delete something } while ret
    or: do { … } while

An alternative to #4 is just to make writing begin/end blocks much
nicer. Perhaps an inline version like: do { } Then it would be
simple to get the while functionality I desire.

Anyone with me on this stuff?? I would really love to see #1-3 in
Ruby 1.9.

On Fri, 02 May 2008 11:37:33 -0500, coderrr wrote:

p :every_time!
end

I’d like this too.

  1. two line if/while statements (like c): if true
    p :true
    p :im_outside_of_if

This is not possible without turning Ruby into either Python
(semantically significant whitespace) or Perl (required semicolons).

coderrr wrote:

  1. two line if/while statements (like c):
    if true
    p :true
    p :im_outside_of_if

I’m a huge fan of one liners like:
x if y

… but sometimes it just doesn’t fit on one line and you need to do
if x … end. Needing an extra line for that end really really
sucks. Shouldn’t we be able to do it C style and save a line?

IMHO a C-style single-line if statement is more trouble than it’s worth.
It makes me cringe every time I see such a thing in C/C++/Java code (I
always put braces around my code blocks, just like Perl requires).

Besides, who needs a C-style if when we have shell-style boolean
expressions?

example 1

true and
p :true

p :im_outside_of_if

example 2 – beware of ‘&&’ vs ‘and’ precedence

true &&
(p :true)

p :im_outside_of_if

#1: makes sense. You can do that with regular methods, but not blocks?
It makes sense implementation wise (they aren’t the same thing), but
can be confusing.

#2: Absolutely not. It leads to harder to read code and well hidden
bugs. Every time I run into such a thing in C / C++ / Java, I add
brackets.

#3 Also one that makes sense, but is more of a personal preference
thing.

#4 I’ve never had a need for it but in terms of consistency I can see
it working.

Jason

IMHO a C-style single-line if statement is more trouble than it’s worth.
It makes me cringe every time I see such a thing in C/C++/Java code (I
always put braces around my code blocks, just like Perl requires).

I don’t care to use the single line if as we already have that (just in
reverse order). I want a double line if, which has never made me
cringe,
although of course this is subjective.

Besides, who needs a C-style if when we have shell-style boolean
expressions?

example 1

true and
p :true

p :im_outside_of_if

Awesome, I’ve learned something new. I never though about a newline
after
an &&. I’ll give this a try and see how it feels. Although from the
looks
of it, a two-line if would read better.

This is not possible without turning Ruby into either Python
(semantically significant whitespace) or Perl (required semicolons).

Are you sure? While loops already require “semantically significant
whitespace” or a semicolon:

while true p :hi end

doesn’t work

while true; p :hi end

or

while true
p :hi end

Isn’t that kinda the same thing?

coderrr wrote:

who needs a C-style if when we have shell-style boolean expressions?

example 1

true and
p :true

p :im_outside_of_if

Awesome, I’ve learned something new.

In fact, an if statement in Ruby is nothing more than shell-style
booleans:

if condition then main_stuff else other_stuff end

condition and main_stuff or other_stuff

condition && main_stuff || other_stuff

All of these do the same thing; they just they have different
precedence.

I never though about a newline after an &&.

Yes, any operator at the end of a line causes Ruby to treat the next
line as part of the overall statement. They’re like an implicit \ at
the end of a line.

#2: Absolutely not. It leads to harder to read code and well hidden
bugs. Every time I run into such a thing in C / C++ / Java, I add
brackets.

Hrm I had a feeling this one might be controversial. I never had an
issue
with it in C as long as the if was broken onto two lines and not one.
Do
you know if there have been any debates/discussions over this already?

On Fri, May 2, 2008 at 1:57 PM, Suraj K. [email protected] wrote:

In fact, an if statement in Ruby is nothing more than shell-style
booleans:

if condition then main_stuff else other_stuff end

condition and main_stuff or other_stuff

condition && main_stuff || other_stuff

Not quite

val = if true
false
else
true
end

val # => false

val = true && false || true
val # => true


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

I usually find I can do this fairly neatly by breaking the line after
the “if”:

x if
y

Yea, I’m aware you can do this but I find it extremely ugly and hard to
understand. When its on one line in this order (statement, condition)
it’s ok but for some reason two lines makes it horrible. When it’s two
lines in the opposite order (condition, statement) it reads great to me.

On 2008-05-02, coderrr [email protected] wrote:

I’m a huge fan of one liners like:
x if y

… but sometimes it just doesn’t fit on one line and you need to do
if x … end.

I usually find I can do this fairly neatly by breaking the line after
the “if”:

x if
  y

Two lines, no “end”! And if you really want to abuse it:

foo(bar,
    baz
    ) if
  this &&
  that &&
  the_other

It’s not pretty and should probably be refactored but it’s
straightforward to type and not so hard to understand later.

Regards,

Jeremy H.

On Fri, 02 May 2008 12:46:25 -0500, coderrr wrote:

doesn’t work

while true; p :hi end

or

while true
p :hi end

Isn’t that kinda the same thing?

You’ve got an end on the end of that while loop that you didn’t have for
the if statement.

You could do
if true
p :true end
and that’s already in the language

You could do
if true
p :true end
and that’s already in the language

But in all fairness, this is not the same as he said:

I’m a huge fan of one liners like:
x if y
… but sometimes it just doesn’t fit on one line and you need to do
if x … end. Needing an extra line for that end really really
sucks.

I can understand this partially. If one wants to fit in ~80 chars /
line,
and even has a few variables that may be a tad longer, or more
conditions
to check, that line would quickly get quite long, and thus you
will naturally end up doing a newline (and then an end).

I dont think the end should be on the same line at all. In fact what
he seems to have suggested was to completely drop the end in this
scenario. This seems to be not possible, but it is simply not the
same as putting the end on the same line :slight_smile:
[Not sure if you agree, but I think the end on a newline is
easier to read than an end on the same line]

FWIW I personally do not mind the newline+end at all. I dont really
think it leads to ugly code or code that “sucks”.

On the other hand though, I would like to have some language
that allows this dropping of closing clauses without writing it,
but which is also “more ruby than python” (I dont really like the def
foo(self): stuff …)

It also seems though that most languages out there simply have a
really horrible syntax, but maybe ruby spoiled me …

On Fri, May 2, 2008 at 9:37 AM, coderrr [email protected]
wrote:

Hey I just put together a list of stuff I totally wish Ruby had and I
think would make a lot of people’s lives better.

In the order of importance:

  1. rescue/ensure available in all blocks

I agree.

  1. two line if/while statements (like c):
    if true
    p :true
    p :im_outside_of_if

Can’t agree with this; I don’t really like it, and if you really need a
two-line conditional, you can split the single-line form with a line
continuation, like

p :true
if true
p :outside_of_conditional

  1. rescue a specific exception type inline

I like that.

  1. inline do/while statements:
    d = s.read dowhile d

I dislike that the inline while functions exactly like a normal while
loop, checking the while condition before the initial run. I would
prefer if it acted like a begin … end while loop, checking the
while condition after the first iteration. I don’t really care to
change how the while keyword works, but it’d be nice if a new keyword
acted this way, maybe dowhile? I haven’t thought up a good name yet.

I see where you’re coming from, but think the best way to deal with
this, as you suggest below, is to provide a compact begin … end
syntax, especially for single-line use.

  1. more concise (inline) syntax for begin…end
    begin ret = a.delete something end while ret <— gross
    begin { ret = a.delete something } while ret
    or: do { … } while

An alternative to #4 is just to make writing begin/end blocks much
nicer. Perhaps an inline version like: do { } Then it would be
simple to get the while functionality I desire.

I like this, but fear that the potential to appear to be a method
call (or to conflict with method calls in existing code, if someone
has defined “do” methods on certain objects) might be a problem;
it would probably be better to have something that didn’t have that
problem. Maybe something like %{ … }?

So it sounds like everyone is pretty much in agreement on #1, having
rescue/ensure available in all blocks w/o needing a begin/end. Now does
anyone know how to lobby Matz to get this into 1.9 ? :slight_smile:

On Fri, 02 May 2008 18:52:46 -0500, Marc H. wrote:

x … end. Needing an extra line for that end really really sucks.
This seems to be not possible, but it is simply not the same as putting
the end on the same line :slight_smile: [Not sure if you agree, but I think the end
on a newline is easier to read than an end on the same line]

FWIW I personally do not mind the newline+end at all. I dont really
think it leads to ugly code or code that “sucks”.

On the other hand though, I would like to have some language that allows
this dropping of closing clauses without writing it, but which is also
“more ruby than python” (I dont really like the def foo(self): stuff
…)

Python’s whitespace isn’t the dealbeaker for me about that langage. It’s
the need to explicitly mention self in the parameter list.

It also seems though that most languages out there simply have a really
horrible syntax, but maybe ruby spoiled me …

I could see a way to do it, by using the presence/absence of the “then”
keyword, but that would be very unintutitive, and we’d be explaining to
newbies the difference 10 times a day here on ruby-talk. Best to leave
well enough alone.

–Ken

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