Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update ampex post, thanks #pry :)

  • Loading branch information...
commit 627f0bd29da83ea883b7370ebf569a60e97c7102 1 parent 10e00ee
@ConradIrwin authored
Showing with 32 additions and 28 deletions.
  1. +32 −28 _posts/2012-05-19-ampex.md
View
60 _posts/2012-05-19-ampex.md
@@ -6,14 +6,14 @@ fuzzydate: May 2012
---
Ruby is renowned for both its readability and its flexibility. The ultimate
-goal of every Ruby program is to work while appearing as simple as possible.
+goal of every Ruby program is to work well while appearing as simple as possible.
-To acheive this as a Ruby programmer, you're encouraged to push a lot of the
-fiddly bits of your program deep into parts of the source where a casual reader
-of the code can ignore them.
+To achieve this as a Ruby programmer, you're encouraged to push a lot of the
+fiddly bits of code deep into parts of your program where a casual reader of
+the source can ignore them.
-Out of the box Ruby ships with some very powerful tools to help you do this, of
-which the most famous are probably its blocks. These allow you to re-use
+Out of the box Ruby ships with some very powerful tools to help you do this, the
+most famous of which is its syntax for custom blocks. This allows you to re-use
existing fiddly code (for example iterating over a list) but to insert a little
bit of customisation (so now you can iterate over a list doubling as you go).
@@ -25,19 +25,20 @@ bit of customisation (so now you can iterate over a list doubling as you go).
The & operator
-----------------
-Blocks are only the beginning of this abiilty though, as Ruby has the `&`
-operator which can be used to "cast" any object to a block.
+Blocks are only the beginning of this ability as Ruby has the `&` operator which
+can be used to "cast" any object to a block. This behaviour works out-of-the-box
+on Procs, Methods, and Symbols.
-By default a handful of Objects already work with `&`, these are Procs, Methods
-and Symbols. You will have seen the Proc support in action a lot, because that's
+You will have seen the Proc support in action a lot, because that's
how you pass `&block` from one function to the next. Method objects are rarely
-used in Ruby, though I find that `.tap(&method(:puts))` is a useful way to debug
-an object in the middle of an expression.
+used in Ruby, though sometimes inserting `.tap(&method(:puts))` is a useful way
+to debug an object in the middle of an expression.
The most interesting example of `&` however is on the Symbol class. This was
-ported to the core Ruby language in version 1.8.7 because it was so popular with
-users of the libraries that implemented it (the [Ruby Extensions Project](http://extensions.rubyforge.org/rdoc/index.html)
-and [ActiveSupport](http://apidock.com/rails/v1.1.1/Symbol/to_proc)).
+ported to the core Ruby language in version 1.8.7 because it was so popular
+with users of the libraries that implemented it (the [Ruby Extensions
+Project](http://extensions.rubyforge.org/rdoc/index.html) and
+[ActiveSupport](http://apidock.com/rails/v1.1.1/Symbol/to_proc)).
The whole purpose of using `&` with a symbol is to generate more readable code,
compare:
@@ -84,7 +85,6 @@ A few examples of how cool this can be:
=> ["a", "b", "c"]
{% endhighlight %}
</figure>
-
{% highlight ruby %}
# when you want to parse some JSON
owners = [{'name' => 'Fred', 'dog' => 'Fido'},
@@ -103,8 +103,8 @@ If you're interested in more that ampex can help you with, the
[README](https://github.com/rapportive-oss/ampex) contains a few more examples.
<aside>While I think the benefits of this are obvious, when ampex was mentioned
-in [#ruby-lang](irc://irc.freenode.net/#ruby-lang), it elicited the
-[response](http://irclog.whitequark.org/ruby-lang/2011-12-23#1324608192;):
+in [#ruby-lang](irc://irc.freenode.net/#ruby-lang), it elicited the response
+from [ZenSpider](http://irclog.whitequark.org/ruby-lang/2011-12-23#1324608192;):
"honestly... if someone used that in production on one of my systems, I'd fire
them on the spot" (Your-Mileage/Manager-May-Vary)</aside>
@@ -126,28 +126,32 @@ source :rubygems
gem 'ampex'
{% endhighlight %}
+We've been using ampex in production for over a year now, and beacuse it's
+written in pure Ruby, it works on Ruby 1.8.7, Ruby 1.9 and JRuby out of the
+box.
Further Thinking
----------------
While ampex helps a lot, there are still cases where you need to introduce a
-temporary variable. As mentioned above, for some of these cases you can use
-`&method(:foo)`; but I find that that code makes me think even harder than the
-equivalent block version.
+temporary variable unnecessarily. As mentioned above, for some of these cases
+you can use `&method(:foo)`; but I find that that code makes me think even
+harder than the equivalent block version.
I would really love to see something like Scala's
[underscore](http://books.google.com/books?id=MFjNhTjeQKkC&pg=PA146) in Ruby
itself. There have been a few attempts at this, for example
-[RubyUnderscore](https://github.com/danielribeiro/RubyUnderscore), but I think
-that rewriting the syntax of your program is likely to be a too-large barrier to
-entry for many people.
+[RubyUnderscore](https://github.com/danielribeiro/RubyUnderscore) adds it to
+Ruby 1.8.7 by syntax rewriting and [this
+patch](https://gist.github.com/1224361) adds it to Rubinius, but none are yet
+ ready to actually be used.
-Particularly, I think it should be possible with some ingenuity to come up with
-a solution that covers another 90% of the things you want to do with pure Ruby
-and some cunning. For example, something like the following could be made to work:
+Particularly I think it should be possible with some ingenuity to come up with
+a solution that covers another 90% of the things you want to do. For example,
+with pure Ruby and a little cunning, the following could be made to work:
{% highlight ruby %}
[1, 2, 3].map(&».puts(X))
{% endhighlight %}
-The question I suppose, is whether or not it can be done simply enough…
+The question, I suppose, is whether or not it can be done simply enough…
Please sign in to comment.
Something went wrong with that request. Please try again.