Wednesday, June 29, 2011

Case switch statements

Thanks to my boss, I learned something I'd forgotten (more evidence for the wisdom of this blog) about the case statement. Here's my code:
case arr_of_structs.first[attribute]
when Float, Fixnum
out.puts "@attribute #{attribute} NUMERIC"
when String
out.puts "@attribute #{attribute} STRING"
when Array
out.puts "@attribute charges relational"
2.0.step(12, 0.5).each do |ph|
out.puts "@attribute charge_at_#{ph} NUMERIC"
end #ph.each do
out.puts "@end charges"
end #case
end # each do

This was failing because I had previously been comparing the .class of the attribute value to the string versions of each class name. This wasn't working, and that becomes clear when I was reminded that case uses the === operator for comparison. I don't really understand all the ramifications, but it is kinda explained here: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html

Tuesday, June 28, 2011

Local gem referenced in Gemfile

When using the ubiquitous 'bundler' gem, you place references to the gems required for a package in the Gemfile which is used to load and automatically ensure the version control and requiring of libraries proceeds smoothly.  In actuality, it almost takes away your pain.  The one feature I wish it had is a parser that adds the version you are using and the gems you are calling and could add them to the Gemfile itself.  That would be a slick feature.

Anyway, the question I have this morning is how to reference a locally generated/installed gem.  Since most of the bundler functionality relies upon querying online repositories of gems like git, rubygems, ...

The solution is relatively simple.  You provide a path which contains a valid .gemspec.  Or, you can also explicitly specify the version of a .gem file for which to look.  http://gembundler.com/man/gemfile.5.html

Here is what I used:
gem 'isoelectric_calc_and_hist', '= 0.0.1',
:path => "/home/ryanmt/Dropbox/coding/isoelectric_calc_and_hist/"

Introduction

Well, I've been inspired by my graduate advisor, who has a blog that he uses to share little helpful snippets of code and instruction on how to solve problems he has solved.  So, I figured, for nothing more than my own reference, that I would write up the occasional explanation, knowing that I tend to forget things I've once known, and that this should let me solidify my own understanding and help when I forget things.  So, I imagine pretty much every other post will be code/computer/ruby related.