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
  • 15 hours ago
  • Comments
  • Permalink
  • Share
    Tweet

How to install RMagick (2.13.1) with latest ImageMagick (6.8.0-10)

I need to use RMagick v2.13.1 in a current project, but RMagick v2.13.1 doesn’t works with the latest ImageMagick (6.8.0-10).

The workaround I found is: first install ImageMagick via Homebrew:

brew install imagemagick

and then:

$ ln -s /usr/local/Cellar/imagemagick/6.8.0-10/lib/libMagick++-Q16.7.dylib   libMagick++.dylib
$ ln -s /usr/local/Cellar/imagemagick/6.8.0-10/lib/libMagickCore-Q16.7.dylib libMagickCore.dylib
$ ln -s /usr/local/Cellar/imagemagick/6.8.0-10/lib/libMagickWand-Q16.7.dylib libMagickWand.dylib

Now RMagick v2.13.1 should install and works

Of course another option is just upgrade RMagick to v2.13.2 which works out-of-the-box with latest ImageMagick

    • #RMagick
    • #ImageMagick
    • #rmagick-2.13.1
    • #imagemagick-6.8.0-10
    • #brew
    • #homebrew
  • 2 days ago
  • Comments
  • Permalink
  • Share
    Tweet

MongoDB shell inserts Float when try to insert an Integer/Long

Because of numbers are doubles in Javascript, when yo do this:

> db.test.insert({name: ‘foo’, value: 1});

you get this:

> db.test.findOne({name:  ’foo’});
{‘name’: ‘foo’, ‘value’: 1.0}

To avoid this you can use NumberInt or NumberLong:

> db.test.insert({name: ‘foo’, value: NumberLong(1)});
    • #mongodb
    • #mongo
    • #javascript
    • #js
  • 1 month 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

RVM: How to remove all gems from default gemset

To remove all gems from a gemset you use the gemset empty command:

$ rvm gemset empty <my_gemset_name>

And to remove the gems from default gemset, you must supply an empty string as the gemset name:

$ rvm gemset empty ""
    • #rvm
    • #gems
    • #gemset
  • 3 months ago
  • Comments
  • Permalink
  • Share
    Tweet

How to cURL and JSON prettyprint

cUrl is a simple and powerful tool to test RESTful APIs, but sometimes the output could be messy, specially when the response is a long JSON.

To improve the output readability, I’m using cUrl with jsontool (a node.js script).

After you install it:

$ npm install -g jsontool

Then pipe the cUrl output to jsontool

$ curl -is http://search.twitter.com/search.json\?q\=foo | json -i

Below an example of the output

Sample of cUrl with JSON prettyprint

    • #curl
    • #json
    • #prettyprint
  • 3 months ago
  • 1
  • Comments
  • Permalink
  • Share
    Tweet

How to remove a Git submodule and put the code into the main repo

You need to remove the submodule and re-add the files into the main repo.

Edit the .gitmodules and .git/config files and remove the lines that specify the submodule, then;

git rm --cached path/to/submodule # delete reference to submodule HEAD (no trailing slash)
rm -rf path/to/submodule/.git
git add path/to/submodule # add files instead of commit reference
git commit -m "remove submodule"
    • #git
    • #submodule
  • 4 months ago
  • 1
  • Comments
  • Permalink
  • Share
    Tweet

Permission denied (publickey) when deploying heroku code - Sometimes is the git remote …

Today I tried to deploy to heroku a new version of an application (that not has been deployed in several months):

git push heroku master

but I got the following error:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

The weird part is that I can deploy other 3 apps without problem, that means that my public SSH key is properly stored on Heroku.

If the Heroku api is experiencing downtime, you will get this error when you try to push. So, I checked https://status.heroku.com/ and everything seemed to be fine on Heroku’s end.

Then I checked my git remotes, and my problem was that the remote heroku repository was corrupted. I refreshed it as follows:

git remote -v

Then removed the heroku one that is wrong:

git remote rm heroku

Then added the new one

git remote add heroku git@heroku.com:myapp.git
    • #heroku
    • #git
    • #remote
  • 4 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

How to delete large amount of data of a MongoDB collection “quickly”

We have a db collection that is around 30 million documents, and I need to trim it down, to only keeping the documents created on the last month. 

One approach would be use the remove command with a condition on the created_at field (the collection already have an index on this field):

db.my_collection.remove({created_at: {$lte: new Date("11/01/2012")}});

But this approach will be very slow, instead of that, a better way to do it is rename the current collection (for instance to “old_collection”) using renameCollection. Then performing a query-and-insert from the “old_collection” into “my_collection”:

db.my_collection.renameCollection("old_collection");

db.createCollection("my_collection");

db.my_collection.createIndex(...); // recreate the indexes for the collection

// copy docs from old collection into the new collection
db.old_collection.find(
    {created_at: {$gte: new Date("11/01/2012")}
  ).sort({_id: -1}).forEach(
    function(row) { db.my_collection.insert(row); }
  );

// drop old collection
db.old_collection.drop();

This approach is typically faster than running a bunch of removes on your data

    • #MongoDB
    • #renameCollection
    • #remove
  • 5 months ago
  • 2
  • Comments
  • Permalink
  • Share
    Tweet
← Newer • Older →
Page 1 of 11

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