Writing Custom RSpec Matchers for a Rails Project

This week I was writing some unit tests using the RSpec testing framework for a Rails project. What I wanted to test was a little more complicated than what the standard pretty RSpec syntax provided for, so I immediately thought that it was time for a custom RSpec matcher. For lack of a better word, RSpec matchers are very “cool”. Not only are they great tools for code reusability (think...

Doing a Ruby Loop the Ruby Way

The best thing about Ruby loops is that you hardly need to use them. Well, at least not in the familiar way if you are coming form a procedural language such as C++, PHP, Java, etc. In this article you’ll see why conventional loop constructs such as while loops and for loops need only be used on rare occasions in Ruby, and how those give way to concise, easy to use, and very powerful syntax. The...

The Ruby Ampersand (&) Operator In a Function Call Explained

Ever since I started learning Ruby, I thought that the Ruby ampersand operator for a function argument syntax is a very interesting, yet somewhat difficult to grasp feature of the language. So I took some notes on it, which I’ve turned into this post. This is how a function call with an ampersand looks like: some_function(&an_object) When used as shown above, the & operator induces a...

Ruby Hash Merge Function for Multiple Hashes, Inject and Asterisk Notation

I needed to merge a number of hashes together the other day, but unfortunately, the standard Ruby hash merge method merges just two hashes. In this post I describe how to write a handy multi-hash merge and other interesting Ruby syntax.

Using Ruby on Rails Routes to Add an API Version Number to Your URLs

In the previous post, which was about printing ActiveRecord Errors, I mentioned that I’m wring a web service API to support an iPhone app. One of the suggestion of my iPhone developer was that we include an API version number in the URL paths, for example, http://example.com/2_0_3/do-something.json So here the API version number is 2.0.3. This way we could keep track of the API version used without...

Writing a Convenient ActiveRecord Error Formatting Method

This post is about writing a method that aggregates ActiveRecord errors into a single string. So instead of getting something like the following from @user.errors: #["is too short (minimum is 6 characters)"], :email=>["has already been taken"]}> I'd like a quick way to get a single string that's ready to be used in print: "Password is too short (minimum is 6 characters). Mail has already been taken."

How to Update Your Local Hosts File

A hosts file is a local domain name lookup service that matches IP (Internet Protocol) addresses with domain names. As I explained in my post about...
Page 1 of 212
Copyright © 2016 by Yaron Walfish. All rights reserved Powered by WordPress | Designed by Elegant Themes