Pragmatic Development Notes

Software development stuff

RStreamTuner in Action

Friend of mine criticised me why I didn’t put some screenshots on project page. Since I couldn’t find a way to do it there I’ve decided to put some screenshots here, so here they are.

RStreamTuner - New Streaming Directories Browser

I like to listen various Internet radio stations while I work. My favourite application was streamtuner mostly because it had support for SHOUTcast. Unluckily new SHOUTcast design cannot be handled by streamtuner and future development on this application is stopped. In other words there is a little chance for streamtuner to be upgraded so I’ve decided to make my own streaming directories browser – RStreamTuner (Ruby Stream Tuner).

As usual developing in Ruby was real pleasure and only doubt was what to use for GUI part. I’ve started with Ruby/Tk and left it very quickly due to very poor documentation (and probably lack of Tcl knowledge). I had to find replacement and my primary goal was to find library that works equally well on Linux and Windows. WxWidgets with WxRuby gem was my final choice. Although RStreamTuner is still in early development phase it is quite usable. At the moment it supports SHOUTcast and Xiph directories, but I plan to add more as soon as I find good one. Good candidate could be Live365 but they’ve made their pages quite hard to parse so I’ll probably leave it for later.

Once I reach beta version, making support for new streaming directories will be quite easy and will require implementation of just a few methods. As a matter of act it is already so simple but I’m not satisfied with the code and will change it once I find a little bit more time. Code is quite rough (I had little time to make RStreamTuner and was eager to get working version as soon as possible). Besides I want to write detailed instructions how to add support for new directories. Anyway if you like to listen Internet radion stations and like Ruby clone RStreamTuner. Of course if you have suggestions do not hasitate to drop me a note and if you make support for new streaming directory just send me a pull request and I’ll try to grab it as soon as possible.

Still on CVS? Too Bad!

At the and of May 2009 Savannah disks crashed. You can read more about it here. This is something no one can predict. You never know when your hardware will let you down, and there is no cure for that. Disks will be replaced, system reinstalled, backups restored and that’s it. Or is it?

Not completely if you are using CVS or SVN and there were some commits after the last backup. Emacs developers spent few days in the discussion to determine what commits they lost.

Luckily they had Git mirror of CVS repository but what surprises me is they didn’t move to Git yet. If they were using Git they would have been able to restore complete repository in a few minutes (maybe longer for large repositories). Since they already have Git mirror for Emacs code I really do not understand why do they not switch to Git completely. Having full history at each developer’s computer is a huge benefit. Not to mention that Git gives you possibilities you can only dream of in CVS and SVN.

Splitting Ruby Array

Recently I had to process arrays of data with more than thousand of members but requirement was not to process them one by one. Instead I had to split array into chunks and to process each of sub-arrays separately. I’ve found one definition of chunk method within comments on this post and I’m including it here. This method splits array in given number of sub-arrays and I needed a version that will split array in the sub-arrays that have predefined size (ok, I know I could simply calculate a number of chunks by dividing total number of elements by required number of elements, but I just wanted one method that’ll do all that for me). So I wrote chunk_max_num method. If maximum number of elements per array is zero or negative it will return original array and for all other cases it will return array which has arrays of given size as members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'enumerator'

class Array
  def chunk_max_num(max_num)
    return slice(0...length) unless max_num > 0
    quot, mod = length.divmod(max_num)
    quot += 1 if mod > 0
    (0..quot).map {|i| i*max_num}.enum_cons(2).map {|a,b| slice(a...b)}
  end

  def chunk(pieces)
    q,r = length.divmod(pieces)
    (0..pieces).map {|i| i * q + [r,i].min}.enum_cons(2).map {|a,b| slice(a...b)}
  end
end

Easy Emacs

First step

In my previous articles I wrote a lot about Emacs, its customization and adjusting it for Ruby and Ruby on Rails development. Although these articles give detailed instructions how to set everything up, following all steps are somewhat cumbersome and require quite a lot of work.

Fortunately there is a solution. Just clone Emacs starter kit from Github and you will be able to develop Ruby and RoR applications in Emacs in just a few simple steps. Emacs starter kit uses Elpa packaging and is very easy to configure. But let’s start from the beginning.

Clone Emacs starter kit with Git, to your empty .emacs.d directory. Be sure that init.el file is in .emacs.d folder. If you do not use Git (do you realize what you are missing?) you can get archived sources if you press download button on Emacs starter kit page on Github. Unpack archive to the .emacs.d and be sure that init.el is in .emacs.d folder.

Emacs starter kit has some packages that are still not in Elpa but you’ll have to install additional packages required for Ruby and RoR. Let’s do it now:

1
M-x package-list-packages

New buffer with list of available packages will open and you can select those you want to install. If you plan to use Emacs for Ruby and RoR you’ll definitely need Rinari. It will install all dependent packages.

There are few other that might come in handy like css-mode, javascript or yasnippet but you can always add them later. If you install yasnippet you should probably need yasnippets-rails, but more about that later. Installing packages is very easy. Just place cursor on the package line and press ‘i’. When you are finished with selection press ‘x’ and packages will be installed. At any time you can press ‘h’ to get quick help for Elpa packaging system. In order to uninstall package you have to press ‘d’ (and ‘x’ after that).

Further customization

When you are done with packages installation just restart Emacs and you are ready to continue work in your favorite language – Ruby and framework – Ruby on Rails :) But what if you want to adjust some settings or keep some of those you set according to my previous articles? Luckily that is easy to do, too.

First create sub-directory in .emacs.d directory with your user name on the system you are using. Put your .el files there and Emacs starter kit will load them automatically during Emacs start up. If you have changed color theme and you’ve used theme from color-theme library put color-theme-library.el in this folder and add, for example, line

1
(color-theme-deep-blue)

to any of .el files in this folder. I keep all my additional settings in customization.el file.

I already mentioned yasnippet and yasnippets-rails packages. Simplest way to install yasnippet library is from Elpa system following above mentioned procedure. Unfortunately yasnippets-rails cannot be installed that way so you should clone it from the Github:

1
git clone git://github.com/eschulte/yasnippets-rails.git

Directory yasnippets-rails should be in the directory with your user name under .emacs.d. Add following lines to your customization.el file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(add-to-list ’load-path\
 (concat dotfiles-dir “/<user-name>/yasnippets-rails”))

(add-hook ‘ruby-mode-hook ; or rails-minor-mode-hook ?
(lambda ()
 (make-variable-buffer-local ’yas/trigger-key)
 (setq yas/trigger-key [tab])))

(require ’yasnippet)
(add-to-list ’yas/extra-mode-hooks
 ’ruby-mode-hook)

(yas/initialize)
(setq yas/window-system-popup-function ’yas/x-popup-menu-for-template)
(yas/load-directory (concat dotfiles-dir
“/<user-name>/yasnippet/snippets”))

(yas/load-directory
 (concat
 dotfiles-dir “/<user-name>/yasnippets-rails/rails-snippets/”))

(make-variable-buffer-local ’yas/trigger-key)

and you will be ready for using new snippets during development.

Final word

My previous articles were based on Rails reloaded package for RoR development in Emacs. It is good library and if you like fancy GUI things you can still use it. There is a new version on Github.

Still, I would recommend trying Rinari that is a package from Elpa. It doesn’t have any GUI features but once your fingers “learn” all shortcuts your RoR development will be much easier and faster.