<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' version='2.0'><channel><atom:id>tag:blogger.com,1999:blog-20640549</atom:id><lastBuildDate>Mon, 28 Jan 2008 05:39:31 +0000</lastBuildDate><title>CodeBlog</title><description/><link>http://pitecan.com/codeblog/</link><managingEditor>増井</managingEditor><generator>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-7898181045274810498</guid><pubDate>Sat, 19 Jan 2008 08:16:00 +0000</pubDate><atom:updated>2008-01-19T17:19:25.933+09:00</atom:updated><title>RubyCocoaでスクリーンセーバを作る</title><description>MacOSでは
&lt;a href="http://developer.apple.com/documentation/UserExperience/Reference/ScreenSaver/Classes/ScreenSaverView_Class/Reference/Reference.html"&gt;ScreenSaverView&lt;/a&gt;
クラスを実装して.saverという名前のbundleに格納すればスクリーンセーバになる。
RubyCocoaでスクリーンセーバを書く方法を
&lt;a href="http://pitecan.com/RubySaver/"&gt;こちら&lt;/a&gt;に解説しておいた。</description><link>http://pitecan.com/codeblog/2008/01/rubycocoa_19.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-6884804747755072626</guid><pubDate>Sun, 13 Jan 2008 08:33:00 +0000</pubDate><atom:updated>2008-01-13T17:33:41.838+09:00</atom:updated><title>自前のCプログラムをRubyCocoaから呼ぶ</title><description>Objective-CのメソッドをRubyCocoaから呼ぶのはとても簡単である。
&lt;br&gt;
πを計算する以下のようなクラスが存在するとき、

&lt;pre class="code"&gt;
#import &amp;lt;Cocoa/Cocoa.h&gt;

@interface Pi : NSObject
- (double)calc:(id)param;
@end

@implementation Pi
- (double)calc:(id)param
{
  // すごい方法でπを計算
  return 3.14159265358979;
}
@end
&lt;/pre&gt;

Xcodeを使ってこれからPi.frameworkを作成し、
/Library/Frameworksとかに置いておけば
以下のように簡単にRubyから呼び出すことができる。

&lt;pre class="code"&gt;
require 'osx/cocoa'
include OSX

NSBundle.bundleWithPath("/Library/Frameworks/Pi.framework").load
ns_import :Pi

class Pi &lt; NSObject
  addRubyMethod_withType('calc', 'v@:@')
end

pi = Pi.alloc.init
puts pi.calc(nil)
&lt;/pre&gt;

自前のCプログラムをすぐにRubyから使えるので便利である。</description><link>http://pitecan.com/codeblog/2008/01/crubycocoa.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-4287164821092554402</guid><pubDate>Tue, 08 Jan 2008 05:57:00 +0000</pubDate><atom:updated>2008-01-13T17:28:05.966+09:00</atom:updated><title>10桁ISBNのチェックディジットの計算</title><description>&lt;pre class="code"&gt;
def add_checksum(isbn)
  v = 0
  (0..8).each { |i|
    v += isbn[i,1].to_i * (i+1)
  }
  v %= 11
  checksum = (v == 10 ? 'X' : v.to_s)
  isbn + checksum
end
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2008/01/10isbn.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-8510629262692438595</guid><pubDate>Mon, 07 Jan 2008 00:16:00 +0000</pubDate><atom:updated>2008-01-13T17:29:06.253+09:00</atom:updated><title>AppleRemoteをRubyで使う</title><description>Tim Burks氏の
&lt;a href="http://rubycocoa.com/appleremote"&gt;Use your Apple Remote from Ruby&lt;/a&gt;というサイトに
AppleRemote(最近のMacに付属してくる赤外線リモコン)をRubyで使う方法が載っている。
Martin Kahr氏が作った
&lt;a href="http://www.martinkahr.com/2007/07/26/remote-control-wrapper-20/"&gt;AppleRemote用ライブラリ&lt;/a&gt;を
framework化してビルドしてRubyから呼べばよいらしい。

&lt;p&gt;
現在(2008/1)のKahr氏のシステムは、
ソースコードの構成やライブラリの引数がBurks氏の記事とは異なっているが、
必要そうなものを全部まとめてframework化して以下のようにコードを少し変えると
無事動かすことができた。

&lt;pre class="code"&gt;
require 'osx/cocoa'
include OSX

NSBundle.bundleWithPath("/Library/Frameworks/AppleRemote.framework").load
ns_import :AppleRemote

class AppleRemoteDelegate &lt; NSObject
  addRubyMethod_withType('sendRemoteButtonEvent:pressedDown:remoteControl:', 'v@:ii@')

  def sendRemoteButtonEvent_pressedDown_remoteControl(buttonIdentifier, pressDown, remoteControl)
    puts "button #{buttonIdentifier}, pressed #{pressDown}"
  end
end


appleRemoteDelegate = AppleRemoteDelegate.alloc.init
appleRemote = AppleRemote.alloc.initWithDelegate(appleRemoteDelegate)
appleRemote.startListening self

NSApplication.sharedApplication.run
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2008/01/appleremoteruby.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-6196047358056404898</guid><pubDate>Sun, 06 Jan 2008 20:39:00 +0000</pubDate><atom:updated>2008-01-13T17:29:49.760+09:00</atom:updated><title>Leopard上でのOpenGL/GLUT</title><description>&lt;a href="http://pitecan.com/UnixMagazine/PDF/if9908.pdf"&gt;昔のUnixMagazine記事&lt;/a&gt;で
OpenGLによるクロスプラットフォーム開発の話を書いたのだがMacは含まれていなかった。
OSXにはOpenGLとGLUTがFrameworkとして標準装備されているので、
以下のようにリンクするだけで簡単にビルドすることができる。

&lt;pre class="code"&gt;
-framework GLUT -framework OpenGL
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2008/01/leopardopenglglut_07.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-3022566625714498459</guid><pubDate>Tue, 25 Dec 2007 23:06:00 +0000</pubDate><atom:updated>2008-01-13T17:30:25.498+09:00</atom:updated><title>RubyOSA</title><description>&lt;a href="http://rubyosa.rubyforge.org/"&gt;RubyOSA&lt;/a&gt;
を使うとRubyからAppleScriptを操作できる。
&lt;br&gt;
例えば以下のスクリプトでiTunesの中の曲をリストできるので、
Rubyから検索したり自動再生したりできる。

&lt;pre class="code"&gt;
require 'rubygems'
require 'rbosa'

OSA::app('iTunes').sources[0].library_playlists[0].tracks.each { |s|
  puts "#{s.name} / #{s.album} / #{s.artist}"
}
&lt;/pre&gt;

この方式だとAppleScript臭が皆無になるのが嬉しいかも。</description><link>http://pitecan.com/codeblog/2007/12/rubyosa.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-4468730806606504526</guid><pubDate>Sat, 22 Dec 2007 17:33:00 +0000</pubDate><atom:updated>2008-01-13T17:31:03.827+09:00</atom:updated><title>RubyでMacの辞書アプリケーションを呼ぶ</title><description>アプリケーションのサービスを利用するとMacのアプリケーションの機能を
Rubyから呼び出すことができる。
例えば辞書アプリを以下のように呼び出すことができて便利。

&lt;pre class="code"&gt;
require 'osx/cocoa'

p = OSX::NSPasteboard.generalPasteboard                                                                                                                                       
p.declareTypes_owner([OSX::NSStringPboardType],self)                                                                                                                          
p.setString_forType("単語",OSX::NSStringPboardType)                                                                                                                           

OSX::NSPerformService("Look Up in Dictionary", p)
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2007/12/rubymac.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-7555802468227168339</guid><pubDate>Sat, 22 Dec 2007 17:29:00 +0000</pubDate><atom:updated>2008-01-13T17:31:56.578+09:00</atom:updated><title>iTunesをRubyで制御する</title><description>RubyCocoa経由でAppleScriptを呼ぶことによって
RubyからiTunesなどを制御できるようだ。
屋上屋を重ねる感じだが、とりあえず仕方がないのかも。

&lt;pre class="code"&gt;
require 'osx/cocoa'

script = OSX::NSAppleScript.alloc.initWithSource(%{
tell application "iTunes"                                                                                                                                                                                   
  play                                                                                                                                                                                                      
end tell                                                                                                                                                                                                    
})
errinfo = OSX::OCObject.new
result = script.executeAndReturnError(errinfo)
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2007/12/itunesruby.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-117028089377412913</guid><pubDate>Wed, 31 Jan 2007 21:55:00 +0000</pubDate><atom:updated>2007-02-01T07:01:33.806+09:00</atom:updated><title>Greasemonkeyで外部スクリプトを呼ぶ</title><description>Greasemonkeyの中でprototype.jsなど外部のライブラリを呼ぶには以下のようにすればいいらしい。
&lt;ul&gt;
&lt;li&gt;&lt;a href='http://d.hatena.ne.jp/ysano2005/20060127/1138382734'&gt;CMS Researcher氏&lt;/a&gt;
&lt;li&gt;&lt;a href='http://rektunpe.sakura.ne.jp/diary/?date=20070107'&gt;たかはしまさゆ記&lt;/a&gt;
&lt;/ul&gt;
面倒だけれども使える。</description><link>http://pitecan.com/codeblog/2007/02/greasemonkey.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-115543786352430222</guid><pubDate>Sun, 13 Aug 2006 02:56:00 +0000</pubDate><atom:updated>2006-08-13T11:57:43.536+09:00</atom:updated><title>Rubilicious</title><description>del.icio.us APIは仕様が変わったようなので古いRubiliciousが動かなくなってしまったようだ。
&lt;a href="http://masao.jpn.org/d/2006-08.html#2006-08-11"&gt;まさお氏のパッチ&lt;/a&gt;で直るようだ。</description><link>http://pitecan.com/codeblog/2006/08/rubilicious.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-114618866540184022</guid><pubDate>Fri, 28 Apr 2006 01:42:00 +0000</pubDate><atom:updated>2006-04-28T10:44:25.410+09:00</atom:updated><title>Rubilicious</title><description>&lt;a href="http://pablotron.org/software/rubilicious/"&gt;Rubilicious&lt;/a&gt;から
&lt;a href="http://del.icio.us/masui"&gt;del.icio.us&lt;/a&gt;へのアクセスがエラーになるのでいろいろ調べていたら
勝手に直ってしまった。
&lt;br&gt;
&lt;a href="http://del.icio.us/masui"&gt;del.icio.us&lt;/a&gt;にしばらくアクセスしないとエラーになるんだろうか?</description><link>http://pitecan.com/codeblog/2006/04/rubilicious.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-114035882855854877</guid><pubDate>Sun, 19 Feb 2006 14:17:00 +0000</pubDate><atom:updated>2006-02-19T23:20:28.570+09:00</atom:updated><title>SQLite</title><description>&lt;a href="http://hondana.org/"&gt;本棚.org&lt;/a&gt;がどうにも遅いのでSQLiteという簡易RDBを試してみたところ、
ファイルを利用してデータベースを構築するよりはかなり高速に処理できることがわかった。
SQLiteでは全データがひとつのファイルとして扱われるのだが
本棚.org全データで30MBぐらいになる。意外と小さい。
毎日バックアップをとっても困らないかもしれない。
&lt;br&gt;
...というわけで完全移行しようと思う。いつごろできるやら。</description><link>http://pitecan.com/codeblog/2006/02/sqlite.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113929944691944304</guid><pubDate>Tue, 07 Feb 2006 08:02:00 +0000</pubDate><atom:updated>2006-02-07T17:04:06.926+09:00</atom:updated><title>TCP通信のログを見る</title><description>田中氏作のdebug-socket.rbを利用すると、
TCP通信のログを見ることができる。
&lt;pre class='code'&gt;
require 'debug-socket'
require "xmlrpc/client"

server = XMLRPC::Client.new("b.hatena.ne.jp", "/xmlrpc", 80)
ok, param = server.call2("bookmark.getCount","http://d.hatena.ne.jp/")

param.each { |key,val|
  puts key
  puts val
}
&lt;/pre&gt;
などとするとXMLRPCのログが/tmpに残る。</description><link>http://pitecan.com/codeblog/2006/02/tcp.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113720913740338607</guid><pubDate>Sat, 14 Jan 2006 03:24:00 +0000</pubDate><atom:updated>2006-01-29T11:00:24.680+09:00</atom:updated><title>Emacsに色をつける</title><description>&lt;a href="http://pitecan.com/codeblog/images/coloremacs.jpg" border=none&gt;&lt;img src="http://pitecan.com/codeblog/images/coloremacs.jpg" width="234"&gt;&lt;/a&gt;
インデントに応じて色を変えると便利かと思ったのだが、
こういうことをするためには
&lt;a href="http://www.fan.gr.jp/%7Ering/doc/elisp-manual/elisp_513.html#SEC514"&gt;文字ごとに属性を設定しなければならないらしい&lt;/a&gt;。
面倒なのでやる気がうせたが一応作ってみた。
&lt;pre class="code"&gt;
(setq indent-colors
     '(
       "DarkSeaGreen1"
       "DarkSeaGreen2"
       "DarkSeaGreen3"
       "DarkSeaGreen4"
       "SeaGreen1"
       "SeaGreen2"
       "SeaGreen3"
       "SeaGreen4"
       "PaleGreen1"
       "PaleGreen2"
       "PaleGreen3"
       ))

(defun set-indent-color ()
 (let (symbol face indent)
   (setq indent (% (indent-level) (length indent-colors)))
   (setq symbol (make-symbol (concat "indent" (format "%d" indent))))
   (setq face (make-face symbol))
   (set-face-foreground face "black")
   (set-face-background face (indent-color indent))
   (save-excursion
     (let (start end)
       (beginning-of-line)
       (setq start (point))
       (end-of-line)
       (setq end (point))
       (if (looking-at "\n") (setq end (1+ (point))))
       (put-text-property start end 'face face)
       ))
   ))

(defun set-indent-colors ()
 (save-excursion
   (let (old new (done nil))
     (goto-char 1)
     (setq old (point))
     (while (not done)
       (set-indent-color)
       (forward-line)
       (setq new (point))
       (setq done (= old new))
       (setq old new)
       )
   )))

(defun indent-color (indent)
 (let (c)
   (setq c (nth indent indent-colors))
   (if (null c)
       (setq c "white"))
   c
   ))

(defun indent-level ()
 (save-excursion
   (progn
     (beginning-of-line)
     (re-search-forward "[^ \t]" nil t)
     (backward-char)
     (current-column)
     )))
&lt;/pre&gt;</description><link>http://pitecan.com/codeblog/2006/01/emacs.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113694855254515837</guid><pubDate>Wed, 11 Jan 2006 03:02:00 +0000</pubDate><atom:updated>2006-01-18T23:27:12.820+09:00</atom:updated><title>Windows/Mac上のRubyスクリプトをDrag&amp;Dropで動かす</title><description>WindowsでもMacintoshでも、
ファイルをアイコンにDrag&amp;DropすることによってRubyスクリプトを起動することができる。
&lt;ul&gt;
&lt;li&gt;Windowsの場合:
&lt;p&gt;
xxxx.batというバッチファイルを作り、以下のようにRubyスクリプトを記述する。
&lt;pre class="code"&gt;
\cygwin\usr\local\bin\ruby -S -x %0 %1 %2
exit
#!ruby
file = ARGV[0]
(...普通のRubyスクリプト)
&lt;/pre&gt;
&lt;li&gt;Macintoshの場合:
&lt;p&gt;
AppleScriptを使う。
スクリプトエディタで以下のようなスクリプトを作成し、
「アプリケーション形式」で保存するとxxxx.appのような名前のアプリケーション
(ドロップレット)ができ、Drag&amp;DropしたファイルがfileListに渡されるので、
これらを連結して引数としてxxxx.rbに渡す。

&lt;pre class="code"&gt;
on open (fileList)
  set args to ""
  repeat with i in fileList
    set arg to POSIX path of i
    set args to args &amp; " " &amp; arg
  end repeat
  do shell script "ruby xxxx.rb " &amp; args
end open 
&lt;/pre&gt;
&lt;/ul&gt;</description><link>http://pitecan.com/codeblog/2006/01/windowsmacrubydragdrop.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113677123559131056</guid><pubDate>Mon, 09 Jan 2006 01:46:00 +0000</pubDate><atom:updated>2006-01-18T23:06:57.690+09:00</atom:updated><title>RubyでExifライブラリ(libexif)を使う</title><description>RubyからExifファイル(デジカメのJPEG拡張形式)を扱うlibexif-rubyのインストールメモ。(Cygwin)
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://sourceforge.net/projects/libexif"&gt;SourceForge&lt;/a&gt;から
&lt;a href="http://prdownloads.sourceforge.net/libexif/libexif-0.6.13.tar.gz?download"&gt;libexif-0.6.13&lt;/a&gt;
を入手したがConfigureに失敗するので
&lt;a href="http://prdownloads.sourceforge.net/libexif/libexif-0.6.12.tar.gz?download"&gt;libexif-0.6.12&lt;/a&gt;
を入手してインストール。
&lt;li&gt;libexif-rubyはオリジナルのものがうまく使えないので
&lt;a href="http://packages.qa.debian.org/libe/libexif-ruby.html"&gt;Debian&lt;/a&gt;の
&lt;a href="http://ftp.debian.org/debian/pool/main/libe/libexif-ruby/libexif-ruby_0.1.2.orig.tar.gz"&gt;libexif-ruby_0.1.2.orig.tar.gz&lt;/a&gt;
に
&lt;a href="http://ftp.debian.org/debian/pool/main/libe/libexif-ruby/libexif-ruby_0.1.2-7.diff.gz"&gt;libexif-ruby_0.1.2-7.diff.gz&lt;/a&gt;
のパッチをあててruby extconf.rb; makeするとうまくいく。
&lt;/ul&gt;</description><link>http://pitecan.com/codeblog/2006/01/rubyexiflibexif.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113671186426323734</guid><pubDate>Sun, 08 Jan 2006 09:12:00 +0000</pubDate><atom:updated>2006-01-09T08:49:47.643+09:00</atom:updated><title>JavaScriptで認意のサイトと通信する方法</title><description>Ajaxで使われる
XMLHttpRequestでは別サイトと通信を行なうことはできないのだが、
scriptタグを使うと認意のサイトと通信を行なうことができることをmala先生に教えてもらった。
(&lt;a href="http://la.ma.la/misc/js/jsan.html"&gt;mala氏のコード&lt;/a&gt;)


以下のようなコードで動的にscript要素を生成し、
JavaScriptプログラムを吐くCGIを呼び出す。

&lt;pre class="code"&gt;
var s=document.createElement("script");
s.charset="UTF-8";
s.type = "text/javascript";
s.src="http://pitecan.com/xxx.cgi";
document.body.appendChild(s)
&lt;/pre&gt;
たとえばxxx.cgiが
&lt;pre class="code"&gt;
a = 100;
&lt;/pre&gt;
というテキストを返す場合、
このコードが非同期的に実行されてaに100が代入される。
(appendChildの実行終了後すぐにこのコードが実行されるわけではないことに注意が必要である)
&lt;p&gt;
しかしscriptタグでこういうことができるのならば
普通のAjaxで使うXMLHttpRequestは不要ということなのだろうか?
不思議。&lt;/p&gt;</description><link>http://pitecan.com/codeblog/2006/01/javascript.html</link><author>増井</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-20640549.post-113660418257559507</guid><pubDate>Sat, 07 Jan 2006 03:22:00 +0000</pubDate><atom:updated>2006-01-07T12:23:02.583+09:00</atom:updated><title>CodeBlog</title><description>g新部氏のプロジェクトに参加してコードブログを書く。
RubyとかJavaScriptとかのプログラムを読んでみよう。</description><link>http://pitecan.com/codeblog/2006/01/codeblog.html</link><author>増井</author></item></channel></rss>