Ruby 1.9 wishlist

C

coderrr

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?

2) 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?

3) 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.

4) 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.

5) 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.
 
K

Ken Bloom

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

I'd like this too.
2) 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).
 
S

Suraj Kurapati

coderrr said:
2) 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
 
J

Jason Roelofs

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

coderrr

[Note: parts of this message were removed to make it a legal post.]
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.
 
C

coderrr

[Note: parts of this message were removed to make it a legal post.]
#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?
 
C

coderrr

[Note: parts of this message were removed to make it a legal post.]
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?
 
S

Suraj Kurapati

coderrr said:
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.
 
R

Rick DeNatale

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
 
C

coderrr

[Note: parts of this message were removed to make it a legal post.]
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.
 
K

Ken Bloom

[Note: parts of this message were removed to make it a legal post.]
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?

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
 
M

Marc Heiler

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 :)
[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 ...
 
C

Christopher Dicely

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.
2) 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 :eek:utside_of_conditional

3) rescue a specific exception type inline

I like that.
4) 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.
5) 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 %{ ... }?
 
K

Ken Bloom

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 :) [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
 
C

coderrr

[Note: parts of this message were removed to make it a legal post.]

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 ? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top