Default Routes Considered Harmful, and Other Rails SEO Tips covers SEO for Rails apps, with both on-page SEO tips (Prettier URLs, Better Title Tags and DRY in Content) and off-page SEO tips (Easy Linking and Bookmarks).
Tag: Ruby
Adding Type Checking in Ruby
Here’s an article on adding type-checking to Ruby which pays a lot of attention to the Types framework.
A New ActiveMerchant Ebook
[Via Ruby Inside] ActiveMerchant is a new PDF-format ebook covering the ActiveMerchant
Ruby library for handling payments. It supports a number of payment gateways, including PayPal, Authorize.Net, and TrustCommerce. It’s a brief 74 pages long and sells for a dirt-cheap $9. I’m going to order myself a copy later today.
In the last article in this series covering the methods in Ruby’s Enumerable
module, I covered all?
and any?
. In this installment, I’ll look at the collect
method, a.k.a. the map
method.
collect, a.k.a. map
- In plain language: Create an array by performing some operation on every item in the given collection.
- Ruby.Doc.org’s entry:
Enumerable#collect
/Enumerable#map
- Expects: A block containing the operation (it’s optional, but you’re likely to use one most of the time).
- Returns: An array made up of items created by performing some operation on the given collection.
collect
and map
are synonyms — you can use either. I personally prefer map
as it’s shorter and makes more sense: I view the operation as using a function to map a collection to an array.
Using collect
/map
with Arrays
When used on an array and a block is provided, collect
/map
passes each item to the block, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.
[1, 2, 3, 4].map {|number| number ** 2} => [1, 4, 9, 16] ["Aqua", "Bat", "Super", "Wonder Wo"].map {|adjective| adjective + "man"} => ["Aquaman", "Batman", "Superman", "Wonder Woman"]
When the block is omitted, collect
/map
uses this implied block: {|item| item}
, which means when applied on an array without a block, collect
/map
is the identity function — the resulting array is the same as the given array.
[1, 2, 3, 4].map => [1, 2, 3, 4] ["Aqua", "Bat", "Super", "Wonder Wo"].map => ["Aqua", "Bat", "Super", "Wonder Wo"]
Using collect
/map
with Hashes
When used on a hash and a block is provided, collect
/map
passes each key/value pair in the hash to the block, which you can “catch” as either:
- A two-element array, with the key as element 0 and its corresponding value as element 1, or
- Two separate items, with the key as the first item and its corresponding value as the second item.
Each key/value pair is passed to the block, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.
burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320} # What if I had just half a burger? burgers.map {|burger| burger[1] / 2} => [160, 150, 225] burgers.map {|sandwich, calories| calories / 2} => [160, 150, 225] burgers.map {|burger| "Have a tasty #{burger[0]}!"} => ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!", "Have a tasty Whopper with cheese!"] burgers.map {|sandwich, calories| "Have a tasty #{sandwich}!"} => ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!", "Have a tasty Whopper with cheese!"] burgers.map {|sandwich, calories| ["Half a #{sandwich}", calories / 2]} => [["Half a Wendy's Double with cheese", 160], ["Half a Big Mac", 150], ["Half a Whopper with cheese", 225]]
When the block is omitted, collect
/map
uses this implied block: {|item| item}
, which means when applied on an hash without a block, collect
/map
returns an array containing a set of two-item arrays, one for each key/value pair in the hash. For each two-item array, item 0 is the key and item 1 is the corresponding value.
burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320} burgers.map => [["Wendy's Double with cheese", 320], ["Big Mac", 300], ["Whopper with cheese", 450]]
Special Case: Using collect
/map
on Empty Arrays and Hashes
When applied to an empty array or hash, with or without a block, all?
always returns true
.
[].map => [] [].map {|item| item * 2} => [] {}.map => [] {}.map {|sandwich, calories| "Have a tasty #{sandwich}!"} => []
In the Next Installment…
…the detect
(a.k.a. find
) method.
RailsConf 2008 Registration is Open
Just got the email: early bird registration for RailsConf 2008 (which is $100 cheaper) is now open. When I was working a nice big company like Tucows, they’d foot the bill, making the decision a no-brainer. Now that I’m at TSOT, which is a start-up, we don’t have those budgets and now I have to think about the bang-per-buck ratio. Are you going, and what factors are you taking into account?
Ruby is Soooooo 2002
My deadbeat ex-housemate made me aware of Ruby’s existence in 2001 when he bought the first edition of the Pickaxe book. It would take another two years before I would get my first full-on contact with Ruby thanks to Tom and Joe McDonald at vpop, who used it to develop Blogware for Tucows. Four years later, Ruby (and the framework that popularized it, Ruby on Rails) is my bread and butter at TSOT. In that time, Ruby has gone from “obscure programming language with most of its docs in Japanese” to “the new hotness” to “the whipping boy”. Reg “Raganwald” Braithwaite weighs in on Ruby’s popularity cycle in his article Ruby is Soooooo 2002.
I’ll have to write more on this later.
I’ve been quite impressed by the “Addison-Wesley Professional Ruby” series of books (I’ve got The Ruby Way and RailsSpace) as well as the work of series editor Obie Fernandez, whom I had the pleasure of meeting at RailsConf 2006. That — along with glowing reviews for both books plus my serious immersion into Ruby and Rails at TSOT — is why I’ve got Design Patterns in Ruby and The Rails Way on order. I’m looking forward to getting my paws on these books, and I’ll post reviews shortly afterwards.
(I’m normally pretty conservative when it comes to spending on computer programming books for the past little while, but that’s because evangelism rather than programming has paid the rent. That situation has changed somewhat.)
Both Design Patterns in Ruby and The Rails Way are in Antonio Cangiano’s set of recommended Ruby and Rails books. If you’re looking to get into either Ruby or Rails (or if you’re already into either and just looking for related reading material), check out his list.