ruby

A book auto-renewer for UNI's library

A while ago I wrote an auto renewer for UNI's library. I thought I would share that code in this post. It's not the best code I've ever written, but it works, and it's much better then trying to remember to do it manually.

So, without further ado, get it.

It's pretty simple to run, basically you just do

  1. ruby rod_library_renewer.rb last_name UNI_id_including_000_in_the_front

The default action as demo'd above is to renew all books. I set this on up on my system using cron to run every day.

This will almost certainly fail if someone reserves one of the books you have..but I don't know how the web interface reports this, so my script doesn't catch it. So. Be aware!

Data-izing arbitrary attributes, the lazy way

For the Cedar Falls Poker website, there's a stats page in which a particular model is hit pretty hard. There used to be a series of methods handling the retrieval of data in a useful manner, but I standardized the way this happens so I could clean up the model code and make it more useful.

This uses Hashes to return data in a format that is easy to turn into pie charts or bar graphs. This code assumes a few things. It assumes you do not have any other methods with _data in the name. It assumes that you're only going to be pulling data that is available as a method or attribute call; namely, using send. If your classes aren't set up this way, they probably should be.

  1. class << self
  2. def data(atr)
  3. returning(Hash.new(0)) do |h|

Using YAML as a parameter hash in Ruby

Sometimes while coding Ruby applications the need develops to have some sort of metadata available as a configuration file. I have written some Ruby code that allows parameters that exist in a yaml file to be used as though they are in the class itself. In addition, I required code that allows the class to modify these parameters and then save them back to the file. Ruby's method_missing is a major player here. Anyway, I thought I would share this code.

To give an example of what exactly this does, assume that there exists a file called 'config.yml' that contains some key value pair such as :name => 'Bob':

  1. ---
  2. :name: Bob

Using this class, we could do stuff like:

  1. require 'boilerplate'
  2.  
  3. class Test < Boilerplate
  4. def run
  5. puts "Name is: #{name}"

Syndicate content