Archive for September, 2008

Charming ruby with curses

September 12, 2008

The other day I did something good. I fished out my Programming Ruby book and skimmed through it. I found many interesting things there, and i was drawn to this sample curses program. I downloaded it – a paddle that moves up and down.
In about 2 hours, I had a snake program up and running. [...]

Modifying choice (command-line parser)

September 8, 2008

I liked choice so much, I didn’t want to leave it (I need unparsed args). So I went back to the source code. And then came back to my timeconverter. I realized I could clean up ARGV based on Choice.choices. This worked.

1 module Choice
2 def get_unparsed_args(args)
3 flat=Choice.choices.to_a.flatten
4 [...]

Command-line option parsing experiences with clip

September 8, 2008

I went through the one examples provided with clip . Looked very promising and simple. Until I applied it to my timeconverter program. Quick observations:
1. in the example “host” should be “server” (typo).
2. If using ruby -w, there are too many uninitialized variable warnings
3. Can’t reuse “-h”, yet on typing “-h” on prompt gives an [...]

Rewriting shell-scripts in ruby

September 8, 2008

Just a note to myself (before I start some real work), that I’ve gotta rewrite some of my most used shell scripts in ruby, and write future ones in ruby. Reasons for frustration with bash scripting:
- if and loop syntax gets me
- unpredictable behavior of unix commands due to differences between gnu, macport and bsd [...]

Ruby command-line parsing (update)

September 8, 2008

The issues with optiflag and choice: regarding determining unparsed arguments seems to have been posted in their respective forums a year or 2 back. But no response from authors. Are these 2 projects abandoned ?
I dipped into the source code of choice (parser.rb) to see if i could figure out how to extract the args, [...]

Command-line parsing: choice and optiflag

September 6, 2008

For some reason, I decided to go for choice, for a tiny time converter (DST) program I had put together. choice has a nice easy way of setting up the options configuration. Easy to remember, no syntax. It has the ability to filter and validate the args. Being a newbie, however I did waste an [...]

Command-line parsing: choosing a ruby library

September 5, 2008

I started with getoptlong, but lately decided to try something that would make life simpler. I need a parser that gives me options for:
mandatory flags
optional flags
switches
I used OptionParser for the ruby csplit program. The code of OptionParser is probably longer than the program [...]

Ruby time conversion with daylight saving

September 4, 2008

I had earlier posted that ruby was not taking DST into account when creating a date with EST as the timezone.
Finally got something working thanks to tzinfo. Needs to be installed separately.

1 #!/usr/bin/env ruby -w
2 # input may be given as "2:00 PM" or "14:00"
3 # http://tzinfo.rubyforge.org/doc/files/README.html
4 require ‘rubygems’
5 [...]

ruby one-liners as random signatures for blog posts and emails

September 4, 2008

The random sigs appearing below are due to some simple features of Vim (and bash).
1. Create sig files by splitting a tips/oneliners file. I found a ruby1liner file on the internet and split it based on ‘^#’. The file names are now ruby.1, ruby.2 etc. A tgz of these is here.
You can split the file [...]

Quick command-line syntax highlighting (coderay)

September 3, 2008

1 #!/usr/bin/env ruby
2 # courtesy: Helder
3 # http://obvio171.wordpress.com/2007/06/03/colorful-ruby-code-for-your-blog/
4 # modified to output to stdout so can be used as a filter
5 # 2008-09-03 23:22
6 require ‘rubygems’
7 require ‘coderay’
8
9 if ARGV.length != 1
10 puts "Wrong number of arguments. Use: codecolor.rb <source_file>"
11 [...]