# Syntactic sugar idea

**URL:** <https://www.ruby-forum.com/t/syntactic-sugar-idea/167931>\
**Category:** Ruby\
**Created:** [May 14, 2009, 4:44am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931 "2009-05-14T04:44:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 4:44am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/1 "2009-05-14T04:44:14Z")

</div>

It seems that often an object will be passed into a block only to invoke  
a method of that object:  
arr.map{ |obj| obj.some\_method }

So I had the (weird? stupid?) thought that it would be nice to have some  
syntactic sugar like this:  
arr.map{ .some\_method }

Does that make any sense?

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 4:53am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/2 "2009-05-14T04:53:01Z")

</div>

On Wed, May 13, 2009 at 10:42 PM, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It seems that often an object will be passed into a block only to invoke  
> a method of that object:  
> arr.map{ |obj| obj.some\_method }
> 
> So I had the (weird? stupid?) thought that it would be nice to have some  
> syntactic sugar like this:  
> arr.map{ .some\_method }
> 
> Does that make any sense?

> > RUBY\_VERSION  
> > =\> “1.9.1”  
> > %w[foo bar baz].map(&:upcase)  
> > =\> [“FOO”, “BAR”, “BAZ”]

On Ruby 1.8.6, you can implement this easily as:

class Symbol  
def to\_proc  
lambda { |x| x.send(self)  
end  
end

-greg

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 5:04am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/3 "2009-05-14T05:04:43Z")

</div>

Jan wrote:

> You can do arr.map(&:some\_method) in ruby 1.9  
> Yes, I know, and that was kind of my point. The to\_proc conversion  
> became popular because people want a shortcut notation for this common  
> case, but IMHO map(&:foo) is way uglier than map{.foo}, and as a bonus  
> you could even do map{.foo.bar} 🙂

-Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 4:55am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/4 "2009-05-14T04:55:21Z")

</div>

- Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-05-14 11:42:31 +0900]:

> It seems that often an object will be passed into a block only to invoke  
> a method of that object:  
> arr.map{ |obj| obj.some\_method }
> 
> So I had the (weird? stupid?) thought that it would be nice to have some  
> syntactic sugar like this:  
> arr.map{ .some\_method }

You can do arr.map(&:some\_method) in ruby 1.9

-J

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 5:07am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/5 "2009-05-14T05:07:38Z")

</div>

On Wed, May 13, 2009 at 11:03 PM, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yes, I know, and that was kind of my point. The to\_proc conversion  
> became popular because people want a shortcut notation for this common  
> case, but IMHO map(&:foo) is way uglier than map{.foo}, and as a bonus you  
> could even do map{.foo.bar} 🙂

I don’t know. I think they’re about the same in terms of looks, and  
the former doesn’t require changes to the parser.

-greg

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 5:34am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/6 "2009-05-14T05:34:33Z")

</div>

On May 13, 2009, at 11:07 PM, Gregory B. wrote:

> the former doesn’t require changes to the parser.
> 
> -greg

plus, doing your .foo.bar has to contend with .foo being nil (or  
otherwise having a NoMethodError for :bar)

some\_collection.map{|e|e.foo.bar}

really isn’t too bad anyway and if you want to handle the possible  
exception:

some\_collection.map{|e|e.foo.bar rescue nil}

possibly with a .compact thrown on the end.

The parser already refused to treat .5 as a Float literal insisting  
that it be 0.5 so I doubt that .foo would get any more favorable  
treatment as a special case (since .5 isn’t really all that special).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 6:16am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/7 "2009-05-14T06:16:38Z")

</div>

Rob B. wrote:

> The parser already refused to treat .5 as a Float literal insisting that  
> it be 0.5 so I doubt that .foo would get any more favorable treatment as  
> a special case (since .5 isn’t really all that special).

Well, you never know… Matz _did_ add the Symbol#to\_proc conversion in  
1.9 and also the foo\n.bar “fluent interface” syntax. So apparently  
trivial changes _do_ make their way into core… sometimes.

-Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 6:28am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/8 "2009-05-14T06:28:20Z")

</div>

Daniel DeLorme wrote:

> It seems that often an object will be passed into a block only to invoke  
> a method of that object:  
> arr.map{ |obj| obj.some\_method }
> 
> So I had the (weird? stupid?) thought that it would be nice to have some  
> syntactic sugar like this:  
> arr.map{ .some\_method }
> 
> Does that make any sense?

It would be nice for avoiding instance\_eval in DSLs:

@x = 400  
@y = 300

config “my window” do  
.width @x  
.height @y  
end

but I just don’t see how to fit it into ruby…

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 7:20am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/9 "2009-05-14T07:20:26Z")

</div>

On May 14, 2009, at 12:27 AM, Joel VanderWerf wrote:

> but I just don’t see how to fit it into ruby…  
> Isn’t this essentially the “with” syntax from Javascript? I think that  
> could fit into a future version of Ruby…

- Josh

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 7:14am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/10 "2009-05-14T07:14:35Z")

</div>

On Thu, May 14, 2009 at 12:16 AM, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Rob B. wrote:
> 
> > The parser already refused to treat .5 as a Float literal insisting that  
> > it be 0.5 so I doubt that .foo would get any more favorable treatment as a  
> > special case (since .5 isn’t really all that special).
> 
> Well, you never know… Matz _did_ add the Symbol#to\_proc conversion in  
> 1.9 and also the foo\n.bar “fluent interface” syntax. So apparently  
> trivial changes _do_ make their way into core… sometimes.

But Symbol#to\_proc is not a parser change. It’s just using an existing  
hook that has been around in Ruby 1.8

-greg

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 9:16am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/11 "2009-05-14T09:16:28Z")

</div>

2009/5/14 Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But the fluent interface change _is_ a parser change. My point was just that  
> seemingly trivial requests _can_ make it into the core, whether they’re a  
> syntax change or not.

We always need to balance cost and benefit. In this case to me the  
benefit seems to be outweighed by costs whereas the other change can  
have an impact on readability. YMMV though.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 8:09am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/12 "2009-05-14T08:09:02Z")

</div>

Gregory B. wrote:

> On Thu, May 14, 2009 at 12:16 AM, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Well, you never know… Matz _did_ add the Symbol#to\_proc conversion in  
> > 1.9 and also the foo\n.bar “fluent interface” syntax. So apparently  
> > trivial changes _do_ make their way into core… sometimes.
> 
> But Symbol#to\_proc is not a parser change. It’s just using an existing  
> hook that has been around in Ruby 1.8

But the fluent interface change _is_ a parser change. My point was just  
that seemingly trivial requests _can_ make it into the core, whether  
they’re a syntax change or not.

-Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 11:33am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/13 "2009-05-14T11:33:48Z")

</div>

On Thu, May 14, 2009 at 4:42 AM, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It seems that often an object will be passed into a block only to invoke  
> a method of that object:  
> arr.map{ |obj| obj.some\_method }
> 
> So I had the (weird? stupid?) thought that it would be nice to have some  
> syntactic sugar like this:  
> arr.map{ .some\_method }

I sometimes miss Perl’s $\_. Without a parser change, it could be used  
as the default name for block parameters:

arr.sort\_by { $_.size }  
File.open(“logfile”, “a”) { $_.puts logtext }  
arr.map { foo2bar($\_) }

Then I remember how people abuse it and avoid using appropriately  
named variables, and I am again happy about the little extra code I  
have to write.

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/14 "2009-05-14T12:29:00Z")

</div>

From: “Lars C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I sometimes miss Perl’s $\_.

Not that I’m advocating its use, but… Ruby does support $\_  
for perl-like command line scripting:

$ ruby -ne ‘puts “dollar\_underscore is: #$\_”’ /usr/share/dict/words |  
head  
dollar\_underscore is:  
dollar\_underscore is: A  
dollar\_underscore is: A’s  
dollar\_underscore is: AOL  
dollar\_underscore is: AOL’s  
dollar\_underscore is: Aachen  
dollar\_underscore is: Aachen’s  
dollar\_underscore is: Aaliyah  
dollar\_underscore is: Aaliyah’s  
dollar\_underscore is: Aaron

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 10:16am UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/15 "2009-05-14T10:16:57Z")

</div>

Robert K. wrote:

> We always need to balance cost and benefit. In this case to me the  
> benefit seems to be outweighed by costs

Right, what folk might not realise is that the 1.8 version constructed  
an object on each usage which had to be GC’d later, whereas in the 1.9  
version it can be free. So there was good reason to support it directly.

Clifford H..

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/16 "2009-05-14T14:05:38Z")

</div>

This is a neat idea, but wouldn’t it conflict with the “fluent  
interface” construct? Using Joel’s DSL example:

config “my window” do  
.width @x  
.height @y  
end

if we changed it to

config “my window” do  
.width calculate\_width  
.height @y  
end

Now how does the parser know what .height refers to: calculate\_width,  
or the implicit block variable?

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/17 "2009-05-14T15:38:44Z")

</div>

Robert K. wrote:

> We always need to balance cost and benefit. In this case to me the  
> benefit seems to be outweighed by costs whereas the other change can  
> have an impact on readability. YMMV though.

The cost isn’t high. 15 minutes of work in JRuby, probably more in CRuby  
but not by a lot.

- Charlie

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 3:45pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/18 "2009-05-14T15:45:13Z")

</div>

2009/5/14 Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Robert K. wrote:
> 
> > We always need to balance cost and benefit. In this case to me the  
> > benefit seems to be outweighed by costs whereas the other change can  
> > have an impact on readability. YMMV though.
> 
> The cost isn’t high. 15 minutes of work in JRuby, probably more in CRuby but not by a lot.

That’s likely only implementation. Then there is testing,  
documentation and before that checking that there are no negative  
effects of the change. If there are - and there seems to be evidence  
that this is the case in Mark’s posting - those negative effects also  
count as costs…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/19 "2009-05-14T15:36:59Z")

</div>

Daniel DeLorme wrote:

> It seems that often an object will be passed into a block only to invoke  
> a method of that object:  
> arr.map{ |obj| obj.some\_method }
> 
> So I had the (weird? stupid?) thought that it would be nice to have some  
> syntactic sugar like this:  
> arr.map{ .some\_method }
> 
> Does that make any sense?

Groovy has this in the form of the “it” magic variable:

[1,2,3].each {puts it}

While I absolutely hate the moniker “it” the idea itself has grown on  
me. Perhaps something more scala-like:

[1,2,3].each {puts \_}

Or a pseudo-global:

[1,2,3].each {puts $it}

I hacked “it” to work in JRuby once recently, and it’s not difficult.

- Charlie

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [May 14, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931/20 "2009-05-14T16:04:07Z")

</div>

Robert K. wrote:

> That’s likely only implementation. Then there is testing,  
> documentation and before that checking that there are no negative  
> effects of the change. If there are - and there seems to be evidence  
> that this is the case in Mark’s posting - those negative effects also  
> count as costs…

Bah, I say.

~/projects/jruby âž” jruby -X-C -e ‘[1,2,3].each {puts $it}’  
1  
2  
3

Diff follows.

diff --git a/src/org/jruby/RubyGlobal.java  
b/src/org/jruby/RubyGlobal.java  
index f25bd42..c107a7b 100644  
— a/src/org/jruby/RubyGlobal.java  
+++ b/src/org/jruby/RubyGlobal.java  
@@ -199,6 +199,7 @@ public class RubyGlobal {

```
      runtime.defineVariable(new ErrorInfoGlobalVariable(runtime,

```

“$!”, runtime.getNil()));  
runtime.defineVariable(new NonEffectiveGlobalVariable(runtime,  
“$=”, runtime.getFalse()));

- 

```
   runtime.defineVariable(new ImplicitItGlobalVariable(runtime,

```

“$it”));

```
      if(runtime.getInstanceConfig().getInputFieldSeparator() == 

```

null) {  
runtime.defineVariable(new GlobalVariable(runtime, “$;”,  
runtime.getNil()));  
@@ -320,6 +321,24 @@ public class RubyGlobal {  
}  
}

- private static class ImplicitItGlobalVariable extends  
GlobalVariable {
- 

```
   public ImplicitItGlobalVariable(Ruby runtime, String name) {

```

- 

```
       super(runtime, name, null);

```

- 

```
   }

```

- 
- 

```
   @Override

```

- 

```
   public IRubyObject set(IRubyObject value) {

```

- 

```
       return

```

runtime.getCurrentContext().getCurrentScope().setImplicitArg(value);

- 

```
   }

```

- 
- 

```
   @Override

```

- 

```
   public IRubyObject get() {

```

- 

```
       IRubyObject obj =

```

runtime.getCurrentContext().getCurrentScope().getImplicitArg();

- 

```
       if (obj == null) obj = runtime.getNil();

```

- 

```
       return obj;

```

- 

```
   }

```

- }
- 

```
private static class LastExitStatusVariable extends GlobalVariable 

```

{  
public LastExitStatusVariable(Ruby runtime, String name) {  
super(runtime, name, runtime.getNil());  
diff --git a/src/org/jruby/runtime/DynamicScope.java  
b/src/org/jruby/runtime/DynamicScope.java  
index e2f6e90..9ad9deb 100644  
— a/src/org/jruby/runtime/DynamicScope.java  
+++ b/src/org/jruby/runtime/DynamicScope.java  
@@ -39,6 +39,8 @@ public abstract class DynamicScope {  
// been called.  
protected DynamicScope evalScope;

- protected IRubyObject implicitArg;
- 

```
protected DynamicScope(StaticScope staticScope, DynamicScope 

```

parent) {  
this.staticScope = staticScope;  
this.parent = parent;  
@@ -165,6 +167,14 @@ public abstract class DynamicScope {  
return staticScope.getAllNamesInScope();  
}

- public IRubyObject getImplicitArg() {
- 

```
   return implicitArg;

```

- }
- 
- public IRubyObject setImplicitArg(IRubyObject implicitArg) {
- 

```
   return this.implicitArg = implicitArg;

```

- }
- 

```
/**
 * Get backref
 */

```

diff --git a/src/org/jruby/runtime/InterpretedBlock.java  
b/src/org/jruby/runtime/InterpretedBlock.java  
index 8015be8..4291066 100644  
— a/src/org/jruby/runtime/InterpretedBlock.java  
+++ b/src/org/jruby/runtime/InterpretedBlock.java  
@@ -162,6 +162,7 @@ public class InterpretedBlock extends BlockBody {  
Frame lastFrame = pre(context, null, binding);

```
      try {

```

- 

```
       context.getCurrentScope().setImplicitArg(value);
        if (hasVarNode) {
            setupBlockArg(context, varNode, value, self);
        }

```

[Next page](https://www.ruby-forum.com/t/syntactic-sugar-idea/167931.md?page=2)
