Posts Tagged ‘ruby’

Deploying GitHub gems to AppFog

February 28th, 2013

Mega-big thanks to georgebrock on this Stack Overflow question.

I was trying to deploy to AppFog using a Ruby Gem that wasn’t published on RubyGems.org.  Well, it was, but this was someone else’s fork of it.

To make that work, in your Gemfile, don’t use the “git@” version of a GitHub repo’s URL.  Use the “git://” version.  For example:

gem 'best_in_place', 
    :git=>'git@github.com:tangledpath/best_in_place.git', 
    :branch=>"lazy_load_collection_url"

becomes:

gem 'best_in_place', 
    :git=>'git://github.com/git-steven/best_in_place.git', 
    :branch=>"lazy_load_collection_url"

Apparently, that’s how you deal with it on Heroku too.

RVM woes on Ubuntu 11.04

May 19th, 2011

Having had an epiphany the other day, I needed this morning to start work on a new prototype.  This post isn’t about that prototype.

It IS about the new setup I was using to do that prototype.  I figured this was a good chance to try out RVM in a Rails project, as well as try out Ubuntu 11.04, as well as install the OS in French.  So, with the new VM up and running, I follow RVM’s installation instructions, anticipation building with each scrolling character.  And then… BAM!

ethan@ubuntu11:~/rvmsource$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Cloning into rvm…
remote: Counting objects: 4765, done.
remote: Compressing objects: 100% (2433/2433), done.
remote: Total 4765 (delta 3086), reused 3183 (delta 1672)
Receiving objects: 100% (4765/4765), 1.57 MiB | 322 KiB/s, done.
Resolving deltas: 100% (3086/3086), done.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/base’ as file does not exist.
RVM will likely not work as expected.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/version’ as file does not exist.
RVM will likely not work as expected.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/selector’ as file does not exist.
RVM will likely not work as expected.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/cd’ as file does not exist.
RVM will likely not work as expected.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/cli’ as file does not exist.
RVM will likely not work as expected.
WARNING:
Could not source ‘/home/ethan/.rvm/scripts/override_gem’ as file does not exist.
RVM will likely not work as expected.
cat: /home/ethan/.rvm/VERSION: Aucun fichier ou dossier de ce type

A few Google searches turned up a few people having a similar problem but no solutions.  But whould’ve thunk it that installing the OS in French would actually have some practical value?  Google must’ve seen that and then dumped me on to this forum, wherein one of the posters linked to this post, which basically says to just follow the directions that RVM spit out as part of the big error message.  Who’d've thunk THAT?  I sure didn’t.  I just focused on the error message itself in my searches.

WARNING:  you have a ‘return’ statement in your ~/.bashrc
This could cause some features of RVM to not work.

This means that if you see something like:

‘[ -z "$PS1" ] && return’

then you change this line to:

if [[ -n "$PS1" ]] ; then

# … original content that was below the ‘&& return’ line …

fi # <= be sure to close the if at the end of the .bashrc.

# This is a good place to source rvm v v v
[[ -s "/home/ethan/.rvm/scripts/rvm" ]] && source “/home/ethan/.rvm/scripts/rvm”  # This loads RVM into a shell session.

It looks like RVM tries to run outside of a standard shell session, and the way .bashrc is configured by default on Ubuntu 11.04 prevents that sort of thing.  When you’re done with your edits your .bashrc should look something like this:

# ‘[ -z "$PS1" ] && return’ <—– delete this line.  It isn’t commented out in the original

if [[ -n "$PS1" ]] ; then

# the rest of your old .bashrc contents

fi

[[ -s "/home/ethan/.rvm/scripts/rvm" ]] && source “/home/ethan/.rvm/scripts/rvm”  # This loads RVM into a shell session.

After making that change I re-ran the installation instructions, and PRESTO!  Or maybe I should say, Voilà!  As always, YMMV.  I really don’t think it was the double install that fixed the problem.  And be sure to replace “ethan” with whatever your actual path is.  That happens to be mine, because, well, that’s my name.

Best comment ever

March 23rd, 2011

Comments are a wonderful addition to any code base, especially when used well.  As for a definition of used well, that is a Holy War I do not wish to start here.

Anyway, I came across the following comment.  Hopefully it isn’t revealing any proprietary information:

# something

That isn’t a typo.  That’s a verbatim reproduction of the comment.

How about you?  What’s the best comment you’ve ever seen?

 

Deprecation warnings in Rails

March 22nd, 2011

Are you working on an ever-evolving code base? Did your project start with an initial engineering effort to fix a specific problem / explore the solution space? As requirements are more fully understood, are you increasingly annoyed with the older code to support different data formats and the like? Do you want to get developers and others to stop using those old formats?

Well, you too can have access to what Rails uses to put deprecation warnings in the log file.

ActiveSupport::Deprecation.warn("Stop calling me!")

It of course takes a good team policy to require all such warnings to be removed, but I was delighted to find that this is readily available and usable for my own deprecation warnings. It sure beats using Rails.logger, because the intent is clearer, and such warnings will get shut off when all deprecation warnings are turned off anyway.

When having trouble installing the mysql gem on OSX

April 21st, 2010

And you get an error like:

*** extconf.rb failed ***

Go read:

http://blog.bmn.name/2008/02/rails-gem-install-mysql-throws-error-extconfrb-failed

Installing Ruby gems that require building native extensions on Snow Leopard

December 18th, 2009

I haven’t tested this on all “machines” running Snow Leopard, but on the brand new one I had to get today for work, I found that the default Ruby / Gems install doesn’t support installing gems that require building native extensions. The error message I was treated to contained the following:

MacBook-Pro-de-Ethan-Garofolo:~ juanpaco$ sudo gem install thin
Password:
Building native extensions. This could take a while...
ERROR: Error installing thin:
ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install thin
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/eventmachine-0.12.10 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/eventmachine-0.12.10/ext/gem_make.out

The solution was hyper-intuitive. Install XCode. Because of course that's the first thing you think of.

Renaming a database in Rails

December 14th, 2009

Hooray for a non-rant post!

The heart of this method works outside of Rails, but there’s one particular convenience method that Rails provides.  Find some way to mimic that behavior, and the rest would apply.  Also, this is a MySQL-specific solution.  I haven’t tried it with any other DB systems.

We “rename” a database by moving all of its tables into a new database.  We do so via MySQL’s RENAME TABLE command.  Basically, create an empty database with the new name.  We then use our ActiveRecord connection’s “tables” method to loop over all the tables in our database, executing a RENAME TABLE from the old to the new with each iteration. “Tables” is the convenience method I mentioned above. Encapsulating this in a method we get:

def rename_database(old_db_name, new_db_name)
    ActiveRecord::Base.connection.execute "CREATE DATABASE #{new_db_name}"
    ActiveRecord::Base.connection.tables.each do |table|
        ActiveRecord::Base.connection.execute
            "RENAME TABLE #{old_db_name}.#{table} TO #{new_db_name}.#{table}"
    end
    ActiveRecord::Base.connection.execute "DROP DATABASE #{old_db_name}"
end

Error checking is left as an (important!) exercise to the reader.  Also, you may prefer to monkey patch this into ActiveRecord itself as opposed to some odd method floating around in global space.

This presupposes that the new database destination will be on the same server.

Another way to rename a database is to mysqldump the old one to a file and then reimport it to the renamed database.  The method presented in this article is advantageous if your database has a large amount of data.  Renaming a table is almost instant.  Dumping millions of rows and reimporting them is not so instant.

How to tell where your gems are installed

September 16th, 2009

From http://groups.google.com/group/rubypgh/msg/0489eed858e642a0:

gem environment

Type the above. That is all.

uninitialized constant Thin::Server::Forwardable

July 2nd, 2009

I may be getting batty in my old age here, but for whatever reason Thin decided to just stop running on my system.  When trying to invoke thin to further development on the code behind my awesome website, I was treated to the following:


ethan:ethang juanpaco$ thin start
/Library/Ruby/Gems/1.8/gems/thin-1.0.0/lib/thin/server.rb:51: uninitialized constant Thin::Server::Forwardable (NameError)
from /Library/Ruby/Gems/1.8/gems/thin-1.0.0/lib/thin/runner.rb:38:in `initialize'
from /Library/Ruby/Gems/1.8/gems/thin-1.0.0/bin/thin:6:in `new'
from /Library/Ruby/Gems/1.8/gems/thin-1.0.0/bin/thin:6
from /usr/bin/thin:19:in `load'
from /usr/bin/thin:19

I didn’t like that.

I went to the line number where the error occurred (/Library/Ruby/Gems/1.8/gems/thin-1.0.0/lib/thin/server.rb:51), and found:
extend Forwardable

Not knowing what Forwardable was, and for giggles, I went to irb and typed “Forwardable” thinking I’d get the class object.  Not defined.  So I try “require ‘forwardable’.”  There was no period in what I typed, but I haven’t figured out how to get Ruby and English to play well together.  Anyway, that worked, and a subsequent typing of “Forwardable” returned the object. So then I tried putting require ‘forwardable’ at the top of server.rb in thin’s gem.

And then it worked.

I’m not sure why it used to work and then suddenly didn’t.  This was after several install/uninstall, swear off programming moments.  Weird.  So, if you’re struggling with this same problem, perhaps the above can be of service.