[Nuby Question] Return value of while loop

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)

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

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

···

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

I can do that in 5 characters plus a newline:

i=128

:slight_smile:

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.

···

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

···

On 2004-09-18, Kristof Bastiaensen <kristof@vleeuwen.org> 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

--
Olivier D.

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 :slight_smile:

Brian.

···

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

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

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

Guy Decoux

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

-- MarkusQ

···

On Sat, 2004-09-18 at 08:36, ts wrote:

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

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

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

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

···

On Sun, 19 Sep 2004 01:25:07 +0900, Markus <markus@reality.com> wrote:

On Sat, 2004-09-18 at 08:36, ts wrote:

> 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

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

:~$ 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 :wink:

Cheers,

Brian

Hi --

> :~$ 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 :wink:

Obfuscation is in any case not a Ruby tradition :slight_smile: 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 :slight_smile:
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

···

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

--
David A. Black
dblack@wobblini.net

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

···

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 :wink:

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

···

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

Obfuscation is in any case not a Ruby tradition :slight_smile: 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 :slight_smile:
and the contest would be to make it clear and elegant.

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

martin

···

Markus <markus@reality.com> 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 :wink:

What were the criteria again? The declared winner

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:

Download
---------------------
http://rubyforge.org/frs/download.php/1522/wxrubylayouts-0.0.3.gem

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

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

···

On Tue, 2004-09-21 at 03:44, Martin DeMello wrote:

Markus <markus@reality.com> 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 :wink:
>
> What were the criteria again? The declared winner

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

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

-- maybe batsman can get it into rpa. much better --

···

Zach Dennis <zdennis@mktec.com> 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:

Download
---------------------
http://rubyforge.org/frs/download.php/1522/wxrubylayouts-0.0.3.gem

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!

David Ross wrote:

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

-- maybe batsman can get it into rpa. much better --

I am planning on releasing a source tarball later tonight. It is also on my list to contact batsman about getting it into the rpa.

Zach

tonight? :frowning: I want to try right now. Can you send me the tarball via email please :slight_smile: --dross

···

Zach Dennis <zdennis@mktec.com> wrote:David Ross wrote:

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

-- maybe batsman can get it into rpa. much better --

I am planning on releasing a source tarball later tonight. It is also on
my list to contact batsman about getting it into the rpa.

Zach

---------------------------------
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!

David Ross wrote:

tonight? :frowning: I want to try right now. Can you send me the tarball via email please :slight_smile: --dross

I 've got to go to a meeting. When I get out I will send you a tarball. /

Zach
/