Archive for the ‘ruby’ tag
Passenger, Apache, Ruby configuration
Installing passenger to work with apache for rails application is especially pleasant since it gives you direction to setup apache passenger module properly. I have done it several times and it has always worked. Recently I had to change the version of ruby that passenger uses and all I had to do is change the PassengerRuby directive in httpd.conf (or apache.conf) as follows:
#LoadModule passenger_module /opt/ruby-enterprise-1.8.6-20090421/lib/ruby/gems/1.8/gems/passenger-2.2.2/ext/apache2/mod_passenger.so
#PassengerRoot /opt/ruby-enterprise-1.8.6-20090421/lib/ruby/gems/1.8/gems/passenger-2.2.2
#PassengerRuby /opt/ruby-enterprise-1.8.6-20090421/bin/ruby
LoadModule passenger_module /opt/ruby/lib/ruby/gems/1.8/gems/passenger-2.1.2/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby/lib/ruby/gems/1.8/gems/passenger-2.1.2
PassengerRuby /opt/ruby/bin/ruby
I am not sure if this is the recommended way but it worked for me.
Enjoy!
Cygwin: installing Webrat on cygwin
gem install webrat
may fail on cygwin when building native library. In my case, I had to install cygwin package libxslt-devel.
Ruby 1.8.7: NoMethodError: undefined method `[]‘ for Enumerable::Enumerator
After upgrading ruby from 1.8.6 to 1.8.7, you may run into this issue:
NoMethodError: undefined method `[]‘ for Enumerable::Enumerator
To resolve this, create a initializer file (in config/initializer/ruby187_compat.rb)
unless '1.9'.respond_to?(:force_encoding)
String.class_eval do
begin
remove_method :chars
rescue NameError
# OK
end
end
end
Ruby Testing Minimalist support
I have normally used irb shell or simple ruby scripts to try simple things out. I wondered what would be a minimalist unit test which would lend me all the assertion support and related goodies. Today I have it:
require 'test/unit'
class TestSort < Test::Unit::TestCase
def test_something
assert_equal(x,y)
end
end
Enjoy!
Ruby Enterprise different from Ruby MRI
I am in the process of switching a project from Ruby MRI 32-bit to Ruby Enterprise 64-bit. I noticed that they are not exactly the same. For one, the order in which active-record objects are fetched from database is different. This manifests as a failing test assertion when comparing has-many objects. The tests that always passed on ruby MRI fail under Ruby Enterprise.
Also, we noticed that mysql on new machine is different. The primary keys assigned to active-record objects are different from those on old machine. While it is never a good idea to hard-code database IDs for test assertions, it worked never the less since databases are freshly created before each test. This could be due to the architecture differences between those machines (32 vs. 64 bit).
Just a few gotchas …
CGI escape, unescape, escapeHTML, unescapeHTML
I just can’t seem to remember which to use when so here are the 4 great helpers when working with URLs and HTML:
When constructing URLs, you use escape and unescape:
url = "http://site.com/?address=" + CGI.escape("123 Main St, City, ST, 99999") # url: http://example.com/?address=123+Main+St%2C+City%2C+ST%2C+99999
unescape does the reverse.
When rendering text inside HTML page, you use escapeHTML and unescapeHTML:
raw_html = CGI::escapeHTML("<div>An example HTML to be displayed raw on an HTML page</div>") #raw_html: <div>An example HTML to be displayed raw on an HTML page</div>
unescapeHTML is for converting an html-escaped string back to proper HTML form.
Simple, but you often forget!
ActionMailer, GMail and TLS
Prior to rails 2.3.2, if you needed to use GMail as your SMTP carrier (sending out emails from your app), you had to use one of the following plugins to modify the ActionMailer::Base class to support TLS for ActiveRecord.
- tlsmail
- ambethia-smtp-tls
- openrain-action_mailer_tls
- simplificator-tls-support
In rails 2.3.2, this is supported natively, provided you had proper configuration specified:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'localhost.localdomain',
:authentication => :plain,
:user_name => "username",
:password => "password"
}
This should work without any plugins/gems in rails 2.3.2. The only caveat is that ruby version has to be >= 1.8.7. It won’t work with ruby 1.8.6 or older, as documented here.
Enjoy!
Installing passenger/mod_rails on CentOS
Installing passenger/mod_rails on CentOS sometimes is not as simple as:
$ gem install passenger
and
$ passenger-install-apache2-module
In my case, I also had to install “apr-util-devel” yup package. Also, for some reason the “sudo yum install
$ sudo su -
$ yum install apr-util-devel
After this, “gem install passenger” and passenger-apache module install worked fine.
Installing mysql gem on CentOS
Installing mysql gem on cent-os may sometimes be not as easy as:
$ gem install mysql
It may result in compilation error and fail to install. While this may happen for various reasons, in my case, I had to install “mysql-devel” package.
$ sudo yum install mysql-devel
Once I did this, the mysql gem installed happily.
calling private methods in ruby
object.method call won’t work if the method is protected or private. Do this instead:
object.send(:method)