Hatena::ブログ(Diary)

nullpo.printStackTrace();

2012-02-11

Firefoxのトラックパッドジェスチャをカスタマイズ

Apple信者歴18年のぬるぽんです。こんにちは。

MacBook Air使ってるのにマジックトラックパッドを買いました。やー、寒いんでこたつから手を出したくないんですよねw こたつの中ではマウス操作しにくいですが、トラックパッドなら使えるのですおww

さて、Mac版Firefoxでは、スワイプジェスチャに履歴を戻る/進む、という動作が割り当てられています。しかし正直ブラウザの履歴移動なんてあまり使わないので、スワイプにタブ移動を割り当ててみた。

調べてみたところ、 about:config でジェスチャの動作を変更できるようだ。

browser.gesture.swipe.left     Browser:PrevTab
browser.gesture.swipe.right    Browser:NextTab

それから、タブを簡単に閉じたかったのでピンチインにクローズを割り当ててみた。

browser.gesture.pinch.in       cmd_close

割り当て可能な動作はこの辺から探してください

http://forums.macrumors.com/showthread.php?t=737074

2012-02-04

MacのlaunchdでRedmineを自動起動

以前、passengerスタンドアローン版を使って自分のMacでRedmineを自動起動するようにしたのですが、rvmをアップデート(1.10.2)したら起動しなくなったでござる!調べた結果1.8.0〜1.10.2ではダメぽでした。

launchd では以下のコマンドを実行している。

/Users/nullpon/.rvm/bin/rvm use 1.8.7-p352@redmine -S passenger start -e production -p 3001

これをシェルで実行すると

Error: unknown action 'ruby'

と表示されて終了ふざけんな。Githubのrvmのコードみて直そうと思ったけど、よく分からん…。

というわけでplistの方を修正する。rvmコマンドによるruby環境の切り替えをせずに、直接 ~/.rvm/bin/ の下にあるrubyを使ってpassengerを起動する方針。

自分の環境ではruby-1.8.7-p352@redmineをRedmineの起動に使っている。その場合のrubyコマンドは

~/.rvm/bin/ruby-1.8.7-p352@redmine

またpassengerコマンドは

~/.rvm/gems/ruby-1.8.7-p352@redmine/bin/passenger

これらをplistにフルパスで指定しよう。~/Library/LaunchAgent/jp.paulownia.redmine.plist を以下のように修正する。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>jp.paulownia.redmine</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/Users/nullpon/Documents/redmine</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/nullpon/.rvm/bin/ruby-1.8.7-p352@redmine</string>
        <string>/Users/nullpon/.rvm/gems/ruby-1.8.7-p352@redmine/bin/passenger</string>
        <string>start</string>
        <string>-e</string>
        <string>production</string>
        <string>-p</string>
        <string>3001</string>
    </array>
    <key>StandardOutPath</key>
    <string>/dev/null</string>
    <key>StandardErrorPath</key>
    <string>/dev/null</string>
</dict>
</plist>

plistをloadしなおす

$ launchctl unload ~/Library/LaunchAgent/jp.paulownia.redmine.plist
$ launchctl load ~/Library/LaunchAgent/jp.paulownia.redmine.plist

2012-01-29

Gemfile.lockってバージョン管理すべき?

Gemを作ってるなら管理すべきでない、アプリを作ってるなら管理すべき、だそうです。

When developing a gem, use the gemspec method in your Gemfile to avoid duplication. In general, a gem’s Gemfile should contain the Rubygems source and a single gemspec line. Do not check your Gemfile.lock into version control, since it enforces precision that does not exist in the gem command, which is used to install gems in practice. Even if the precision could be enforced, you wouldn’t want it, since it would prevent people from using your library with versions of its dependencies that are different from the ones you used to develop the gem.

When developing an app, check in your Gemfile.lock, since you will use the bundler tool across all machines, and the precision enforced by bundler is extremely desirable for applications.

Clarifying the Roles of the .gemspec and Gemfile « Katz Got Your Tongue?

2012-01-27

他の言語に慣れた人がRubyを使ったときにつまずきがちな点

あと4つは募集中です

他の言語に慣れた人がRubyを使ったときにつまずきがちな9つのポイント - 西尾泰和のはてなダイアリー

他の言語というかJavaしか知らなかった昔の自分がつまづいたような気がする点を4つ

メソッド名がJavaと違いすぎる

慣れるしかない…

Java

"hoge   ".trim();

Ruby

"hoge   ".strip

ダメなうらわざ、やったら負け

class String
  alias :trim :strip
end

"   hoge   ".trim

null/nilに対するメソッド呼び出し

JavaならNullPointerExceptionとなりますが、RubyではNoMethodError。Rubyはnilもオブジェクトなのでヌルポは無いのです。

エラーメッセージにfor nil:NilClassとあれば、メソッド未定義ではなく、変数の未初期化かもしれないのでそっちも調べよう。

NoMethodError: undefined method `hoge' for nil:NilClass

forループ?

無いです。timesを使いましょ

5.times { |e|
   puts e
}

for in構文はあるけど

for i in (0..4) 
  puts i
end

Rubyならeachですよね。

(0..4).each { |e| 
   puts e
}

eachやmapのようなEnumerableのブロック付きメソッドを使わないなら、Rubyを使う意味があまり無いと思う。

{}とdo endってどっちを使うのがいいの?何が違うの?

好きな方を使えばいいと思うよ

ただし、優先度が違うので以下のようにすると解釈が変わる

def hoge a
  puts a
  if block_given?
    yield
  else
    3
  end
end

hoge hoge(1) { 2 }
puts "--"
hoge hoge(1) do 2 end

結果

1
2
--
1
3

以下のように解釈されるようだ。

hoge(hoge(1) { 2 })  

hoge(hoge(1)) do 2 end 
2004 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2011 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2012 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |