[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby 1.9 wishlist

coderrr

5/2/2008 4:38:00 PM

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.

15 Answers

Ken Bloom

5/2/2008 4:49:00 PM

0

On Fri, 02 May 2008 11:37:33 -0500, coderrr 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 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).

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Suraj Kurapati

5/2/2008 5:03:00 PM

0

coderrr wrote:
> 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
--
Posted via http://www.ruby-....

Jason Roelofs

5/2/2008 5:31:00 PM

0

#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

coderrr

5/2/2008 5:34:00 PM

0

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

coderrr

5/2/2008 5:40:00 PM

0

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

coderrr

5/2/2008 5:46:00 PM

0

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

Suraj Kurapati

5/2/2008 5:58:00 PM

0

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.
--
Posted via http://www.ruby-....

Rick DeNatale

5/2/2008 6:14:00 PM

0

On Fri, May 2, 2008 at 1:57 PM, Suraj Kurapati <snk@gna.org> 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.denh...

Jeremy Henty

5/2/2008 6:48:00 PM

0

On 2008-05-02, coderrr <coderrr.contact@gmail.com> 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 Henty

coderrr

5/2/2008 6:55:00 PM

0

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