At a first glance the 1.9 ruby hash syntax looks like a simple change. No longer using the infamous “Hashrocket” and moving to a familiar JavaScript Object Notation.
{:cat => 'meow'} # 1.8 {cat: 'meow'} # 1.9 On an average day I write equally as much JavaScript as I do Ruby so the new 1.9 syntax is an easy swtich for me.
Do these updates to the syntax mean that we’ll be seeing the demise of the 1.
In the apps I’ve been working on recently I decided to utilize rails built in authentication ‘has_secure_password’ which began shipping with Rails 3.1. Has_secure_password is simple to use and doesn’t bring in any extra bulk. With some of these projects I also decided to use MongoDB & Mongoid. When trying to use the has_secure_password method I got:
undefined local variable or method `has_secure_password' for User:Class (NameError) Solution: You must include include ActiveModel::SecurePassword.
While working with the ActiveRecord method ‘new_record?’ and Transactions I ran into an odd issue after I had run a few objects through a Transaction. Although the transaction had rolled back and thus not saving the record, running ‘object_instance.new_record?’ resulted in a return of false as if the record had been saved to the database.
I also found the same issue recorded here: http://squarewheel.wordpress.com/2008/06/11/new_record/
Here is example code where the error will happen.
While working with my new employer (in 2011) I noticed a minor but recurring issue in our application. We had been storing flash[:notice] messages in most of our actions but were not using that flash message anywhere. This was resulting in the flash message showing up at in-appropriate times. What I was unaware of was a slightly different call which is flash.now[:notice]. This results in the flash only having a lifespan of the through the next request.
TLDR; Don’t store Tweet ID’s as INT. Use BIGINT.
While writing a Twitter application in Rails I was having an issue saving tweets to the database.
A quick brief on the application: The app would download tweets from the mention timeline of a particular user. It would only download tweets it hadn’t already processed based on the last saved tweets unique id (as set by twitter). All the data from the tweets were making it into the database as expected but every time the application downloaded tweets it would re-download the same tweets.
While setting up a new Rails development environment I ran into the error bson keys must be strings or symbols. After some googling I found the following method to fix it:
/gems/bson-1.2.0/lib/bson/bson_c.rb:24:in `serialize': keys must be strings or symbols (TypeError) rvm gemset empty bundle install /fixed
This is just a quick bit that I learned early in my Ruby career and always found interesting.
Symbols Are Memory Leaks
So is this the end of the world?
No
Symbols are a unique thing in ruby. It’s immutable, constant and unique. You can re-use the same Symbol over and over without allocating extra memory. As opposed to using a String over and over which will allocate memory with each instance.