Tag Archives: ruby

Remembering Ruby

I’ve seen some discussion over the years on how to onboard new Ruby developers.  What about re-onboarding “old” developers though?

I’ve been a Ruby developer for several years.  I remember the bump to Rails 1.0.  We used to have to hand-crank our app servers while walking uphill.  You kids don’t know how good you have it.  I digress while I shake my cane.
 
As I was saying, I’ve been in the community for a long time, but I have also been away from ruby for several months on other languages.  I consider Ruby my “home” language, though, and the Ruby community is a special community that I enjoy feeling a connection with.
 
So how do I reorient myself?  How do I stay connected with the language and the community that I am not always directly involved in?
  • Twitter
  • Local Ruby Brigade
  • Practice
  • Playing Around
Twitter
 
I follow a lot of developers on Twitter, as one does.  Sometimes I binge add people, and sometimes I cull the list.  I love the community, but I don’t love the drama.  The drama sometimes eats at me, actually.  The things that we get worked up about also eat at me, but sometimes we get a little caught up in the swirl.
 
I like seeing the evolution of the language and ideas.  I like seeing other languages come into play, too.  I don’t think Ruby is necessarily the best language solution for all problems, and I believe that it’s important to have a varied language toolkit at your disposal.
 
Local Ruby Brigade
 
User groups are a fantastic resource.  The Dallas Ruby Brigade has always been very open and nurturing in my experience.  There are monthly meetings where people give talks, and I’ve given a few over the years.  There are also weekly meetups where we code and socialize.  We also have an active mailing list where we discuss current events, upcoming meetings, and also ask and answer questions.
 
I felt safe as an inexperienced developer, and I am tremendously grateful.  A local user group is a really great place to keep your chops up and get experience talking in a group setting (whether in the front or in the audience).
 
After a couple of years I felt like it was my turn to “give back” and get more involved, which has been very fulfilling.
 
Practice
 
Certainly you can practice the wrong things, but I find practice very valuable.  Repetition helps me learn and cements ideas for me.  I also learn by doing.  I need a concept to have a practical application or else I just will not see it.  For the past several months I have done Exercism exercises in several languages.  For a while I was doing Ruby backend services, so I did Javascript exercises to keep up those chops.  Then we started doing a lot of native apps, so I focused on Ruby exercises.  I’ve also looked at a few katas here and there.  Sometimes they’re really hard and I don’t get them.  Sometimes they’re too easy and I don’t get much value out of those either.  Usually they’re just right though.
 
Scratch An Itch (Play Around)
 
I’ll occasionally try to cook up a little project to work on.  I can’t tell you how many times I’ve redone my blog.  Not each of those has seen the light of day, and that’s ok.  It’s more about the process.  I usually also have a new data model in mind, which means that the data needs to be transformed.  I love data, and this is a really fun exercise for me.  ActiveRecord is great, but it can also be pretty limiting.  Sometimes raw SQL really is the best way to express what you need to do.
 
Other
 
I also try to make it to at least one Ruby conference a year, as well as submit talks on various things.  Thinking about what I might be able to shed light on or what things of value I might be able to share helps me take a critical look at my own growth and evolution.
 
Addendum
 
I realized as I was walking around thinking about doing something in Xcode that this isn’t just Ruby for me.  I bounce around languages a lot based on the projects I work on.  One thing that is unique to Ruby, though, is the community.

Leave a Comment

Filed under programming, ruby

Rake Task To Load Production Data

When I’m working on a Rails app that relies on live data, like a reporting app for example, I find myself constantly wanting to pull live data to my local dev database. So I made a rake task to do this:

namespace :db do
  namespace :heroku do
    desc "Load the PROD database from Heroku to the local dev database"
    task :load => :environment do
      if Rails.env == 'development'
        Bundler.with_clean_env do
          config = Rails.configuration.database_configuration[Rails.env]
          system <<-CMD
            heroku pgbackups:capture
            curl -o latest.dump `heroku pgbackups:url`
            pg_restore --verbose --clean --no-acl --no-owner -h localhost \
              -U #{config["username"]} -d #{config["database"]} latest.dump
            rm -rf latest.dump
          CMD
        end
      end
    end
  end
end

3 Comments

Filed under programming, ruby

Ruby Minitest

I had the privilege of attending DCamp this past weekend. It was awesome. While doing the Game Of Life pairing sessions, one of my pair partners wanted to explore the exercise using Minitest. Neither of us had ever used it, so we moved on with RSpec and I made a note to come back to it.

I try to be discerning about bringing additional dependencies (including gems) into a project. I’ve always just used test/unit, and I prefer that syntax over the RSpec syntax. Minitest is baked into the stdlib (starting with Ruby 1.9.3), so you’ve already got everything you need. You can also write tests using either the test/unit or spec style. Win-win right? If you want more there are also additional gems that can supplement functionality. Everybody wins!!!

Here is a basic class, taken from the Minitest gem documentation.

lib/meme.rb

class Meme
  def i_can_has_cheezburger?
    "OHAI!"
  end

  def will_it_blend?
    "YES!"
  end
end

Unit tests, using Minitest:

test/test_meme.rb

require "minitest/autorun"
require_relative "../lib/meme"

class TestMeme < Minitest::Unit::TestCase
  def setup
    @meme = Meme.new
  end

  def test_that_kitty_can_eat
    assert_equal "OHAI!", @meme.i_can_has_cheezburger?
  end

  def test_that_it_will_not_blend
    refute_match /^no/i, @meme.will_it_blend?
  end

  def test_that_will_be_skipped
    skip "test this later"
  end
end

And a spec testing the same things:

spec/meme_spec.rb

require 'minitest/autorun'
require_relative "../lib/meme"

describe Meme do
  before do
    @meme = Meme.new
  end

  describe "when asked about cheeseburgers" do
    it "must respond positively" do
      @meme.i_can_has_cheezburger?.must_equal "OHAI!"
    end
  end

  describe "when asked about blending possibilities" do
    it "won't say no" do
      @meme.will_it_blend?.wont_match /^no/i
    end
  end
end

Outstanding. You can run either of these from the command line:

$ ruby spec/meme_spec.rb
Run options: –seed 25656

# Running tests:

..

Finished tests in 0.001430s, 1398.6014 tests/s, 2097.9021 assertions/s.

2 tests, 3 assertions, 0 failures, 0 errors, 0 skips

You can also use rake to run the tests, and this is the preferred method.

Rakefile

require 'rake/testtask'

Rake::TestTask.new(:spec) do |t|
  t.warning    = false    # this is the default -- shown as example of options
  t.verbose    = false
  t.test_files = FileList['spec/*_spec.rb']
end

Rake::TestTask.new(:unit) do |t|
  t.test_files = FileList['test/test_*.rb']
end

desc "Run ALL tests"
task :default do
  Rake::Task[:spec].invoke
  Rake::Task[:unit].invoke
end

Now you can ask rake what tasks it has for you:

$ rake -T
rake default # Run ALL tests
rake spec # Run tests for spec
rake unit # Run tests for unit

Cool. How about we just run the default rake task (ALLTHETESTS):

$ rake
Run options: –seed 28918

# Running tests:

..

Finished tests in 0.001187s, 1684.9200 tests/s, 2527.3799 assertions/s.

2 tests, 3 assertions, 0 failures, 0 errors, 0 skips
Run options: –seed 26846

# Running tests:

..S

Finished tests in 0.000936s, 3205.1282 tests/s, 3205.1282 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 1 skips

Awesome. You can also run all the tests, just a group of tests, or even just a single test file. This looks tasty enough to overtake test/unit as my go-to testing framework.

Leave a Comment

Filed under programming, ruby

iOS Map Comparison

We experiment with a lot of things at work.  Tools.  Technologies.  Workflows.  Recently we were discussing an effective way to do native iOS development with the team.  I love RubyMotion.  I think it makes it faster and easier to create a native iOS app.  There is a learning curve, though.

We are considering doing a project where the app is native, and written is XCode, but the guts of the app are just web views fed by a Rails app.  But before we could really consider that as an option, we needed to know how it would handle mapping.

So I whipped up a quick and dirty RubyMotion app to see.  I used Leaflet for the HTML version, and had the webpage be served from as a resource in the app.

Choose your adventure
Choose your adventure
« 1 of 3 »

I didn’t see anything that scared me off.  You lose the native feel, but I’m not crazy about the MMMapView and it’s annotations.  I’d much rather work with Leaflet and use markers.

So it looks promising.

Here’s a link to the project: https://github.com/barrettclark/MapCompare

Leave a Comment

Filed under iOS, programming, ruby

Ruby Attr Antipattern

Recently I’ve seen a good handful of something that I’ve never seen before in the Ruby world, and it mystifies me. I’ve been going through exercises on Exercism (http://exercism.io), and in the Ruby exercises I’ve seen several people using private attr_accessor. What?

The argument, so far, has been that it’s the same as doing it the long way so why not?

It’s weird. That’s why not. It’s not expressive. That’s why not. It’s a lazy shortcut that still didn’t buy you anything. That’s why not.

An example:

class Demo
  def initialize(something)
    @something = something
  end

  def based_on_something
    something * 2
  end

  private
  attr_reader :something
end

So, I am all for the use of attr accessors, and I am all for tucking implementation details away in private methods. The use of a private attr does nothing for the latter. You’ve simply added more obscure code that doesn’t tell future you or future maintainers anything about what you intended or thought might happen. If you think the implementation might change or evolve, then go ahead and write the extra 2 lines to define the method explicitly. Otherwise just use the ivar.

Now, it’s entirely possible that I’m just being grumpy old man and shaking my cane. If there is a good reason that’s come along that I haven’t picked up on yet I would love to understand it. For now, though, I’m maintaining that this is a weird antipattern.

2 Comments

Filed under programming, ruby

IRB Benchmark Helper Method

Quick and dirty ruby benchmarking helper that you can stick in your .irbrc:

require 'benchmark'
def benchmark(n, &block)
  Benchmark.bm do |x|
    x.report do
      n.times { block.call }
    end
  end
end

Then call it like so:

str = "2013-01-21"
benchmark(10) do
  Date.parse(str)
end
benchmark(10) { Date.strptime(str, "%Y-%m-%d") }

Simples!

Leave a Comment

Filed under programming, ruby