2008/01/20

biorhythm.rb

感謝して ruby-1.9.0-0 をダウンロードしました。(2007年末)Mac OS X 10.5 でのmake からのインストールは成功しました。Rubyのおかげで趣味プログラミングは楽しいと味わわせてもらっています。Ruby を作っている方々ありがとうございます。

ruby-1.9.0-0/sample/のなかに biorhythm.rb がありました。名前にひかれ読んでみたけど何をやっているのかはすぐわからないので、おもむろに

$ /usr/local/bin/ruby biorhythm.rb
Your birthday (YYYYMMDD):

と聞かれたので適当な日付を入力してみたら


 Your birthday (YYYYMMDD): 2008/1/1
 >>> Biorhythm <<<
 The birthday 2008.01.01 is a Tue
 Age in days: [50]

                     P=physical, E=emotional, M=mental
              -------------------------+-------------------------
                      Bad Condition    |    Good Condition
              -------------------------+-------------------------
 2008.02.21 : ..E...............M......|.......................P.
 2008.02.22 : .....E........M..........|........................P
 2008.02.23 : .........EM..............|.......................P.
 2008.02.24 : ......M.......E..........|...................P.....
 2008.02.25 : ...M...............E.....|...............P.........
 2008.02.26 : .M.......................E.........P...............
 2008.02.27 : M........................|..P..E...................
 2008.02.28 : M.....................P..|..........E..............
 2008.02.29 : .M.............P.........|...............E.........
 2008.03.01 : ..M......P...............|...................E.....
             -------------------------+-------------------------


情緒、体力、精神力のグラフが。すばらしい。

$ /usr/local/bin/ruby biorhythm.rb -help
ヘルプをみながら、

Usage: biorhythm [options]
    -b, --birthday=DATE              specify your birthday
        --date=DATE                  specify date to show
    -g, --show-graph                 show graph (default)
    -v, --show-values                show values
        --days=DAYS                  graph range (only in effect for graph)
    -h, --help                       show this message


遊んでみた。

biorhythm.rb -b 20070101 と入力することで誕生日を指定できて、
biorhythm.rb -b 20070101 --date='20080101' グラフの日付を指定できる。どちらの日付も未来の日付をも指定できた。
biorhythm.rb -b 20070101 -v  をつけると数字のみになる。


こんどは

p ag = p.to_i/365


の一行を追加してみたら年齢が出た!おもしろ。> まて、365でいいのか?よくない感じがする。date 標準ライブラリで計算してもらうべきか。

--imported_from
http://www.midore.net/daybook/2008/01/1200758465.html

2007/12/22

iTunes-playlist-output.xml

iTunesplaylistを選択-iTunesメニュー-ファイル-書き出し-Unicodeテキスト
を選択した結果のテキストどれも全く気に入らないので、XML書き出したのを整形。

# 自分で作成した playlist の曲をよみやすいテキストに書き出す.一歩手前
# playlist.rb

require 'rexml/document'

class BaseXML

  attr_accessor :doc

  def initialize(path=nil)
    @doc = readxml(path) unless path.nil?
  end

  def readxml(path)
    return nil unless FileTest.exist?(path)
    return REXML::Document.new(IO.read(path)) unless path.nil?
  end

  def key?(xpath)
    return true unless @doc.root.elements[xpath].nil?
    return false
  end

  def lookup(xpath)
    return nil unless key?(xpath)
    if @doc.root.get_elements(xpath).size == 1
      @doc.root.elements[xpath]
    else
      @doc.root.get_elements(xpath)
    end
  end

end

def one(song)
  h = Hash.new
  begin
    song.each{|x| h[x.text] ||= x.next_element.text}
  rescue
  end
  trackID = h['Track ID']
  @playlist[trackID] = [h['Name'], " / ", h['Album'], " -", h['Artist']]
end

@playlist = Hash.new
xml = BaseXML.new('output.xml')
list = xml.lookup('dict/dict/dict').each{|k| }
list.each{|x|
  song = x.to_a.select{|i| i.class.to_s =~ /REXML::Element/}
  one(song)
}

iid = xml.lookup('dict/array/dict/array/dict/integer')
iid.select{|x| x.class.to_s =~ /REXML::Element/}
iid.each{|x|
  print @playlist[x.text]
  print "\n"
}
exit

% /usr/local/bin/ruby playlist.rb > output.txt


+++ memo +++

iTunes-ファイル-書き出し が書き出す XML の特徴。

document root からたどって <dict><dict> の配下にある <dict> 要素におさめられているのが一曲分。
一曲分の要素は <key>or<integer>or<string> のいずれかで構成されている。

 <dict>
 <key>Track ID</key><integer>2314</integer>
 <key>Name</key><string>イエロー・マジック・カーニバル</string>
 <key>Artist</key><string>Van Dyke Parks</string>
 <key>Composer</key><string>細野晴臣</string>
 <key>Album</key><string>細野晴臣トリビュート・アルバム Tribute to Haruomi Hosono [Disc 1]</string>
 <key>Genre</key><string>ポップ</string>
 <key>Kind</key><string>AAC オーディオファイル</string>
 ...
 </dict>


例えば、doc.root.elements['dict/dict/dict/Artist'] としても ”Van Dyke Parks” は得られないから
h['Artist'] で呼び出しができるように一旦ハッシュにした。

xpath='dict/array/dict/array/dict/integer'には曲固有のID番号がふってある。path='dict/array/dict/array/dict/integer'の'integer' 要素の順番が曲順であって
xpath='dict/dict/dict'の順番とは無関係。

--imported_from
http://www.midore.net/daybook/2007/12/1198249205.html

2007/12/18

Leopard-Ruby

Security Update 2007-009 に Ruby update が含まれていた

Security Update 2007-009

% ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]


--imported_from
http://www.midore.net/daybook/2007/12/1197975605.html