Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Ruby and Ruby on Rails Scene in Barcelona


I was asked on Reddit for some info about the Rails scene in Barcelona, so I went ahead and compiled some links here so that everyone can take advantage of it :)

Meetup and events

A Social Network in Rails: Activity Feed

This post is part of an ongoing feature about creating a social network in Rails. You can see part 1 here, about creating elegant permalinks for your content.

Today I'm presenting a wonderful gem called public_activity , which lets you create activity feeds with a few lines of code:
To begin with, add the gem to your Gemfile, as usual:
gem "public_activity"

A Social Network in Rails: Elegant permalinks


In this post we're adding permalinks to a Rails app using the friendly_id gem.

Let's say you have a `User` and a `Photo` model in your app, and a user has_many :photos.
To add a permalink to a photo post that looks like this:
www.nice-pics.com/berners-lee/photos/my-first-site



Add friendly_id to your Gemfile:
gem "friendly_id"
Add the permalink schema to your routes.rb:
get ":user_id/photos/:photo_id", to: "photos#show", as: :photo_permalink
Now, add the friendly_id scheme to your User:

Globalize and Rails 4.2

Recently I've been working on preparing our Rails app at Ztory to Rails.4.2. As of today 9/12, here are the changes to our current gemset we needed to support this Rails version:
gem "rails", "~> 4.2.0.rc2"
gem "devise", git: "https://github.com/plataformatec/devise.git", branch: "lm-rails-4-2"
gem "globalize", github: "ncri/globalize" # for Rails 4.2
I've facing an issue with the globalize gem, so I'm documenting my progress here. Using the latest stable release of the gem gives me the following error:

A Social Network in Rails: a gem-by-gem guide


  1. 5-minute Setup with RVM and Railsbricks
  2. Elegant permalinks
  3. Activity Feed
  4. Comments
  5. Notifications (coming soon)
  6. Follow/mention people (coming soon)
  7. ...
I'm compiling and expanding these blog posts into a book via Leanpub, A Social Network in Rails, subscribe to be updated of its progress here.

Disable dangerous rake tasks in production


Yesterday, we had to restore the production DB from a backup, since we ran "rake db:schema:load" in production by mistake. To avoid that problem in the future, I decided to disable that task, and others than can screw with the DB, in production. 

It was a matter of adding a prerequisite to those dangerous tasks, that checks if they are being run in production, and exit accordingly. I also added a flag to override this safeguard, together with some code to backup the DB. To do this, I added the following code to lib/tasks/disable_db_tasks_on_production.rake:

Dev diary, day 3: Python for Rubyists

Today was a fun day. Due to the problem I had yesterday with importing relative paths, I spent most of this morning reading about modules in Python. I also found out that there is a nice debugger for python code, called pdb, which reminds me a lot of irb when used with the -rdebug option.

Ever since I started doing Python full-time (~8 days ago!) I've had this nagging feeling that it is not as "pretty" or usable as Ruby. Talking with my colleagues about this, and other differences between the two languages, led me to this presentation, which really lifted my spirits:



There might be lots of superficial differences between the way Ruby and Python developers do their thing, but the philosophy behind both languages, and the capabilities provided by them, seem to be pretty similar. Out all of the transitions between languages that I've had (Java to Javascript, JS to Ruby, etc) this one is being the smoothest one, and that's making me happy :-)

PD: I need to start using better fonts for these posts —Arial and Courier do not match that well. Sorry about that!

131% faster Ruby tests, no new code needed

Some speed improvements on a Rails app test suite, about 100 assertions.

Initial state – Ruby 1.9.3-p448:

Total run time: 37s


First improvement – Ruby 1.9.3-p448 with Railsexpress patches:

Tuning your Ruby GC for fun and profit

By simply adding the following to my .zshrc:

export RUBY_HEAP_MIN_SLOTS=2000000
export RUBY_HEAP_SLOTS_INCREMENT=500000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=70000000
export RUBY_FREE_MIN=100000

I'm getting a 10% increase running the test suite on a Rails app:

# Before:
rake  24.57s user 3.77s system 95% cpu 29.690 total
# After:
rake  21.41s user 4.22s system 95% cpu 26.897 total

Try it out and see it for yourself :)