Freelance Mobile & Web Developer

A Ruby & Javascript hacker.

Using Pry With Rails

Pry is a IRB replacement with some very nice additional features. I’ll link to additional resources at the end of this post.

Some of the additional features pry offers us include:

  • Native syntax highlighting
  • Source and document browsing
  • Very flexible plugin architecture

Installation

You can install and run pry with:

1
2
$ gem install pry
$ pry

They are a few ways to integrate pry with Rails but the one I like it the most came from @fnando and originally from Luca Pette. Instead of patching of Rails initializer file, as described on the Pry Wiki, you can use the following method:

On IRB initialization file load Pry instead:

~/.irbrc
1
2
3
4
5
6
7
begin
  require 'pry'
  Pry.start
  exit
rescue LoadError => e
  warn "=> Unable to load pry"
end

This will load try to load Pry and if failed will print a message to stdout. The exit command after Pry’s invocation is to avoid going back to IRB after the Pry session has finished.

The other part of the configuration is done on .pryrc

~/.pryrc
1
2
3
4
5
6
7
8
9
if defined?(Rails)
  begin
    require "rails/console/app"
    require "rails/console/helpers"
  rescue LoadError => e
    require "console_app"
    require "console_with_helpers"
  end
end

This will allow you to continue using rails console but using pry instead of irb. I also like to use awesome_print so I added this to my config file:

~/.pryrc
1
2
3
4
5
6
begin
  require "awesome_print"
  Pry.config.print = proc {|output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output)}
rescue LoadError => err
   warn "=> Unable to load awesome_print"
end

This will try to load awesome_print gem and failing to do that will print a message to stdout.

For more information on Pry see:

Routing a Wordpress Blog Inside a Rails App

This post describe how to host a subdomain route for a alien application inside a Ruby on Rails app. Wordpress is used here as an example but these instructions work with any outside url.

If you do have control over the web server you can use a url rewrite to accomplish this. See steps for doing this using Apache or nginx.

However what about when you have no control over the web server layer (such as when hosting the Rails application on Heroku)? The answer is to use the Rails routing engine do the routing.

config/routes.rb
1
 match "/blog" => redirect("http://yourwordpressblog.com"), :as => :blog

This will match any ‘/blog’ route and redirect to your chosen domain.

Full Text Search With MongoDB

Recently I had to implement full text search on a Ruby on Rails application I was developing and based on my needs the easiest way to do it was building a helper array with the keywords needed and search using those keywords.

To illustrate this I’ve implemented stock tickers search using the awesome rstat.us project. Have a look at my Github fork.

update.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Update
...
    # Adds stocks array mentions
    key: stocks, Array, :default => []

  # Parses update text and build stocks array for simpler search
  def parse_symbols
    stocks = []
    self.text.split(' ').each do |update|
      stocks << update if update[0] == '$'
    end
    return stocks
  end
end

and then the controller:

updates_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
class UpdatesController < ApplicationController
...
    def create
      ...
      u = Update.new(:text => params[:text],
          :referral_id => params[:referral_id],
          :author => current_user.author,
          :twitter => do_tweet)

      u.stocks = u.parse_symbols
      ...
    end
end

So it basically looks for words starting with $ and adds it to the stocks array. We can then search using

Perform search using MongoDB built in text search
1
Update.all(:conditions => {:stocks => '$goog'})

There are different solutions such as mongoid_search that could be used but for my needs this approach works the best.

MongoDB gives a lot of flexibility for the developer and speeds things up. Of course there are trade-offs to be made while using so YMMV.

Starting on the Blogging Path

After trying to blog and failing to continue writing I feel the need to start once more. Hopefully this time I’ll be able to sit down and write about technical subjects that interest me.

My intention with this blog is to share findings and document my learning process as I build and work on different applications.

More to come soon and thanks for stopping by.