The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for the ‘Uncategorized’ Category

rbcurse continues as “canis”

leave a comment »

rbcurse will only have bug-fixes in it. rbcurse has a tutorial on github, too.

The code has been taken over by Canis (on github and available as a gem). Canis has removed a lot of stuff, and changed things too, 

Please move onto Canis if you are interested. Canis is NOT backward compatible with rbcurse.

 

Advertisements

Written by totalrecall

August 30, 2014 at 11:09 pm

Posted in Uncategorized

Ruby command-line parsing (update)

with 5 comments

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, but after trying out variations on the command line, and print statements, it does not look like an easy task – not for me at least (a newbie). The author is a big-shot and busy (and very cool looking), so I guess he won’t be reading my mail for some time (http://www.youtube.com/watch?v=-Reg0yhJw4s !).

The comments on this page led me to some excellent options such as:
Thor: by whycat’s (yehuda katz). It’s a great tool, maps commands on the CL to ruby methods. It is executed like rake: thor prog options. Something definitely to use in the near future.
clip: This one does state in its help that it allows the remainder args to be accessed. It seems to simplest of all.
– I once again looked at trollop. It does not do any validations, and I am not sure whether the remainder args are accessible or not. What i can see is that I would have to have a separate switch case for all the options.
– I once again looked at Commandline. It begins to look more appealing after the issues I have faced with the others.
– I gave a much closer look at cmdparse : looks great for commands and subcommands as in rake and webgen (its written by the webgen author who is very active and responds immediately). Could not figure out whether it will give me the remainder or not.

My next step will probably to give clip and trollop a try. Along the way, I discovered interesting things such as SproutCore (a ruby javascript framework), gambit, elif (a backward file reader), SimpleConsole, python’s cly, read up more on JSON …

As can be seen from my sig on git, I looked at the tutorials of several revision control systems such as bazaar, mercurial, git and subversion and finally decided to try out git after all. Have been using rcs until now.


I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git.
— Linus Torvalds

# print file except for blank lines
$ cat | ruby -pe ‘next if $_ =~ /^\s*$/’


HTML.vim,
mtsend.py

Written by totalrecall

September 8, 2008 at 12:29 pm

Posted in Uncategorized

Command-line parsing: choice and optiflag

leave a comment »

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 hour trying to access the param inside the action block using Choice.choices[:param] which was not yet set up. Finally, it dawned on me to use value as in:
action do |value|
as in:

  option :countries do
    long '--countries=[PATTERN]'
    desc 'List all countries, or those matching a pattern'
    action do |value|
      if value == true
        puts TZInfo::Country.all.sort
      else
        puts TZInfo::Country.all.sort.find_all{|item| item.name =~ /#{value}/;  }
      end
      exit
    end
  end

My bad!
Later I got stuck for another hour trying to figure out why my program ran fine when the user entered no timezone (and the default was used), but gave wrong results when he entered it.
Finally I printed ARGV[0] and discovered that choice does not consume ARGV as it parses. I searched the docs, examples etc but there was no mention of how to access unused params.
The command is executed as:


./timeconverter.rb --from="America/New_York" "12:00 PM"

The “–from” is optional and defaults to America/New_York. The “12:00 pm” is not an option. When I used OptionParser, “12:00 PM” would be the only item left on ARGV.

So I thought about shifting ARGV myself. However, I do not know when the user enters “-f ” or “–from=” or the default is invoked. Each would result in “12:00 PM” being in a different slot on ARGV.

A bit rattled by this, I installed Optiflag and played with the samples. Optiflag, too does not shift ARGV, although it does maintain a hash called ARGV.flags and I could remove the flags and values from ARGV so it leaves me with “12:00 PM”. That said, optiflag does print the help very well. If i have 3 mandatory flags, and I enter only one, it only prints out the 2 I have not entered, not the entire options list.

At this stage, I did give yet another visual look at CommandLine and also at GetOpt::Declare — which just blows my mind away, but its not for this lifetime. Perhaps, I’ll just go back to OptionParser or even getoptslong. Commandline parsing should not take me more time that my program itself. I sent a mail to chris of choice.rubyforge.org, waiting for an answer.


# convert Unix newlines (LF) to DOS format (CR/LF)
# – strip newline regardless; re-print with dos EOL
$ cat | ruby -ne ‘BEGIN{$\=”\r\n”}; print $_.chomp’

Written by totalrecall

September 6, 2008 at 9:30 am

Posted in Uncategorized

Javascript: the good and bad

leave a comment »

Just wrote my first program in javascript (see previous post). Things I liked about javascript:
1. C-like syntax – thankfully!
2. Good regexp classes.
3. Decent handling of arrays, strings, etc. (Although, NO PRINTF !!!)

Problems I faced:

1. While executing, Firefox does NOTHING if there is an error in the file. No error message! It takes me ages to figure out each missing “;” or typo.
2. I tried looking for verifiers, and found several. None have a compile procedure along with the code. Could get none to install on my command line. JSLint comes closest in giving an online textarea which will validate.
But JSlint.com expects me to remove all HTML first. Then it complains about each “==” and insists on my using “===”. It also complains incorrectly about my using the Array(a,b,c) constructor. I have surprisingly not been able to find any utility to help me in finding errors.

3. Vim does NOT indent the program due to the “.html” extension — I have to rename the file to “.js”, indent and then rename back to “.html”.

If anyone has solutions to the above, would be grateful to hear from you.

Mozilla’s current and prior versions use a format called MORK to structure the history data. It has been charmingly referred to by former Netscape engineer Jamie Zawinski as “…the single most braindamaged file format that I have ever seen in my nineteen year career.”

Written by totalrecall

August 26, 2008 at 11:00 pm

Posted in Uncategorized

Comparing some static HTML generators

with 2 comments

I have started looking at some static html website generators. Will be writing more about them in separate posts. I am not looking to have to know or do too much with CSS or too much customization. I would like it to use sensible defaults, conventions and leave me to just fill data. I like filters like textile, markdown and maruku. It should look good straight off. Links on the side or below would be great.

I first started with zenspider.com’s ZenWeb (written in ruby). The sparse directions for usage gave me many errors. I spent hours figuring them out. I still cannot get it to correctly make a site map of a subfolder from the main (due to bugs). Very basic filter. Not sure if it;s abandoned.

Then downloaded Webby – this thing began downloading half the internet ! and crashed out while running Rdocs on haml and builder.

So then i downloaded webgen (ruby gem) which looked better based on documentation. This does not run – gives an error about not finding console.
An error has occurred: uninitialized constant Console
That’s the issue with these uncompiled languages — so much time wasted due to uninitialized variables that only give errors when the program tried to execute the erroneous line.

Webby surprisingly runs and I will write more on that again.

Then also downloaded qsgen (a python egg), but not sure what all it does, and some samples of the format would have helped. To look at later.

Then i tried downloading HTMLGen – finally found it in macports – that first downloaded a lower version of python (2.4) (I have 2.5.1), and then finally said it can’t find htmlgen!

Edit: uninstalled latest webgen and installed an older stable version
gem install webgen -v '< 0.5.0'
This things rocks! Much more mature than webby.

[Edited in Vim using HTML.vim]
Published using mtsend.py by Scott Yang.

Written by totalrecall

August 24, 2008 at 7:09 pm

Posted in Uncategorized

What is wrong with man pages and –help

leave a comment »

Today most programs have at least 20 command line options, usually much more. A newcomer wishes to run the basic or minimal functionality of a tool/utility without as yet getting into complex usage. However, the “–help” and man pages of ALL programs do not give basic usage.

All options, frequently used ones, as well as rarely used ones, are all clubbed together alphabetically. A new user has to read all these 50 or so options to figure out how to use the program in its most basic way. What are the minimal use cases for a program/tool? What are the values of some of the arguments to be passed?

The mini HOW-TO from gnupgp.org should take me through a cycle, a use case, but I am given all the options. So what is recipient – an email id, a user on my system. A HOW-TO is not a help file or reference manual — it should show me how to do certain operations or cases.
Anyway, back to more playing around.

[Edited in Vim using HTML.vim]
Published using mtsend.py by Scott Yang.

Written by totalrecall

August 24, 2008 at 10:57 am

Posted in Uncategorized

Mac OS X OpenPGP with Alpine

with one comment

As usual, poured through many many difficult how-to’s for getting PGP on my system. Then gave up and just did the usual “sudo port -v install gnupgp“. A few minutes later, I was creating my keys.

Now that makes me really wonder about some program called Mac GPG Guard which showed a lot of programs I needed to download. It also pointed me to an install process which had some Mac Classic screens (which scared me off). What all is it doing? What did I miss ? Now (looking back) I realize that the classic screens are for people importing keys from the classic OS (I am new so I did not get that). Also, all these files to download are GUI programs to make your life easier. Not necessary. I followed the key generation procedure on that page (fairly standard).

Again many pages of how to integrate Alpine with pgp, including something called Topal (not for OS X). Some configuration did not work – untested stuff. But this one worked like a charm – http://spevack.org/PineGPG — he provides a tiny script (ez-pine-gpg) to run which generates the exact lines to be added to your .pinerc.

That’s all I had to do to install and integrate Openpgp and alpine. It was very simple if you follow the above.

Will I actually be using it ? I wonder — I don’t know anyone who has a public key I could encrypt with, or would verigy my signature. Most people I know probably use a web client for mail, so they would have to copy the mail to a local file and then decrypt. I am not sure how people use this on a regular basis. Alpine wants me to enter my passphrase every time I wish to sign a message or decrypt a message. Certainly not something i would like to do for every mail.

References: PineGPG

[Edited in Vim using HTML.vim]

Published using mtsend.py by Scott Yang.

Written by totalrecall

August 24, 2008 at 9:48 am

Posted in Uncategorized