Avatar Edgar Gonzalez

  • Random
  • Archive
  • RSS

Migrating FasterCSV from ruby 1.8 to CSV in 1.9+

Currently in StreetEasy we’re moving a Rails app from Ruby 1.8 to 1.9

In Ruby 1.9 FasterCSV is already included as standard library’s CSV. And in Ruby 1.9 you can’t continue using FasterCSV, if you do, you’ll get an error message like this:

Please switch to Ruby 1.9's standard CSV library.
It's FasterCSV plus support for Ruby 1.9's m17n encoding engine.

The workaround for this is, instruct bundler to include fastercsv only if I’m running ruby 1.8:

gem 'fastercsv', :platforms => :ruby_18

And change references in the application from FasterCSV to CSV, and add this (in a Rails app usually in the config/environment.rb):

require 'csv'
if CSV.const_defined? :Reader
  # Ruby 1.8 compatible
  require 'fastercsv'
  Object.send(:remove_const, :CSV)
  CSV = FasterCSV
else
  # CSV is now FasterCSV in ruby 1.9
end

Based on a James Edward Gray II post

    • #ruby
    • #ruby1.8
    • #ruby 1.8
    • #ruby1.9
    • #ruby 1.9
    • #csv
    • #FasterCSV
  • 3 days ago
  • Comments
  • Permalink
  • Share
    Tweet

RubyCheckOnSave Sublime Text 2 Plugin

More than one year ago, I created a very simple script that enable Sublime Text 2 to automatically check the syntax of ruby files when they are saved

Three months ago I finally found the time to convert that script into my first Sublime Text 2 plugin: the RubyCheckOnSave plugin.

    • #ruby
    • #SublimeText2
    • #sublimetext
    • #plugin
    • #rubycheckonsave
  • 3 months ago
  • Comments
  • Permalink
  • Share
    Tweet

How to sort in ruby an enumerable by an attribute that could be nil

I have an Enumarable of tokens, and I need to sort them descending by the last_access attribute, but for some objects this attribute could be nil. One OOP approach to do it, is defining the <=> method (Comparable) in the Token class:

class Token
  include Comparable

  define <=> other
    ...
  end
end

Then I’ll only need to call tokens.sort. But I’m not ready to change the behavior of the Token class in that way, instead of that I prefer to follow an approach that don’t have side effects. So, an easy hack is to use an immediate-if like this:

tokens.sort{|a,b| 
  (a.last_access && b.last_access) ? 
    a.last_access <=> b.last_access : 
    (a.last_access ? 1 : -1) 
}.reverse!
    • #ruby
    • #sort
    • #enumerable
    • #array
    • #nil
  • 5 months ago
  • 2
  • Comments
  • Permalink
  • Share
    Tweet

Ruby on Rails :: How to add HTTP basic authentication to your staging app in Heroku

First you set up your staging app following the Heroku’s guide for Managing Multiple Environments for an App

Then edit the config/environments/staging.rb:

#config/environments/staging.rb
MyApp::Application.configure do

  # Basic authentication
  config.middleware.insert_after(::Rack::Lock, "::Rack::Auth::Basic", "My App") do |u, p|
    [u, p] == [ENV['HTTP_USER'], ENV['HTTP_PASSWORD']]
  end

  ...

And finally set the config vars:

heroku config:add HTTP_USER='foo' HTTP_PASSWORD='bar' -a myapp-staging
    • #rails
    • #heroku
    • #authentication
    • #ruby
    • #rack
  • 9 months ago
  • 2
  • Comments
  • Permalink
  • Share
    Tweet

The annoying “warning: useless use of == in void context” in RSpec

If in your specs you have something like this:

it "should be bar" do
  foo.should == 'bar'
  something_else
end

The ruby -w command will throw the following warning:

warning: useless use of == in void context

The way to avoid this is using this:

foo.should eql('bar')

Or:

foo.should be == 'bar'

Or move the statement

foo.should == 'bar'

so that it’s the last line inside the it block

    • #ruby
    • #rspec
  • 10 months ago
  • 1
  • Comments
  • Permalink
  • Share
    Tweet

Ruby 1.8.7 and multi_json

I’m starting to work on a gem for Singly API that uses multi_json.  The tests for the gem worked in 1.9.2 and 1.9.3 but I got the following runtime error in 1.8.7:

missing dependency for FaradayMiddleware::ParseJson: no such file to load -- json

I have been using 1.9.3 more and more lately and I forgot that json was added to the stdlib in 1.9.  So that’s why it works in 1.9.x. while in 1.8.7 I need to require a supporting library like yajl.

    • #ruby
    • #multi_json
    • #json
    • #1.8.7
    • #1.9.2
    • #1.9.3
  • 11 months ago
  • Comments
  • Permalink
  • Share
    Tweet

How emacs changed my life

(or Ruby is what it is, thanks to emacs)

by @yukihiro_matz

    • #emacs
    • #ruby
  • 1 year ago
  • Comments
  • Permalink
  • Share
    Tweet

SublimeText2 - Check ruby syntax after save

Edit the file: /path/to/SublimeText2/Packages/Ruby/Ruby.sublime-build to contain this:

{
    "cmd": ["/path/to/ruby/bin/ruby", "-cw", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.ruby"
}

Then create the following file /path/to/SublimeText2/Packages/User/ruby_check.py:

import sublime, sublime_plugin

class rubyCheck(sublime_plugin.EventListener):
    def on_post_save(self, view):
        if view.file_name()[-3:] == '.rb':
            view.window().run_command("build")

Now when you save your ruby file automatically the syntax will be checked

Here the gist

UPDATE 02/01/2013: recently I created the RubyCheckOnSave plugin.

    • #ruby
    • #sublimetext2
    • #sublimetext
    • #rubycheckonsave
  • 1 year ago
  • 4
  • Comments
  • Permalink
  • Share
    Tweet

How compare versions strings in ruby

There is a lot of way to compare versions strings in ruby, for instance you turn each version string in to an array of integers and then use the array comparison operator. But I prefer this one:

Gem::Version.new('0.3.2') < Gem::Version.new('0.10.1')

    • #ruby
    • #comparison
    • #version
  • 1 year ago
  • 13
  • Comments
  • Permalink
  • Share
    Tweet

MongoDB how to compare two IDs fields on same document

To compare two fields on same document in MongoDB you can use a $where (just be aware it will be fairly slow, has to execute Javascript code on every record) :

db.my_collection.find( "this.fieldA > this.fieldB" } );

(check this thread on StackOverflow)

In my case I want to find all the documents where a field id_A is equal to field id_B, so I tried this:

db.my_collection.find( "this.id_A == this.id_B" } );

but this not works because you can’t overload == in javascript, so the way to do is:

db.my_collection.find( "this.id_A.equals(this.id_B)" } );

And from ruby with MongoID you use:

MyCollection.where("$where" => "this.id_A.equals(this.id_B)")
    • #mongodb
    • #id
    • #field
    • #ruby
    • #mongoid
  • 1 year ago
  • 7
  • Comments
  • Permalink
  • Share
    Tweet
← Newer • Older →
Page 1 of 2

Portrait/Logo

About

Edgar Gonzalez

Father, husband, and software developer. I'm building web applications since 1994 for a living and I love it.

Rubyist, Rails developer since 2006, believer of Agile Development and Lean startups, NoSQL enthusiast.

Born in Caracas - Venezuela, living in New York.

Me, Elsewhere

  • @edgar on Twitter
  • Facebook Profile
  • edgargonzaleznetve on Youtube
  • egg on Flickr
  • edgar on Foursquare
  • Google
  • My Skype Info
  • Linkedin Profile
  • edgar on github

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr