Wednesday, January 28, 2009

Why "require 'rubygems'" In Your Library/App/Tests Is Wrong

I have been suitably convinced...

In response to all the responses to:
http://twitter.com/rtomayko/status/1155906157
You should never do this in a source file included with your library,
app, or tests:
require 'rubygems'
The system I use to manage my $LOAD_PATH is not your library/app/tests
concern. Whether rubygems is used or not is an environment issue. Your
library or app should have no say in the matter. Explicitly requiring
rubygems is either not necessary or misguided.
When you feel the urge to "require 'rubygems'" because some shit isn't
working, stop and consider whether one of these solutions is more
appropriate:
1. RubyGems installs special versions of executables included with
gems. Here's rake's as an example:
$ cat /opt/local/bin/rake
#!/opt/local/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem 'rake', version
load 'rake'
In this case, rubygems is required when the executable is run. Explicitly
requiring rubygems in code loaded by these files doesn't make sense. The
whole point of these wrapper scripts is to push the rubygems loading
machinery into the environment and out of your app/library/tests.
2. When running "ruby FOO.rb" and a LoadError occurs because rubygems
is not available, DO NOT add "require 'rubygems'" to FOO.rb. Instead,
run the command as "ruby -rubygems FOO.rb". This will require rubygems
before evaluating FOO.rb.
3. When running "ruby FOO.rb" and a LoadError occurs because rubygems
is not available, DO NOT add "require 'rubygems'" to FOO.rb. Instead,
set the RUBYOPT environment variable:
$ RUBYOPT="rubygems"
$ export RUBYOPT
$ ruby FOO.rb
You can even put that in your ~/.{bash,zsh,sh}rc if you prefer to always
have rubygems loaded and available.
Why You Shouldn't Force Rubygems On People
------------------------------------------
When I use your library, deploy your app, or run your tests I may not want
to use rubygems. When you "require 'rubygems'" in your code, you remove my
ability to make that decision. I cannot unrequire rubygems, but you can
not require it in the first place.
view raw Tests Is Wrong hosted with ❤ by GitHub


http://gist.github.com/54177

Recommended Ruby, Rails, REST books!

I have read the following books (some more closely than others). I'd recommend each of them for various reasons. All have been helpful in understanding many of the pragmatic concepts baked into Rails, understanding the Ruby programming language, and web development in general. I hope you have a chance to check them out on your own path of learning!
:Mark

RESTful Web Services

The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition) (Addison-Wesley Professional Ruby Series)

The Rails Way (Addison-Wesley Professional Ruby Series)

Design Patterns in Ruby (Addison-Wesley Professional Ruby Series)

Ruby for Rails: Ruby Techniques for Rails Developers

Prototype and Scriptaculous in Action [Ajax]

Advanced Rails Recipes

Developing Facebook Platform Applications with Rails (Pragmatic Programmers)

Agile Web Development with Rails, 2nd Edition

Thursday, January 22, 2009

'Daemon' by Daniel Suarez

Daemon, by Daniel Suarez. This looks like a really fun read, going to grab it! Check it out.

Friday, January 16, 2009

'carl_spackler' about to get ORM-ified

...yup, it's time... no more mysql gem... Should have done this earlier, but no better time than the present to implement...

...this weekend...going to convert any [current 'carl_spackler'] database queries that are mysql-specific, into ActiveRecord calls. This way, someone is just one adapter change away from using their database, any db they want that ActiveRecord supports, with Spackler. ...but really, it will make things easy for me to write to my db using the ActiveRecord syntactical sugar. Laziness... a virtue!

ActiveRecord::Base.establish_connection({
:adapter => "sqlite",
:dbfile => "db/mygolfdb.sqlite"
})

Sunday, January 11, 2009

normalizing up 3 part names -- initial stake in ground

...all tests passing... ...collecting all 2008 PGATour data, and more Euro data now...

There are ZERO orphans in the 2008 PGATour data right now. Have collected each and every player's data for 36 tournaments in 2008. Including any other 3 part names.

The Player class is not in its ultimate form, but it is there and it splits names appropriately... still doesn't flatten special wacky characters and I'm not using any Bayesian techniques yet, but takes care of the 3 part names accurately: Jose Maria Olazabal, David Berganio Jr., Davis Love III, etc, etc.... also had to RegEx out of things like "Davis Love III (PB)"... the (PB) indicating the course name.

re = /\(\w{2}\)/
processed = name.gsub(re, "")
...re-scraping about 75 tournaments for PGA and Euro Tour with new names in the next 15 mins... pushed the new code to the carl_spackler GitHub repo .

CARL_SPACKLER::Player class:

class Player
SPECIALS = []
LAST_ONE_NAMES = ["Olazabal", "Jimenez", "Johnson", "Singh", "Thompson", "Wan", "Hicks"] #for names where last 1 name = lname
LAST_TWO_NAMES = ["V", "IV", "III", "II", "Jr.", "Jr", "Sr.", "Sr", "Jong", "Pelt", "Broeck"] #for names where last 2 names = lname
def initialize(scraped_full_name)
@full_name = scraped_full_name
@fname = ""
@lname = ""
self.parse_clean_name
end
attr_reader :fname, :lname, :full_name #lname may include spaces to accomodate "Berganio Jr.", "Love III", etc
def translate_crazy_name_char(special_char)
special_char.strip() #really just a stub for now
end
def flatten name
#flatten special characters to non-freakish ASCII. E.g. different than straight flatten, make é = e (not e'')
re = /\(\w{2}\)/
processed = name.gsub(re, "") #strip out course in parens E.g. Davis Love III (PB)
processed = processed.gsub(/,/, "") #get rid of commas in name
processed
end
def parse_clean_name
# take full name and break it apart based on some simple rules
# later may use Bayesian techniques
names = self.flatten(@full_name).split(" ")
if names.length == 2 #normal
@fname = flatten(names[0])
@lname = flatten(names[1])
elsif names.length == 3
# check if any parts of the scraped_full_name match with CONSTANTS
names.each do |nm|
if LAST_ONE_NAMES.include?(nm) #one of the names indicates it's a 3 part name
@lname = flatten(names[2])
@fname = flatten(names[0]) + " " + flatten(names[1])
elsif LAST_TWO_NAMES.include?(nm) #one of the names indicates it's a jr, III name
@lname = flatten(names[1]) + " " + flatten(names[2])
@fname = flatten(names[0])
else #some untrapped 3 part name that doesn't match either case
#split as if it's LAST_TWO_NAMES
@lname = flatten(names[2]) + " " + flatten(names[1])
@fname = flatten(names[0])
end
end
end
end
end
view raw player.rb hosted with ❤ by GitHub

Friday, January 02, 2009

vote for GitHub

Vote for GitHub as best bootstrapped startup in 2008. It takes 2 seconds and they are truly deserving:
http://crunchies2008.techcrunch.com/votes/?nominee_id=8&category_id=2