Post tagged: ruby

Using Paperclip for attachments

Doing something more than a toy example

This is not as straightforward as it seems.

In development, it works fine. Simply declare that the model has an attachment:

class Download < ActiveRecord::Base
        ...
        has_attached_file :attachment

and stuff will work automagically. A directory “system” will be created in your public folder and the attachments will be created in there …

Rdoc, the essentials

A short memory jogger.
= Top level title

== 2nd level / sub-title

----

A horizontal rule is at least 4 dashes

----

Text can be _italic_, *bold* or +monospaced typewriter+.

   Verbatim / literal text is indented. But look out
   if it's after a list, as they must be idented to a
   different extent

----

   * Indent your lists and use stars …

A useful .irbrc file

Tweaking irb.

For better or worse, programming in Ruby means that you will be spending a lot of time in irb. You can customise its behaviour in your .irbrc file, usually found in your home directory. The tweaks in mine were gathered from various places across the 'net:

require "rubygems"
require "wirble …

Language Wars

"What language would you recommend to introduce programming to an audience of life science students at a bachelor level?"

(Originally published on BiocodersHub)

Following several lengthy and passionate discussions in different venues on what language to use for teaching bioinformatics, I've started cutting and pasting my reply. And here it is.


You'll get a lot of different opinions on this because:

  • It's a religious issue. That is, it comes …

Can I use RMagick?

A simple question, with the answer simply laid out for you.

RMagick is necessary for a lot of Ruby packages, so the restrictions on using it are critical (and very annoying). Here are the constraints on what can be used where:

     
Ruby 1.8 RMagick 1 RMagick 1
Ruby 1.9 RMagick 2

Or to put it another way: if you …

Simple string interpolation

Safe and straightforward markup, for renaming files.

It's often useful for a script to allow the specification of how an output file is named, e.g. myscript.rb -o file.out. At the same time, it would be nice to have the output name based on the an input file name, and have easy and consistent naming …

Illogical or

'Or' or something else.

In which we consider the difference between or and || in Ruby. The secret is ... it depends. On context.

Behold:

>> foo = nil or "foo"
=> "foo"
>> bar = nil || "bar"
=> "bar"

That makes sense. But!:

def testor (&block)
   foo = block or "foo"
   bar = block || "bar"
   return foo, bar, block
end

>> testor()
=> [nil, "bar …

Ruby 1.9, keyword arguments, WTF

Amongst the many entries Ruby has in the "you have got to be kidding me" stakes, this is a doozy.

Due to Ruby's lack of explicit support for keyword arguments, it's traditional to use a quirk of its argument parsing that pushes named arguments into a hash:

def foo (a …

Parsing Excel

Some (very) quick notes on libraries for manipulating (mostly reading) Excel files in Ruby.

Parseexcel

  • This is the usual / traditional method for reading Excel
  • Doesn't work on XSLX (modern Excel)
  • There are a number of versions handing around the web, look on rubygems for latest version
  • Appears to not call …

Creating a Hobo project

A step-by-step guide to setting up Hobo development.

Hobo is a nifty thing, a better Rails than Rails, but there are a number of small roadbumps to getting a project started. This is a list of one possible way through the maze.

Assumptions

  • You're working in a Unix-like environment
  • For development purposes, you will be building a sandboxed …

Drawing sequence logos

A very simple script to do a simple but tedious task.

Sequence logos are a common way of representing SNPs and diversity in groups of sequences. This script automates the task. It's a bit rough around the edges and serves mainly as a base for further hacking.

Usage is:

drawlogo.rb [options] FILE1 [FILE2 ...]

where options are:

-h, --help Display this …

Reducing a sequence to SNPs

A script for a simple task.

A largely self-explanatory script. This will "shrink" an alignment, deleting all sites that don't contain a polymorphism in some member sequence. A little bit of script candy as well, this takes any number of files and saves the results in a new file named according to a definable schema:

#!/usr …

Rubyvis

Some problems, some solutions.

If you need to draw a custom graph type in Ruby, there's a few problems:

  1. There's only a modest number of graphing libraries
  2. You don't have to go to far for your graph type to not be covered by these

Which leaves you few choices. You could manually draw it …

Diagnostic printf

A better basic debugging tool.

The crude "lets drop a print statement in" approach keeps being useful even with advanced dynamic languages. Here's a (slightly) improved version for ruby that pretty prints any number of passed objects and (importantly) where the print call was made from:

def dbg (*args)
   print("dbg: #{caller()[0]}: ")
   args.each …