# [Nuby Question] Return value of while loop.

Discussion in 'Ruby' started by Brian Schröder, Sep 18, 2004.

1. ### Brian SchröderGuest

Maybe this question would be more adequate for the ruby-nuby forum , but
while I'm writing my lecture slides about ruby, I saw that a while loop
always returns nil. Eg.

i = 1 # => 0
i *= 2 while i > 100 # => nil

I would have expected the loop to return 128. Is there an intention behind
this? Just thought I'd voice my "least surprise" to see if for others this
is also against the POTS.

PS: If you want you can see this as a warmup for our first Quiz . What
is the most concise way to write this:

i = 1
loop do
i *= 2
break i if i > 100
end

which would simulate the behaviour I'd expect of the 2-liner above. (No
semicolons allowed)

Brian Schröder, Sep 18, 2004

2. ### Brian SchröderGuest

> i = 1 # => 0
> i *= 2 while i > 100 # => nil

That was a typo, should have been
i = 1 # => 0
i *= 2 while i < 100 # => nil

cheers,

Brian

Brian Schröder, Sep 18, 2004

3. ### Kristof BastiaensenGuest

On Sat, 18 Sep 2004 10:43:18 +0200, Brian Schröder wrote:

> Maybe this question would be more adequate for the ruby-nuby forum , but
> while I'm writing my lecture slides about ruby, I saw that a while loop
> always returns nil. Eg.
>
> i = 1 # => 0
> i *= 2 while i > 100 # => nil
>
> I would have expected the loop to return 128. Is there an intention behind
> this? Just thought I'd voice my "least surprise" to see if for others this
> is also against the POTS.
>

Here is my guess:
I would suspect while to return the last statement. The last statement
will always be the test from the while, and always false. In this case
it may be more logical to return nil.

However it may be more useful if while returned the last value of the
block.

> PS: If you want you can see this as a warmup for our first Quiz . What
> is the most concise way to write this:
>
> i = 1
> loop do
> i *= 2
> break i if i > 100
> end
>
> which would simulate the behaviour I'd expect of the 2-liner above. (No
> semicolons allowed)

Here is a oneliner:
i = 128

Regards,
KB

Kristof Bastiaensen, Sep 18, 2004
4. ### Olivier D.Guest

On 2004-09-18, Kristof Bastiaensen <> wrote:
>> PS: If you want you can see this as a warmup for our first Quiz . What
>> is the most concise way to write this:
>>
>> i = 1
>> loop do
>> i *= 2
>> break i if i > 100
>> end

>
> Here is a oneliner:
> i = 128

I can do better in two different ways:

\$i <<= 1 while defined?(\$i) ? (\$i < 100) : (\$i = 1)

or worse, my entry for a future Ruby's obfuscated code contest:
i = (i ? i : 1) * 2 while defined?(i) ? ((i ? i : 1) < 100) : (i = 1)

But there is a strange behaviour I don't understand, if I try:
i <<= 1 while defined?(i) ? (i < 100) : (i = 1)
the variable i is 'defined' but set to nil, that's why the previous line
won't work as expected...

--
Olivier D.

Olivier D., Sep 18, 2004
5. ### tsGuest

>>>>> "O" == Olivier D <purple.dot_meteor@gmail_dot.com> writes:

O> i <<= 1 while defined?(i) ? (i < 100) : (i = 1)

i <<= 1 while i.nil?? (i = 1)i < 100)

Guy Decoux

ts, Sep 18, 2004
6. ### MarkusGuest

On Sat, 2004-09-18 at 08:36, ts wrote:
> >>>>> "O" == Olivier D <purple.dot_meteor@gmail_dot.com> writes:

>
> O> i <<= 1 while defined?(i) ? (i < 100) : (i = 1)
>
> i <<= 1 while i.nil?? (i = 1)i < 100)

i <<= 1 while (i = i||1) < 100

-- MarkusQ

Markus, Sep 18, 2004
7. ### Brian CandlerGuest

On Sat, Sep 18, 2004 at 05:44:44PM +0900, Brian Schrder wrote:
> PS: If you want you can see this as a warmup for our first Quiz . What
> is the most concise way to write this:
>
> i = 1
> loop do
> i *= 2
> break i if i > 100
> end
>
> which would simulate the behaviour I'd expect of the 2-liner above. (No
> semicolons allowed)

I can do that in 5 characters plus a newline:

i=128

Perhaps you should reword your question as:

# x is initialised to an Integer
i = 1
loop do
i *= 2
break i if i > x
end

in which case the smallest I can think of is:

i=1
i*=2 while i<=x
i

(the third line being necessary since you want the answer to be the value of
the overall expression, not just being left in 'i')

Regards,

Brian.

Brian Candler, Sep 18, 2004
8. ### Andrew JohnsonGuest

On Sun, 19 Sep 2004 01:25:07 +0900, Markus <> wrote:
> On Sat, 2004-09-18 at 08:36, ts wrote:
>> >>>>> "O" == Olivier D <purple.dot_meteor@gmail_dot.com> writes:

>>
>> O> i <<= 1 while defined?(i) ? (i < 100) : (i = 1)
>>
>> i <<= 1 while i.nil?? (i = 1)i < 100)

>
> i <<= 1 while (i = i||1) < 100

i *= 2 while (i = i||1) < 100

However, if I read the OP correctly, the interest wasn't the
shortest expression that sets i to 128 via doubling --- the
interest was in the shortest expression that "returned" the value of
i thusly set (sans semi-colons). The original example of what the
OP was after (in irb):

:~\$ irb
irb(main):001:0> i = 1
=> 1
irb(main):002:0> loop do
irb(main):003:1* i *= 2
irb(main):004:1> break i if i > 100
irb(main):005:1> end
=> 128

My first thought used the ability (for good or evil) to combine
if/while modifiers:

:~\$ irb
irb(main):001:0> i = 1
=> 1
irb(main):002:0> break i if i > 100 while i = i || 1 and i *= 2
=> 128

But a little rearranging results in a one liner:

:~\$ irb
irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
=> 128

regards,
andrew

--
Andrew L. Johnson http://www.siaris.net/
They're not soaking, they're rusting!
-- my wife (on my dishwashing habits)

Andrew Johnson, Sep 18, 2004
9. ### Brian CandlerGuest

On Sat, Sep 18, 2004 at 05:32:02PM +0100, Brian Candler wrote:
> Perhaps you should reword your question as:
>
> # x is initialised to an Integer
> i = 1
> loop do
> i *= 2
> break i if i > x
> end
>
> in which case the smallest I can think of is:
>
> i=1
> i*=2 while i<=x
> i

Ah, I forgot ||= to initialise, so if you're counting lines rather than
characters:

i*=2 while (i||=1)<=x
i

Of course, i<<=1 might be microscopically faster. But it's also one
character longer

Brian.

Brian Candler, Sep 18, 2004
10. ### Brian SchröderGuest

> :~\$ irb
> irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
> => 128

You won, and I'm even able to grasp what you have written. So no
extra points for obfuscation

Cheers,

Brian

Brian Schröder, Sep 18, 2004
11. ### David A. BlackGuest

Hi --

On Sun, 19 Sep 2004, Brian Schröder wrote:

> > :~\$ irb
> > irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
> > => 128

>
> You won, and I'm even able to grasp what you have written. So no
> extra points for obfuscation

Obfuscation is in any case not a Ruby tradition Actually we
almost had a de-obfuscation contest at the first Ruby conference. The
idea was that people would contribute horribly written, unclear code
(yes, I know that obfuscated doesn't have to mean horribly written
and the contest would be to make it clear and elegant. The contest
never happened; the conference, in November 2001, did take place, but
the events of that September slowed everyone down and some of the
things we'd planned fell by the wayside.

David

--
David A. Black

David A. Black, Sep 18, 2004
12. ### James Edward Gray IIGuest

On Sep 18, 2004, at 4:12 PM, David A. Black wrote:

> Obfuscation is in any case not a Ruby tradition Actually we
> almost had a de-obfuscation contest at the first Ruby conference. The
> idea was that people would contribute horribly written, unclear code
> (yes, I know that obfuscated doesn't have to mean horribly written
> and the contest would be to make it clear and elegant.

This is a good practice everyone should try once and a while. You can
learn a lot from Refactoring. We'll have to have a couple of Ruby
Quizzes like this...

James Edward Gray II

James Edward Gray II, Sep 18, 2004
13. ### MarkusGuest

On Sat, 2004-09-18 at 12:44, Brian SchrÃ¶der wrote:
> > :~\$ irb
> > irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
> > => 128

>
> You won, and I'm even able to grasp what you have written. So no
> extra points for obfuscation

What were the criteria again? The declared winner

(i *= 2) > 100 and break i while i = i || 1

is longer than either

(i <<= 1 while (i = i||1) < 100) || i

or

(i += i while (i ||= 1) < 100) || i

and so far as I can see does the same thing. Were they supposed to be
obscure? Or not? Or...?

-- Markus

Markus, Sep 18, 2004
14. ### Martin DeMelloGuest

Markus <> wrote:
> On Sat, 2004-09-18 at 12:44, Brian Schröder wrote:
> > > :~\$ irb
> > > irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
> > > => 128

> >
> > You won, and I'm even able to grasp what you have written. So no
> > extra points for obfuscation

>
> What were the criteria again? The declared winner

The criterion was to "break i" on loop exit.

martin

Martin DeMello, Sep 21, 2004
15. ### Zach DennisGuest

[ANN] wxRuby Layout Manager Library 0.0.3 Released!

wxRuby Layout Manager Library 0.0.3 has been released.

Overview
--------------------
The wxRuby Layout Manager Library is bringing the ease of use, layout
managers to wxRuby from other languages. It now supports the following
layout managers: BaseLayout, BoxLayout, FlowLayout and ParagraphLayout.
It is available now in RubyGem format from:

---------------------

Online Documentation Is Available!
-----------------------------------------------------------------
http://www.mktec.com/dev_www/wxrubylayouts/

The inline rdoc documentation has been updated in the RubyGem file and
even has copy and paste working code for each layout manager.

Plans For Upcoming Releases
----------------------------------------------------
Next Revision (0.0.4)
- BorderLayout
- GridLayout
- Update documentation with mor escreenshots and more workingexample code

Between Now and Next minor Release(0.1.0)
- CardLayout
- Spring Layout
- ViewLayout (custom implementation) A ViewLayout uses a ViewTemplate
to determine where to place widgets. More coming on this later...

Licensing Info
---------------------------
wxRuby Layout Manager Library is currently being releaesed under LGPL
(we use it alot at work, so i thought what the heck) but I am thinking
of changing this to a "Perl" compatible free license or to the ruby
license. Please feel free to email me if you have any thoughts on this
matter.

Any questions, comments or concerns you can feel free to email me.

Zach

Zach Dennis, Sep 21, 2004
16. ### MarkusGuest

On Tue, 2004-09-21 at 03:44, Martin DeMello wrote:
> Markus <> wrote:
> > On Sat, 2004-09-18 at 12:44, Brian SchrÃ¶der wrote:
> > > > :~\$ irb
> > > > irb(main):001:0> (i *= 2) > 100 and break i while i = i || 1
> > > > => 128
> > >
> > > You won, and I'm even able to grasp what you have written. So no
> > > extra points for obfuscation

> >
> > What were the criteria again? The declared winner

>
> The criterion was to "break i" on loop exit.

Pardon me if I am being dense, but I still don't see what you are
getting aT. What is the magic of break i? Wouldn't any control
structure that terminated the loop at the appropriate point and returned
i (as the value of the expression) be just as acceptable?

For example:

(i += i while (i ||= 1) < 100) || i

is semantically equivalent to

(i *= 2) > 100 and break i while i = i || 1

so far as I can see.

I tend not to use break (since it is "unstructured" in the GTCH
sense) so forgive me if I am just missing some obvious property or well
known behaviour of break.

-- MarkusQ

Markus, Sep 21, 2004
17. ### David RossGuest

Re: [ANN] wxRuby Layout Manager Library 0.0.3 Released!

--0-1938662796-1095781753=:4500
Content-Type: text/plain; charset=us-ascii

I need a tarball please. Gems are crap. kthnx, --dross

-- maybe batsman can get it into rpa. much better --
Zach Dennis <> wrote:
wxRuby Layout Manager Library 0.0.3 has been released.

Overview
--------------------
The wxRuby Layout Manager Library is bringing the ease of use, layout
managers to wxRuby from other languages. It now supports the following
layout managers: BaseLayout, BoxLayout, FlowLayout and ParagraphLayout.
It is available now in RubyGem format from:

---------------------

Online Documentation Is Available!
-----------------------------------------------------------------
http://www.mktec.com/dev_www/wxrubylayouts/

The inline rdoc documentation has been updated in the RubyGem file and
even has copy and paste working code for each layout manager.

Plans For Upcoming Releases
----------------------------------------------------
Next Revision (0.0.4)
- BorderLayout
- GridLayout
- Update documentation with mor escreenshots and more workingexample code

Between Now and Next minor Release(0.1.0)
- CardLayout
- Spring Layout
- ViewLayout (custom implementation) A ViewLayout uses a ViewTemplate
to determine where to place widgets. More coming on this later...

Licensing Info
---------------------------
wxRuby Layout Manager Library is currently being releaesed under LGPL
(we use it alot at work, so i thought what the heck) but I am thinking
of changing this to a "Perl" compatible free license or to the ruby
license. Please feel free to email me if you have any thoughts on this
matter.

Any questions, comments or concerns you can feel free to email me.

Zach

---------------------------------
Do you Yahoo!?
vote.yahoo.com - Register online to vote today!
--0-1938662796-1095781753=:4500--

David Ross, Sep 21, 2004