Tag Archives: Ruby

Matz, why?

In “The Well Grounded Rubyist”, David Black talks about the “Class/Object Chicken-and-Egg Paradox”:

The class Class is an instance of itself; that is, it’s a Class object. And there’s more. Remember the class Object? Well, Object is a class… but classes are objects. So, Object is an object. And Class is a class. And Object is a class, and Class is an object.

Tagged , , , , ,

Blocks as Closures

Closures can be something difficult to grasp but they have some interesting use-cases from simple to harder ones. For instance:

def power_to
  foo = 1
  Proc.new { foo += foo }

proc_obj = power_to
puts proc_obj.call
puts proc_obj.call
puts proc_obj.call

This will print:

$ 2
$ 4
$ 8

The method power_to returns a Proc object which has the state of foo local variable. Though, the variable remains accessible to the block even if that variable is out of scope when the block is called again.

That’s closures and it’s the basis of lexical scoping which you can find in other programming languages such as JavaScript.

Tagged , , ,

Faraday: your new best friend

I am a die-hard REST API fan, however the default Net::HTTP Ruby lib is very verbose and doesn’t give me all the features and flexibility I need for coding API clients fast and easy.
Faraday is a hell of a tool.. (disclaimer: the name is not related with any English scientist well known for contributions in the electromagnetism field). Faraday it’s a HTTP library, started by Rick Olson, which reuses Rack middleware concepts and provides a common interface for building API wrappers. Also it supports many out of the box advanced features, like OAuth, SSL, HTTP caching (Last-Modified and ETag headers), HTTP proxy, easy response data parsing, which could be cumbersome to code and configure with the popular Net::HTTP lib.

Here is a basic snippet:

require 'faraday'

Faraday.get(:url => 'http://faradayisfun.com')

But you can code more complex things and tweak configurations:

require 'faraday'

conn = Faraday.new(:url => 'http://faradayisfun.com') do |builder|
  builder.response :logger #logging stuff
  builder.use Faraday::Adapter::NetHttp #default adapter for Net::HTTP
  builder.use FaradayMiddleware::ParseJson #cool for parsing response bodies

res = conn.post do |request|
  request.url '/example'
  request.headers['Content-Type'] = 'application/json'
  request.body = "{send: body}"


Do you need HTTP basic auth? Faraday gives you some:

conn.basic_auth('user', 'pwd')

With Faraday you can consume APIs with some lines of code and customize its behavior like a pro. Checkout the github repo and take a look at all the cool extra options and supported features.

Tagged , , , , ,

For all the Ruby coders out there

“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck”

You will know what I mean…

Tagged ,


What they promise?

The best way to manage library dependencies in Objective-C projects.

How do you use it (blabla)?

Specify the libraries for your project in an easy to edit text file. Then use CocoaPods to resolve all dependencies, fetch the source, and set up your Xcode workspace.

How do you use it (now for real…)?


CocoaPods is distributed as a ruby gem, installing it is as easy as running the following commands in the terminal:

$ [sudo] gem install cocoapods
$ pod setup

Get started

Search for pods by name and description.

Then list the dependencies in a text file named Podfile in your Xcode project directory:

$ edit Podfile
platform :ios
pod 'JSONKit',       '~> 1.4'
pod 'Reachability',  '~> 3.0.0'

Now you can install the dependencies in your project:

$ pod install

Make sure to always open the Xcode workspace instead of the project file when building your project:

$ open App.xcworkspace

Note: Everything was taken from here.  You can also check their repo on GitHub here.


If you want to use my own library to create NSURLConnections using blocks, you can use it by adding to your  PodFile this:

pod 'NSURLConnection-Blocks', '~> 1.0'
Tagged , , , , ,