[翻译] Ruby Golf
原文地址:http://rubysource.com/ruby-golf/
?
Ruby golf is the art of writing code that uses as few characters as possible. The idea originates in the world of Perl (where it is, unsurprisingly, known as Perl Golf). As a language, Perl is well suited to this as it has more than its fair share of bizarre constructs and syntactic sugar. Ruby also contains some large dollops of syntactic sugar which makes it suited to the odd round of golf.
Ruby golf 是一种使用尽量少的字符来编写代码的技术。这个想法最初产生于Perl社区(不出意料的成为Perl Golf)。由于Perl拥有一系列奇异的语法结构和特征,所以很适合应用这种技术。Ruby同样包含大量的奇异语法特征,所以也非常适合这种成为Golf的技术。
The obsession with trying to minimise the number of bytes used in a program harks back to a bygone age when memory was at a premium and every byte counted. It doesn’t really have any practical use in today’s world, but this does not mean that it is not still used. Code minimisation can be found in competitions such as the?1k JavaScript competitions?and?Perl Apocalypse. There are also a number of sites dedicated to the art of Code Golf.
试着减少程序中使用的字节数的想法可以追溯到那个内存昂贵到以每个字节来计费的时代。当下,这种想法并没有多少实际意义,但是很多时候它仍被使用。代码最小化可以在一些比如称作1k JavaScript competitions和Perl Apocalypse的比赛中找到。也有很多网站试着对Code Golf这种技术有所贡献。
There is often a competitve element associated with Code Golf: Hackers try to outdo each other as they strive to find the ‘perfect’ solution. One that achieves the aim of the code but could not possibly be done in fewer characters. I find this to be analagous to mathemticians who search for ‘elegant proofs’ and am reminded of Einstein’s quote “Everything should be made as simple as possible, but not simpler”.
跟Code Golf相关的一个竞争元素是:黑客们总是试图通过找到'完美'解决方案而超越他人。一个人可以完成代码的功能却可能无法用更少的字母实现。我发现这类似一个寻找“最优解”的数学家被爱因斯坦的名言提醒:"任何事情都应该尽可能简单,而不仅仅是简单。"
Unfortunately, the code that is produced is often very difficult to understand as it usually relies on various hacks and shortcuts. In fact, a complimentary game is to try and figure out what the code produced actually does! There is often a thin line between being clever and too clever…
不幸的是,由于过分依赖各种编程技巧,编写出来的代码经常难以理解。实际上,一个免费的游戏是试着找出这段代码真正做的事情。总有一条细线横亘在变的聪明和变得过于聪明之间...
Code Golf has a bit of a?marmite?reputation – people either love it or hate it, so to try and maintain some balance, I’ve listed a few of the advantages and disadvantes of trying your hand at a round or two:
Code Golf有截然不同的两种名称:喜欢它或者厌恶它。所以为了试着去保持某种平衡,我列举了一些Code Gold的优点和缺点:
If you fancy having a go at some golf, then here are a few tips that can be used to cut your Ruby code down to size and reduce your golfing handicap:
专业提示:
如果你喜欢尝试一下Golf,那么这里有一些小贴士可以用来帮助你减少你的Ruby代码并且减少你在这个过程中遇到的困难。
Mass Assignment
This is an easy one that most people will know, but assigning all your variables at once will help to cut down on code bloat.
=> "a-b-c"
?
Reuse Loops
A good way to avoid using 2 loops for different object types is to bung all the objects into a single array and use just one iterator, but then perform different tasks depending on the object type.
fizzbuzz(31) => "31"
?
Implement a Caesar Shift Cipher
swingers([["Homer","Marge"],["Micky,"Minnie"],["Fred","Wilma"],["Peter","Lois"],["George","Judy"]])
=>
[["Homer","Wilma"],["Micky","Lois"],["Fred","Judy"],["Peter","Marge"],["George","Minnie"]]
?
?
?
(待续。)
?
?