# Return values of modifiers

**URL:** <https://www.ruby-forum.com/t/return-values-of-modifiers/187848>\
**Category:** Ruby\
**Created:** [April 30, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848 "2010-04-30T12:37:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/1 "2010-04-30T12:37:55Z")

</div>

This is logical, but no very useful :

irb(main):007:0\> x = 10  
=\> 10  
irb(main):008:0\> x = x + 1 if false  
=\> nil  
irb(main):009:0\> x = x + 1 if true  
=\> 11

Idem, a while modifier always returns nil

irb(main):010:0\> x += 1 while x \< 15  
=\> nil

It would be nice to get the return value of the last instruction  
_before_ the test fails.

\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/2 "2010-04-30T14:57:58Z")

</div>

2010/4/30 Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):010:0\> x += 1 while x \< 15  
> =\> nil
> 
> It would be nice to get the return value of the last instruction  
> _before_ the test fails.

Why? What use case is there that would require this or what code  
would become (much) more elegant?

Note that the cost (in terms of memory) might be significant if the  
expression before “while” returns a large volume of data on each  
iteration. That’s probably irrelevant for most cases but it may cause  
a nasty memory issues in rare cases because every last value needs to  
be kept for the duration of the next loop execution.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/3 "2010-04-30T15:16:11Z")

</div>

Robert K. wrote:

> 2010/4/30 Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > irb(main):010:0\> x += 1 while x \< 15  
> > =\> nil
> > 
> > It would be nice to get the return value of the last instruction  
> > _before_ the test fails.
> 
> Why? What use case is there that would require this or what code  
> would become (much) more elegant?

I was just cleaning a library and found two methods ending with  
something like

ind += 1 while \<some\_test\>  
ind  
end

and tried to get rid of the last line, quite ugly and looking  
unnecessary. You’re right : not _much_ more elegant. But once you get  
used to syntactic sugar, you’ll ask for some more … Remember Dijsktra  
: “abuse of syntactic sugar gives cancer of the semi-colon”.  
\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 5:28pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/4 "2010-04-30T17:28:42Z")

</div>

David A. Black wrote:

> In the case of the while in modifier position, it looks like it’s  
> equivalent to this:
> 
> while \<some\_test\>  
> ind += 1  
> end
> 
> and it is. I prefer this to having it be equivalent to:
> 
> loop do x += 1; break x if \<some\_test\>; end
> 
> David

Yes, that was my point.  
\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/5 "2010-04-30T15:47:44Z")

</div>

Hi –

On Fri, 30 Apr 2010, Michel D. wrote:

> > would become (much) more elegant?  
> > used to syntactic sugar, you’ll ask for some more … Remember Dijsktra  
> > : “abuse of syntactic sugar gives cancer of the semi-colon”.

I don’t think there’s any syntactic sugar here. I always think of  
syntactic sugar as “It looks like it’s doing X, but it’s really [or  
also] doing Y.” For example, x \* y, where it looks like an infix  
operator but is really sending a message to an object.

In the case of the while in modifier position, it looks like it’s  
equivalent to this:

while \<some\_test\>  
ind += 1  
end

and it is. I prefer this to having it be equivalent to:

loop do x += 1; break x if \<some\_test\>; end

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/6 "2010-04-30T17:32:29Z")

</div>

> Yes, that was my point.  
> \_md  
> @David : Sorry, I misunderstood you, English is not my native tongue. I  
> read the opposite "I prefer it to be equivalent …  
> \_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/7 "2010-04-30T17:32:37Z")

</div>

Hi –

On Sat, 1 May 2010, Michel D. wrote:

> > loop do x += 1; break x if \<some\_test\>; end
> > 
> > David
> 
> Yes, that was my point.

I thought you were saying you _did_ want it to be equivalent to that  
loop construct, rather than to the non-modifier while construct. Did I  
misunderstand?

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/8 "2010-04-30T22:17:44Z")

</div>

Sure, it would be nice while/until loops return value of the last  
iteration,  
I agree.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 8:38am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/9 "2010-05-01T08:38:30Z")

</div>

Benoit D. wrote:

> Sure, it would be nice while/until loops return value of the last  
> iteration,  
> I agree.

This boils down to the fact that ‘if false ; end’ returns nil, hence  
means ‘do nil’ and not ‘do nothing’ (as a comment does).

Inserting a piece of code between ‘if false’ and ‘end’ is not equivalent  
to commenting it out, and does not means ‘jump over it’. I feel it  
should.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [April 30, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/10 "2010-04-30T19:13:25Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sat, 1 May 2010, Michel D. wrote:
> 
> > > loop do x += 1; break x if \<some\_test\>; end
> > > 
> > > David
> > 
> > Yes, that was my point.
> 
> I thought you were saying you _did_ want it to be equivalent to that  
> loop construct, rather than to the non-modifier while construct. Did I  
> misunderstand?

No, you did not. This is was I suggested. But I misunderstood your first  
comment, guessing you agreed with me. Sorry for the noise.  
\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 8:53am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/11 "2010-05-01T08:53:17Z")

</div>

On Sat, May 1, 2010 at 8:38 AM, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Benoit D. wrote:

> Inserting a piece of code between ‘if false’ and ‘end’ is not equivalent  
> to commenting it out, and does not means ‘jump over it’. I feel it  
> should.  
> The parser parses it, that is all, it is never executed I fail to see  
> what your desired behavior is?  
> Cheers  
> R.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 9:23am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/12 "2010-05-01T09:23:04Z")

</div>

Robert D. wrote:

> On Sat, May 1, 2010 at 8:38 AM, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Benoit D. wrote:
> 
> > Inserting a piece of code between ‘if false’ and ‘end’ is not equivalent  
> > to commenting it out, and does not means ‘jump over it’. I feel it  
> > should.  
> > The parser parses it, that is all, it is never executed I fail to see  
> > what your desired behavior is?  
> > Cheers  
> > R.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 9:30am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/13 "2010-05-01T09:30:00Z")

</div>

Michel D. wrote:

> I would like the return value of a block ‘if ; … ; end’ to be  
> the last return value before parsing the ‘if’. As if the block did not  
> exist, or was commented out.

I am not familiar with the ruby parser. What I suggest could be just to  
pop the stack when exiting from the block.  
\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 9:25am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/14 "2010-05-01T09:25:53Z")

</div>

Michel D. wrote:

> Robert D. wrote:
> 
> > On Sat, May 1, 2010 at 8:38 AM, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > Benoit D. wrote:
> > 
> > > Inserting a piece of code between ‘if false’ and ‘end’ is not equivalent  
> > > to commenting it out, and does not means ‘jump over it’. I feel it  
> > > should.  
> > > The parser parses it, that is all, it is never executed I fail to see  
> > > what your desired behavior is?  
> > > Cheers  
> > > R.

I would like the return value of Ã block ‘if ; … ; end’ to be  
the last return value before parsing the ‘if’. As if the block did not  
exist, or was commented out.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 10:33am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/15 "2010-05-01T10:33:22Z")

</div>

On Sat, May 1, 2010 at 9:30 AM, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Michel D. wrote:
> 
> > I would like the return value of a block ‘if ; … ; end’ to be  
> > the last return value before parsing the ‘if’. As if the block did not  
> > exist, or was commented out.
> 
> I am not familiar with the ruby parser. What I suggest could be just to  
> pop the stack when exiting from the block.  
> \_md  
> It is impossible, how could the parser continue its work??

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 10:28am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/16 "2010-05-01T10:28:06Z")

</div>

On 2010-05-01, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like the return value of à block ‘if ; … ; end’ to be  
> the last return value before parsing the ‘if’. As if the block did not  
> exist, or was commented out.

That would seem very surprising to me. In general, I do not expect the  
return value of an expression to be the return value of some other  
expression  
which was in no way a part of it.

-s

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 3:30pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/17 "2010-05-01T15:30:39Z")

</div>

What I meant is:

> i = 0  
> =\> 0  
> while i \< 10  
> i += 1  
> 33  
> end  
> =\> nil # should be 33

This is already the case for class Foo..end, and if/unless

It should be for the loops too (without explicit break).

And when the condition is wrong and then the “block” is never used,  
then nil should be returned.

What do you think?

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/18 "2010-05-01T16:17:03Z")

</div>

Benoit D. wrote:

> What I meant is:
> 
> > i = 0  
> > =\> 0  
> > while i \< 10  
> > i += 1  
> > 33  
> > end  
> > =\> nil # should be 33
> 
> This is already the case for class Foo..end, and if/unless
> 
> It should be for the loops too (without explicit break).
> 
> And when the condition is wrong and then the “block” is never used,  
> then nil should be returned.
> 
> What do you think?

Benoit, if you have only  
i = 0  
while i \< 10  
i += 1  
end  
which is the same as  
i = 0  
while i \< 10  
i += 1  
i  
end  
then it should return 10, as I wanted ?  
\_md

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/19 "2010-05-01T16:23:19Z")

</div>

2010/5/1 Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> i = 0  
> \_md  
> Sure, that’s my point.

(But I think the ‘if/unless’ behavior is correct and “foo if false”  
should return nil as it is)

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [May 1, 2010, 11:29am UTC](https://www.ruby-forum.com/t/return-values-of-modifiers/187848/20 "2010-05-01T11:29:54Z")

</div>

Robert D. wrote:

> It is impossible, how could the parser continue its work??

OK !

[Next page](https://www.ruby-forum.com/t/return-values-of-modifiers/187848.md?page=2)
