Posted in RSpec, RubyOnRails on November 30, 2007 | 1 Comment »
I’m barely even a beginner at using RSpec, but one of the first tests I needed to do involved Attachment_Fu.
Here’s what I did to get my model to pass validation:
require File.dirname(__FILE__) + ‘/../spec_helper’
describe Image do # # I created the spec/images directory and placed this # test image prior to running [...]
Read Full Post »
Posted in Ruby on November 29, 2007 | Comments Off
This is one of the reasons I’m a fan of Ruby:
class << Math def log2(n) log(n) / log(2) end end
class Integer def nearest_power_of_2 2**(Math.log2(self).floor) end end
47.nearest_power_of_2 => [...]
Read Full Post »
Posted in RubyOnRails on November 29, 2007 | Comments Off
Rails 2.o has a new organizational concept with the addition of the config/initializers directory. By default you will have the inflections.rb and mime_types.rb initializers.
I have used a few different approaches to organizing application constants. However, no approach felt right. I didn’t like ‘polluting’ the environment.rb with them because I never thought that [...]
Read Full Post »
Posted in RubyOnRails on November 29, 2007 | Comments Off
A new ‘discovery’ today. I love finding the occasional meta programming derived method that isn’t explicitly listed with the other methods on the rails api site.
find_or_initialize_by_<field>find_or_create_by_<field>
They do what you think. For example if I did:
user = User.find_or_initialize_by_login(“stonean”)
If the record exists, all is good. If not, well then you can either get an [...]
Read Full Post »
Posted in Quick Tips on November 20, 2007 | Comments Off
To indent a bunch of lines at once just highlight them and hit the Tab key (Shift-Tab to undo indention).
Read Full Post »
Posted in Quick Tips on November 20, 2007 | Comments Off
I didn’t want lighttpd starting on boot so I removed it:sudo launchctl unload -w /Library/LaunchDaemons/org.macports.lighttpd.plistBut then I couldn’t figure out how to start it. Bummer. Then I finally found:/opt/local/etc/LaunchDaemons/org.macports.lighttpd/lighttpd.wrapper start
Read Full Post »
Posted in Quick Tips, Ruby, RubyOnRails on November 20, 2007 | Comments Off
In the category of why I haven’t I used this yet (duh!):
run: gem_serverto run as daemon: gem_server –daemon
then access: localhost:8808
Documentation for your installed gems. Nice.
If you don’t have any documentation, run: gem rdoc –all
This will generate the docs for all the gems installed on your system.
Note: gem_server has been deprecated in favor of [...]
Read Full Post »
Posted in Interview Questions, RubyOnRails on November 20, 2007 | Comments Off
These questions are intended to query for intermediate level knowledge of the Rails framework.
Q) I have two tables, users and addresses. The users table has a foreign key column to the addresses table called address_id. This is a one user to a one address setup. How do I represent that relationship in the User ActiveRecord [...]
Read Full Post »
Posted in Interview Questions, Ruby on November 20, 2007 | 1 Comment »
Interview questions/answers for an entry level Ruby developer.(Use these questions in conjunction with the 101 level Rails questions)
Q) How do you comment out a block of code?
A) Use =begin and =end.
=begin def my_commented_out_method end =end
You could use successive # signs, but that’s just tedious:## def my commented_out_method# end#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) How do [...]
Read Full Post »
Posted in Interview Questions, RubyOnRails on November 20, 2007 | Comments Off
Interview questions/answers for an entry level RubyOnRails developer:
Q) What site contains the Rails framework documentation?
A) api.rubyonrails.com
Note: When I was starting to learn Rails, there was always a tab open for this site. This is a guess that other developers learning Rails would be utilizing this page as well.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q) What does RAILS_ROOT represent?
A) The base directory [...]
Read Full Post »