[Nuby Question] Return value of while loop.

B

Brian Schröder

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

Kristof Bastiaensen

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
 
O

Olivier D.

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

ts

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

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


Guy Decoux
 
B

Brian Candler

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

Andrew Johnson

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
 
B

Brian Candler

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

Brian Schröder

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

David A. Black

Hi --

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
 
J

James Edward Gray II

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
 
M

Markus

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
 
Z

Zach Dennis

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
 
M

Markus

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
 
D

David Ross

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

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top