Rubymine print current line
One way of debugging your ruby scripts is to print out the value of variables in different sections of your code.
Rubymine has a feature called live templates that allows you to type a short command and have RubyMine extract it in to full blown code. Some built in shortcuts exist, such as def (creates a method), desc (create a RSpec test), it (RSpec test), etc.
To create your own, go to RubyMine -> Preferences -> Live Templates. Click one of the existing templates and hit Copy.
I named mine pl (Put Line Number) and put the following content in:
puts “#{__FILE__:#{__LINE__}”
Now when I’m typing code, I can hit the following:
pl<TAB>
p “Hello”
And it will print out the following:
/Users/jasonn/source/project/junk.rb:1
“Hello”
I’ve found this helpful for tracking down where a specific print statement is located.
Updated: Rails3 Custom Environment Variables
A while back, I wrote a blog post on adding custom environment variables to Rails3.
This has gotten much easier with a change made by Jose Valim back in March.
The change allows you to create your own custom configuration variables like so:
[ruby]
# config/environments/development.rb
Demo::Application.configure do
[…]
config.json_url = "http://www.domain.com"
[…]
end
[/ruby]
[ruby]
# config/environments/test.rb
Demo::Application.configure do
[…]
config.json_url = "http://test.domain.com"
[…]
end
[/ruby]
This allows you to add any configuration variables you want to your Rails app. What if you have a bunch of configuration values? You don’t want to pollute your config.* namespace. Wouldn’t it be helpful to have config.my_app.json_url and config.my_app.other_url?
That’s easy enough:
[ruby]
# config/environments/application.rb
Demo::Application.configure do
config.my_app = ActiveSupport::OrderedOptions.new
end
[/ruby]
[ruby]
# config/environments/development.rb
Demo::Application.configure do
[…]
config.my_app.json_url = "http://www.domain.com"
[…]
end
[/ruby]
[ruby]
# config/environments/test.rb
Demo::Application.configure do
[…]
config.my_app.json_url = "http://test.domain.com"
[…]
end
[/ruby]
Now you can do the following in the Rails console:
[ruby]
$ rails console
>> Demo::Application.config.my_app.json_url
"http://www.domain.com"
>>
[/ruby]
[ruby]
$ rails console test
>> Demo::Application.config.my_app.json_url
"http://test.domain.com"
>>
[/ruby]
Now your Rails application will have access to all the configuration you need.
Visiting Pivotal Labs in Boulder, CO
I recently had the opportunity to visit Boulder, CO (Wiki, Map). The reason for my visit was to visit the Pivotal Labs office and spend a day pairing with them.
One of the cool/interesting features of the Pivotal office is that the company provides breakfast in the morning. I walked into the office to find two quiches, pears, strawberries, Orange Juice, Milk, Coffee and several choices of breakfast cereal.
The day started out with a standup. We then split off into pairs and worked on our respective projects. It was interesting to be thrown into the fire and within a half hour I was working on a large, well known website, implementing features that are now live on that site.
We split for lunch around 11:30. After lunch we switched pairs and I worked on a different portion of the code. Pivotal has a strong TDD/BDD approach to coding. We wrote tests that failed, then wrote the code to make them pass. After all the tests passed, we refactored. This is what Rails development is supposed to be.
The afternoon/evening was rounded out with a team retrospective meeting. After the meeting, the office all joined in for a game of StarCraft II.
I thought my interview/pairing day went really well, and I’m happy to say that I’m moving to Boulder to start working for Pivotal Labs March 14th.
Here’s some pics:
The Snow was falling as I arrived at Denver International Airport
The Pivotal office has a full size, full time ping-pong table
This is the view from the office.
The spotting scope is from a Russian Destroyer. One of the Pivots said during the summer you can see mountain climbers climbing the distant mountains. The large grill is for hosting summer parties.
This is from across the street. Pivotal Labs is on the second floor. Ted’s is an easy lunch.
Rails3 Custom Environment Variables
I recently saw a question on Stack Overflow regarding custom variables that have different values based on which environment you are running for Rails.
It turns out it’s very easy to accomplish.
[ruby]
# config/initializers/configuration.rb
class Configuration
class << self
attr_accessor :json_url
end
end
[/ruby]
This gives you access to a Rails variable called Configuration.json_url. Now we just need to initialize this value in our environment files.
[ruby]
# config/environments/development.rb
# Put this inside the ______::Application.configure block
config.after_initialize do
Configuration.json_url = "http://test.domain.com"
end
[/ruby]
[ruby]
# config/environments/test.rb
config.after_initialize do
Configuration.json_url = "http://www.domain.com"
end
[/ruby]
Now you can test it in the Rails console.
[text]
$ rails console
>> Configuration.json_url
"http://test.domain.com"
>>
[/text]
[text]
$ RAILS_ENV=production rails console
>> Configuration.json_url
"http://www.domain.com"
>>
[/text]
Now your Rails app has access to these variables, so you can use them in your Controllers, Models or Views as needed.
Pushing Depot to Heroku
I’m working my way through Agile Web Development with Rails, 4th edition by Sam Ruby.
I just finished up with Chapter 6: Creating the Application. It’s working fine on my machine, but I wanted to push it to Heroku. Here’s how I did it:
[plain]
echo "gem ‘heroku’" >> Gemfile
bundle install
git init
git add .
git commit -m ‘Initial commit’
heroku create
git push heroku master
heroku rake db:migrate
heroku rake db:seed
heroku open
[/plain]
I then added a /products on the Heroku URL that came up and my Depot application is now running on Heroku.
Playing around with Conductor by DHH
A while back, I saw that DHH had published a rails engine called Conductor. I added it to my list of things to look at and tonight, I got some time to take a look.
I’m using RVM with 1.9.2p0 and rails 3.0.1.
[plain]
rails new conductor && cd conductor
[/plain]
I edited my Gemfile and added the following line:
[plain]
gem ‘conductor’, :git => ‘http://github.com/dhh/conductor.git’, :require => ‘conductor/engine’
[/plain]
I then ran “bundle install”.
I fire up rails server and hit http://localhost:3000/conductor (as suggested by the README) and I get an error:
[plain]
Routing Error
No route matches "/conductor"
[/plain]
Am I missing something? I need to research Rails3 engines some more.
Straight HTML/CSS for a change of pace
I recently worked on a personal project involving HTML and CSS. My wife’s family is trying to sell a 52 acre parcel of land in Noxon, MT. They wanted a website that showed the property better than the real estate agent was able to provide (short summary, only one photo).
The site I came up with is quickly rising in the results for Montana land for sale.
Hopefully this page will help as well. </lame seo attempt>
I got Google Analytics setup and I think Google Adsense is next.
Cruise control.rb refusing to build a project
I ran into an interesting problem at work today. I had a cruise control project that would not build the project after a commit was made. Other projects in CruiseControl.rb were working properly.
Here’s the steps I used to debug:
Login to your Cruise Control box and cd to $BUILD_DIR/projects/$PROJ_NAME/work
Run the following command. This is how CC.rb figures out if updates have been made.
[plain]git log –pretty=raw –stat HEAD..origin/master[/plain]
When I ran this command, I was getting “warning: refname ‘origin/master’ is ambiguous.” returned. After some google searches, I landed upon some mailing list messages that pointed me towards the issue.
The reason git was complaining about origin/master being ambiguous was that someone created a tag named “origin/master” (the beating has been scheduled).
I had to delete this tag locally by running the following command.
[plain]git tag -d origin/master[/plain]
Then I had to delete the remote branch by running the following.
[plain]git push origin :refs/tags/origin/master[/plain]
Now the CC server is working properly.
Testing syntax highlighting
[java]
public class HelloBlogger {
public static void main(String [] args) {
System.out.println("Hello Blogger!!");
}
}
[/java]
rspec file is newer than
My pair and I were working on implementing LessCSS at work this week and we ran into an interesting problem. We wanted to write RSpec tests that would figure out if the .less file was newer than the .css file, and if so provide a good error message.
We discussed this with Hosh and he suggested using a custom matcher, which we had not heard of before. In researching custom matchers, we came up with the following solution:
[ruby]
def be_newer_than(new_file, msg=nil, msg2=nil)
simple_matcher do |old_file, matcher|
old_file_mtime = File.stat(old_file).mtime
new_file_mtime = File.stat(new_file).mtime
matcher.description = "be newer than #{new_file.inspect}"
matcher.failure_message = msg || "expected #{old_file.inspect} to be newer than #{new_file.inspect}"
matcher.negative_failure_message = msg2 || "expected #{old_file.inspect} to be older than #{new_file.inspect}"
old_file_mtime >= new_file_mtime
end
[/ruby]
This allows you to do the following:
[ruby]
css_file = "site.css"
less_file = "site.less"
css_file.should be_newer_than(less_file)
[/ruby]
This will tell you if css_file is newer than the less file.
The optional arguments allow you to pass custom error messages. For example:
[ruby]
it "should be up to date" do
output = "#{css_file} is not up to date. Please run the following: "
output += "\n\t lessc #{less_file}\n\n"
css_file.should be_newer_than(less_file, output)
end
[/ruby]