Mar 12, 2013

Fix openssl issues with ruby 2.0.0

Just in case you want to install ruby 2.0.0 and have problems with openssl failing to install. Try the following bits of code. They finally fixed my issues:

$ rvm remove 2.0.0 # get rid of unsuccessful installation
$ rvm get head --autolibs=3 # get the latest RVM and build required libs
$ rvm requirements # just in case, install all other required stuff
$ rvm install ruby-2.0.0
$ rvm --default use ruby-2.0.0

(Source: apple.stackexchange.com)

Sep 18, 2012

Simple error classes in ruby

I just stumbled on this little gem to create your own flat error classes. Instead of defining your class in the most common ruby way of 

class MyCustomError < StandardError; end

You can also write

MyCustomError = Class.new(StandardError)

No ; required. So much nicer

(Source: blog.steveklabnik.com)

Sep 11, 2012
Feb 11, 2012

p and pp for json

Great little trick by Peter Cooper (@peterc).

You can use j and jj just as p and pp to print an object as json:

  require 'json'
  data = { name: "Max Mustermann", tags: ['some', 'tags']}
  j data # for simple output
  jj data # for really beautiful output

If you wanna learn a few more tricks, visit his ruby trick shots video.

Oct 26, 2011

Did you really think about your users’ name?

I just stumbled upon a really interesting article which discusses the various name schemas used around the world. And although I thought I knew quite a bit about that topic, I never thought about the implications it has on web- and data design.

So if you are developing apps for people fromm various cultural backgrounds, do them and yourself the favor and read Personal names around the world @wrc and think about your forms and fields.

Now that I know all that stuff, I finally understand why I had to clean/modify quite a lot of data by hand in one of my recent projects.

Oct 21, 2011

Did you know about ruby’s class_exec?

I just stumbled upon a little gem called “class_exec” while reading ruby’s Module docs. I don’t know about other developers preferences, but I really really hate eval(). It is fine in JavaScript for creating JSON objects, but apart from that I really do not like it. No matter if is a simple eval, instance_eval or class_eval. Writing code in strings and evaluating it afterwards just seems wrong. It is something which always bothered me.

As it seems kept on using it although there is a much more beautiful solution to the problem. Here is the description of the method:

Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver.

So great! Welcome class_exec, good bye class_eval!

I don’t know why I did not find this earlier, but I guess because the web is full of blog posts and code samples which utilize class_eval. And nobody really updated the stuff after ruby 1.8.7 was released and the class_exec method introduced. I guess it is time to change that!

Oct 20, 2011

Declarative cucumber features

Having an active community with lots of active developers and tons of blogs sharing code and setup might be considered great. And usually it is. But in case of cucumber I guess it was not always the best and therefore the removal of web_steps.rb in cucumber 1.1.0 is a huge and important step.

I have to admit, i never really liked cucumber! I t felt wrong and was annoying to maintain. So I sticked with rspec and integration testing was not really handled. But then I read the post The training wheels come off by Aslak Hellesøy and I finally understood why I did not like cucumber. I was writing code just not in ruby but some other language the web_steps file provided. 

Checking other blogs about the release and common errors/mistakes people make when using cucumber I just started to rewrite my existing cucumber features and adding new ones. And It is fun: Clear and easy scenarios, clear and maintainable step definitions. I like!

And here a few more links which might come in handy:

Cheatsheets for cucumber and capybara:

Aug 23, 2011

Directly access an object if it's present

rubyquicktips:

If you want to access an object only if it’s present, you can use Rails’ Object#presence.
The API docs on presence have a good explanation:

This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP…

Jun 6, 2011

Migrating and populating STI models

I just spent quite some time on a “bug” in one of my migrations. Run after run, it failed to populate the database properly. Even after adding the obligatory

  MyModel.reset_column_information

But after checking the database carefully, I realized that some rows had the intended data, while others didn’t. Long story short: Don’t forget to reset the column information on all models which inherit from the base model. In my case it was something like:

  Account.reset_column_information
  Guest.reset_column_information
  User.reset_column_information
Mar 11, 2011

Ruby retry exceptions

I just finished coding my loop when it struck me and I searched for a solution which feels more ruby like.

What I was trying to achieve is executing a piece of code until it ran without an exception. This might sound stupid, but in some cases it makes perfect sense. In this specific case, I was fetching secure data from an external provider via SSL connection. Unfortunately, the SSL connection did not always open as expected. So I have to try till it works (or I give up).

Instead of using a loop, ruby provides us with keyword retry - What a wonderful little keyword:

begin
  # Put your code here which has to be executed
rescue YourExpectedException => ex
  retry
end

And if you wanna protect yourself from an endless loop, add something like retry unless i > 10.

Navigate
« To the past Page 1 of 8
About