Upgrade to Pro — share decks privately, control downloads, hide ads and more …

October 2011 - Plain Ol' Ruby, Part 1

dallasruby
October 07, 2011

October 2011 - Plain Ol' Ruby, Part 1

Barrett Clark begins a mini series on using Ruby without external frameworks. This session focuses on Test::Unit and touches on the Ruby load path, __FILE__, and single file execution.

Source available at: https://github.com/barrettclark/DallasRubyPOR

Barrett can be found on Twitter at http://twitter.com/barrettclark

dallasruby

October 07, 2011
Tweet

More Decks by dallasruby

Other Decks in Programming

Transcript

  1. WHAT’S SO GREAT ABOUT ME? I WORK AT GEOFORCE BEEN

    DOING RUBY FOR 4ISH YEARS I HAVE A YELLOW LAB GITHUB: BARRETTCLARK TWITTERS: @BARRETTCLARK Friday, October 7, 2011
  2. class Example1 attr_reader :text def initialize @text = 'Oh Hai'

    end end require 'test/unit' require File.expand_path(File.dirname(__FILE__), 'example1') class Example1Test < Test::Unit::TestCase def test_initialize example = Example1.new assert_equal 'Oh Hai', example.text end end 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips Friday, October 7, 2011
  3. SOME TRICKS INCLUDE VS COMMAND LINE SHEBANG VS EXPLICITLY EXECUTE

    VIA RUBY __FILE__ == $0 “(IRB)” == “IRB” “EXAMPLE1.RB” == “EXAMPLE1.RB” Friday, October 7, 2011
  4. require 'csv' require File.join(File.dirname(__FILE__), 'zipcode') class FileReader def self.read_zipcodes filename

    = File.join(File.dirname(__FILE__), 'zipcode.csv') options = { :headers => true } CSV.foreach(filename, options) do |row| next if row.size != row.headers.size attributes = Hash.new row.headers.each { |field| attributes[field.to_sym] = row[field] } zipcode = ZipCode.new(attributes) end end end # Run from the command line if __FILE__ == $0 FileReader.read_zipcodes end Friday, October 7, 2011