Fake Data for Testing
How are you generating fake data for testing? I usually use popular athlete names and that’s really easy, but what about addresses and phone numbers. Aren’t they horrible? That’s been my experience.
Take a look at Faker gem. It randomly generates “reasonable” fake data. It even has the ability to grab from BS wordlist. To install, run sudo gem install faker and you’re good to go. For me, it failed twice before installing. I’ve noticed that with other gems as well, wonder what’s going on with RubyForge…
To test it, from irb, try this:
require 'faker'
a = Faker::Name
puts a.name
It should come back with a randomly generated name.
It’s a great gem. Have fun with it!
Gems in Leopard
I noticed that the gems I updated and installed break in Leopard. This is great since it breaks my previous projects as well. Nice Apple!
Here’s a quick fix from a fellow Pragmatic Studio alunmi, Luis de la Rosa.
http://www.luisdelarosa.com/2007/11/10/ruby-bugs-on-leopard/
Luis does a great job of explaining why.
Happy Thanksgiving!
I would like to wish everyone a safe and fun Thanksgiving, especially to those traveling. I apologize in advance for anyone who gets offended by me saying Happy Thanksgiving or Merry Christmas. I just can’t bring myself to say “Happy Holidays”.
Model Doesn’t Have To Be An ActiveRecord
It annoys me when a new Rails developer automatically assumes that all models are ActiveRecord. Here’s an example of a model in one of my applications where I’ve decided to put a class that retrieves the information from the web and behaves like a model. I could’ve put it in helper or module, but it seems appropriate to put it in a model. Obviously this object doesn’t have CRUD since it’s read-only and you can use parse method to get the results.
This model retrieves the history of NJ lottery results and provides it as an array. It also puts the results in a local file so that it doesn’t hit the website every incident. This is also useful when you don’t have a web service running on the provider server.
require 'net/http'
class WinningNumbers
attr_accessor :number_history, :game
def initialize(game)
case game
when "mega"
@location = "/lottery/data/big.dat"
@game = "mega"
when "pick_6"
@location = "/lottery/data/pick6.dat"
@game = "pick_6"
else
@location = "/lottery/data/big.dat"
@game = "mega"
end
end
def fetch
history = ""
Net::HTTP.start( 'www.state.nj.us', 80 ) do |http|
history = http.get( @location ).body
end
File.open(File.join(RAILS_ROOT, 'data', "#{@game}.dat"), 'w') do |file|
history.split("\n").each do |l|
file.puts l.split("%")[4..9].join(" ")
end
end
end
def parse
open(File.join(RAILS_ROOT, 'data', "#{@game}.dat")) {|file| file.readlines }.each {|l| l.strip! }
end
So there, don’t be afraid to create a model without ActiveRecord.
Debian Etch & MySQL Gem
I just got another VPS for a client’s app and found it interesting that my normal MySQL gem installation was giving me problems. It simply will not work. Since I didn’t compile MySQL from the source, but used the one supplied by Debian, I had this feeling that something is not right.
The solution was quite simple. Here’s what I did (as the root):
apt-get install libmysqlclient-dev
gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
This may happen to you since VPS providers use different OS images. FYI, this was a VPS from VPSLand.
RubyConf Video
If you’re like me, a family man with kids, then you know it’s not really possible to take attend a conference just for your pleasure. I really wanted to attend RubyConf, more so than RailsConf (there’s a reason why this blog is RubyHead, not RailsHead). I’m so glad that those guys at confreaks.com are making it possible to attend the talks at RubyConf2007.
They just posted the first video over the weekend, the town meeting with Matz.
The link to the videos is here.
I know it’s not the same thing as attending the conference, but at least I get to see and hear really interesting talks.
New Ruby Logo
One of the first thing I do and enjoy in the morning is reading messages from Ruby newsgroup using Unison. I’ve been keeping up with the thread on new logo for Ruby and I have to agree with the majority of people, it’s ugly! It’s so ugly that I don’t want to post it here, but if you’re interested, take a look using google.