<div>
<div>
<span>1. The (a ? b : c) ternary operator does the expected thing if you chain it and leave out parentheses, making it shorter than if/elsif/else, even if it looks terrifying:</span></div><div><span><br></span></div><div><span>s > 3600 ? "more than an hour" : s > 60 ? "more than a minute" : s > 1 ? "more than a second" : "GET OUT OF THE WAY"</span></div><div><span><br></span></div><div><span>2. a[-1] is 1 character shorter than a.last.</span></div><div><span><br></span></div><div><span class="Apple-style-span" style="color: rgb(160, 160, 160); ">On Thursday, May 12, 2011 at 10:30 AM, Murray Steele wrote:</span></div><div>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div>I think it might be useful to do a bit of a review of things people learned while doing rubygolf*. We didn't have time on the night and it sounds like that might have been at good part of the SHRUG meeting.<div><br></div>
<div>I'll start with some snippets that I learned / remembered:</div><div><br></div><div>1. Enumerable#inject doesn't need a block, it actually takes 2 params, a initial value and a symbol as an argument. Each iteration the symbol is the method that'll be called on the accumulator and passed the current element. Also if you don't supply an initial element then it'll use the first element of the enumerable. So you can turn</div>
<div><br></div><div>array.inject(0) {|s, i| s + i}</div><div><br></div><div>into</div><div><br></div><div>array.inject(:+)</div><div><br></div><div>2. Procs and Lambdas can be invoked via .[] rather than .call.</div><div>
<br></div><div>3. There are millions of ways of defining methods on something. Luckily, the tests don't say that Golf needs to be a class, so we can define Golf as a constant and just add methods to it. So we can turn:</div>
<div><br></div><div>class Golf</div><div> class << self</div><div> def hole1</div><div> ...</div><div> end</div><div> end</div><div>end</div><div><br></div><div>into:</div><div><br></div><div>class << Golf = ''</div>
<div> def hole1</div><div> ...</div><div> end</div><div>end</div><div><br></div><div>(e.g open up the metaclass of Golf, which happens to be a constant of an empty string, rather than a class. Works because assignment (Golf = '') has higher precendence than class-opening-up (class << Golf).</div>
<div><br></div><div><div>I tried to shave 1 character by setting Golf to be numbic constant:</div><div><br></div><div>class << Golf = 1</div></div><div> def hole1</div><div> ...</div><div> end</div><div>end</div>
<div><br></div><div>But it doesn't work because Fixnums have no metaclass. Depending on what you do to try and add the methods you get various errors like:</div><div><br></div><div>TypeError: no virtual class for Fixnum</div>
<div>or</div><div>TypeError: can't define singleton method "hole1" for Fixnum"</div><div><br></div><div>I found this surprising, (and I think I've come across it before, and was surprised then too) as I always believed the "everything is an object" mantra of Ruby, assuming everything was the same under the hood. Turns out that while it's still true that everything is an object, some objects are clearly more objecty than others.</div>
<div><br></div><div>I doubt I'd ever want to use any of these tricks in production code (as someone said, it's can be far from readable), but I don't think it's been just a toy exercise. In trying to shave characters off my solution I've been pushing the boundaries of what Ruby can and can't do. This in turn helps solidify my understanding of the object-model and syntax parser.</div>
<div><br></div><div>So, anyone else want to share what they learned?</div><div><div><br></div><div>Muz</div><div><br></div><div>*: I'm still working on mine and while I don't want to look directly at other solutions, I'm happy to be given inspiration by discussing techniques**.</div>
<div><br></div><div>**: Yes I know this makes no sense.<br><br><div>On 10 May 2011 15:03, Andrew McDonough <span dir="ltr"><<a href="mailto:andrew@andrewmcdonough.co.uk">andrew@andrewmcdonough.co.uk</a>></span> wrote:<br><blockquote type="cite"><div>Thanks James. I'm glad that ShRUG enjoyed it. We were a bit short on<br>
time at LRUG, with less than an hour available for coding. Four teams<br>
completed the course, and our winner was Tomasz Wegrzanowsk (@t_a_w),<br>
who was working on his own, and managed 704 characters<br>
(<a href="http://j.mp/lbcXV2" target="_blank">http://j.mp/lbcXV2</a>). Second place was @zuppr, @jamiemill @morticed<br>
and @aanand who came a close second in with 843 chars<br>
<a href="http://j.mp/mBjfJU" target="_blank">http://j.mp/mBjfJU</a>, third place was @danlucraft, @nfelger, Jordi,<br>
Dan, Tony, Steve and Keith who completed the course in 883 chars<br>
<a href="http://j.mp/jHhbWU" target="_blank">http://j.mp/jHhbWU</a> and finally one other anonymous team completed the<br>
course with 1208 characters (if this was you, please let us know who<br>
you are). The winner, Tomasz (<a href="http://j.mp/lSsmJd" target="_blank">http://j.mp/lSsmJd</a>), was presented with<br>
a trophy (<a href="http://j.mp/iRFwGP" target="_blank">http://j.mp/iRFwGP</a>) and the second placed team got medals.<br>
There are still four medals left, which I will award at the next LRUG<br>
to the four people who produce the best solutions (not necessarily the<br>
shortest ones), so there's still something to play for if you didn't<br>
come along on the evening.<br>
<br>
There appears to have been a lot of work done since to get the<br>
character counts down even further, in particular from Paul Battley<br>
with some help from Tom Stuart. Their commit log is worth looking<br>
through:<br>
<br>
<a href="https://github.com/threedaymonk/rubygolf/commits/master" target="_blank">https://github.com/threedaymonk/rubygolf/commits/master</a><br>
<br>
They got the solution down to 654 characters, before realising they<br>
could exploit my "whitespace isn't counted" rule to encode their<br>
solution in spaces and newlines, and then simply decode and exec.<br>
This brought the solution down to 37 characters. He gets one of the<br>
four medals. Genius:<br>
<br>
<a href="https://github.com/threedaymonk/rubygolf/blob/master/lib/golf.rb" target="_blank">https://github.com/threedaymonk/rubygolf/blob/master/lib/golf.rb</a><br>
<br>
I have put the introductory presentation I made up on heroku:<br>
<br>
<a href="http://rubygolf-presentation.heroku.com" target="_blank">http://rubygolf-presentation.heroku.com</a><br>
<br>
Skills Matter have published some photos of the evening:<br>
<br>
<a href="http://www.flickr.com/photos/skillsmatter/5704350908/in/set-72157626683669388/" target="_blank">http://www.flickr.com/photos/skillsmatter/5704350908/in/set-72157626683669388/</a><br>
<br>
I will follow up later with some analysis of the solutions and some of<br>
the tricks and techniques used, but for now, you can look through the<br>
various solutions that were submitted:<br>
<br>
<a href="https://github.com/andrewmcdonough/rubygolf/network" target="_blank">https://github.com/andrewmcdonough/rubygolf/network</a><br>
<br>
Congratulations to ShRUG - you guys did really well completing the<br>
course with so few characters.<br>
<br>
Andrew<br>
<br>
---<br>
Andrew McDonough<br>
CTO, Tribesports<br>
<a href="http://tribesports.com" target="_blank">http://tribesports.com</a><br>
<div><div><br>
<br>
On 10 May 2011 13:59, James Almond <<a href="mailto:james@jamesalmond.com">james@jamesalmond.com</a>> wrote:<br>
> Hi all,<br>
> As promised, an update on ShRUG's progress with the Ruby Golf night we ran<br>
> in parallel with LRUG.<br>
> Firstly, I managed to fork the repository before the 7th hole was added and<br>
> nobody noticed until a fair amount of time into the evening so we completed<br>
> the course without it. We also worked with a slightly lighter and RSpec<br>
> 2 compatible runner, but all specs and counting mechanisms stayed the same.<br>
> Everyone had 1.9.2 installed and favoured it, so we went with that over<br>
> 1.8.7. I think we were still pretty close to what you were running. We had<br>
> around 5 pairs of 2.<br>
> Our lowest team effort on the night was 592 (with hole 7<br>
> missing): <a href="https://github.com/ianwhite/rubygolf/blob/master/lib/golf.rb" target="_blank">https://github.com/ianwhite/rubygolf/blob/master/lib/golf.rb</a><br>
> A group discussion and edit took the count down to<br>
> 522: <a href="https://github.com/shruggers/rubygolf/blob/ecd0b312bfb9707b6debfc01062eab5e39d5f95a/lib/golf.rb" target="_blank">https://github.com/shruggers/rubygolf/blob/ecd0b312bfb9707b6debfc01062eab5e39d5f95a/lib/golf.rb</a><br>
> A bit of post-ShRUG editing and adding hole 7 our final team count was<br>
> 604: <a href="https://github.com/shruggers/rubygolf/blob/master/lib/golf.rb" target="_blank">https://github.com/shruggers/rubygolf/blob/master/lib/golf.rb</a><br>
> The discussion and collating of the results at the end was good fun and also<br>
> made it one of the latest running ShRUG meetings I can remember.<br>
> Everyone had a great evening, and learnt something new. Thanks to Andrew<br>
> (and anybody else who contributed) for the holes, much appreciated! The<br>
> #rubygolf radio silence from Sheffield was because everyone was too busy<br>
> attempting the holes, sorry about that.<br>
> James<br>
</div></div>> _______________________________________________<br>
> Chat mailing list<br>
> <a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
> <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
><br>
><br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</div></blockquote></div><br></div></div>
</div><div>_______________________________________________<br>Chat mailing list<br><a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br><a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br></div></div></span>
</blockquote>
<div>
<br>
</div>
</div>
</div>