略語を調べるスクリプトstand4を書きました

こんにちは。

みなさんはプログラミングをするときに変数名, メソッド名を省略することがあると思います。
source -> src
distance -> dist
といった具合にです。

上の例は, よく使われるものですが, 自分で略語を考える場面も多々あるはずです。
その略語が的を射たものかどうか判定するときに, Acronym Finderなどの短縮語検索サイトが便利です。
ただ, いちいちブラウザに入力して確認するのは面倒・・・ということで, Rubyのnokogiriライブラリの勉強がてら, 略語を調べるスクリプトを書きました。

#!/usr/bin/env ruby

require 'nokogiri'
require 'open-uri'

if __FILE__ == $0
    query_word = ARGV[0].upcase;

    if query_word == ''
        exit
    end

    uri = 'http://www.acronymfinder.com/'+query_word+'.html';
    n = Nokogiri::HTML(open uri)

    list_results = n.xpath('//table[@id="ListResults"]');

    unless list_results.empty?
        puts query_word+' stands for ...'
        puts list_results.xpath('./tr')[2].xpath('.//td')[2].text
        puts list_results.xpath('./tr')[3].xpath('.//td')[2].text
        puts list_results.xpath('./tr')[4].xpath('.//td')[2].text
    end
end

このスクリプトを書く上で学んだこと

  • nokogiriライブラリの使い方
  • XPathの書き方
  • Rubyのちょっとした文法