( ꒪⌓꒪) ゆるよろ日記

( ゚∀゚)o彡°オパーイ!オパーイ! ( ;゚皿゚)ノシΣ フィンギィィーーッ!!!

self.send(pred)がtrueならばselfを、そうでないならnilを返すメソッド

何をいっているのかというと、こういうことです


class Object
  def filter(&pred)
    (yield self) ? self : nil
  end
end

書いてみた。

foo".filter(&:present?) # => "foo"
"".filter(&:present?)   # =>  nil


ようは、 str.present? ? str : other みたいなやつを書きやすくするためのものです

str = "hoge" 
str.filter(&:present?) || "fuga" # => "hoge"

str = "" 
str.filter(&:present?) || "fuga" # => "fuga"

よい名前が思い浮かばなかった(´・ω・`)

8/29追記

ActiveSupportにObject#presenceというのがあるそうだ。present?だけならこれで充分。自分は、任意のlambdaを渡したい場合があるのでこれも無駄にはならない、はず。


8/29さらに追記

というか、ずいぶん前に自分で既に書いてgemにしてあったし俺は一体何をやっているんだ……。
おそらく上位存在からの記憶操作が行われた可能性がある……!!

yuroyoro/lambda_driver · GitHub

git-ignoreというコマンドを書いた話


ちょろっと書けそうだったので書いた。

yuroyoro/git-ignore · GitHub

Demo

Installation

PATH通った場所においてくれ

curl -sL https://raw.githubusercontent.com/yuroyoro/git-ignore/master/git-ignore > ~/bin/git-ignore

Examples

`git ignore add "pattern"`で、.gitignoreへ追加する。

$ git ignore add '*.log'


.gitignoreから削除するには、`git ignore remove "pattern"`を実行する。

$ git ignore remove '*.log'


add/removeには複数のパターンを同時に渡すことができる。

$ git ignore add '*.log' '*.bak'


`git ignore list`で定義されているパターンを出力。

$ git ignore list


`--global` オプションを使うことで、グローバルな `.gitignore` (`$HOME/.gitignore`)に対してadd/removeすることも可能。
以下のコマンドで、`*.class` を `$HOME/.gitignore` へ追加する。

git ignore --global add "*.class"


`git ignore pull ` コマンドでは、github/gitignoreから引数の言語に応じた.gitignoreファイルを取得して、
存在していないパターンを追加することが可能。
以下の例では、Haskell.gitignoreGithubからダウンロードする。

$ git ignore pull Haskell


引数なしで`git ignore pull`を実行すると、引数に指定可能な一覧を確認することができる。

$ git ignore pull

rspecの--tagオプションを利用して任意のコマンドライン引数をspec側に渡すという邪悪なhack

今まで、rspecコマンドでは任意の引数を渡すことはできなかったので、環境変数経由で引き渡すという方法をとっていた。
( ;゚皿゚)ノシΣ フィンギィィーーッ!!!

MY_OPT1=true rspec spec/my_spec.rb

環境変数で渡すのはダルいのでなんとかしたいと思い、`--tag`オプション経由で値を渡すというダーティなhackを書いた。

仕掛けは、helper.rbなどで、以下のように`Rspec.world.filter_manager.incusions`から引数を切り出してクラス変数に保持しておくようなアレをホゲる。

helper.rb

class MyOptions
  class << self
    OPTION_KEYS = [:my_opt1, :my_opt2]
    attr_accessor :options
    def parse(world)
      @options = world.filter_manager.inclusions.slice(*OPTION_KEYS)
    end
  end
end

RSpec.configure do |config|

  # filterをonに
  config.filter_run :focus => true
  config.run_all_when_everything_filtered = true

  # --tagから独自の引数を切り出して保持しておく
  MyOptions.parse(RSpec.world)
end

あとは、`MyOptions.options[:my_opt1]`のように参照できる。

my_options_spec.rb

require File.join(File.dirname(__FILE__), 'helper')

describe "MyOptions" do
  subject { MyOptions.options }

  it { should include(:my_opt1) }
  it { should include(:my_opt2) }
end


実行すると、`--tag`経由で引数が渡っていることが確認できる。

$ rspec spec/my_options_spec.rb -t my_opt1 -t my_opt2
Run options: include {:focus=>true, :my_opt1=>true, :my_opt2=>true}

All examples were filtered out; ignoring {:focus=>true, :my_opt1=>true, :my_opt2=>true}

MyOptions
  should include :my_opt1
  should include :my_opt2

Top 2 slowest examples (0.0029 seconds, 100.0% of total time):
  MyOptions should include :my_opt1
    0.00205 seconds ./spec/my_options_spec.rb:6
  MyOptions should include :my_opt2
    0.00085 seconds ./spec/my_options_spec.rb:7

Finished in 0.0034 seconds
2 examples, 0 failures


`--tag`渡さないとfailする。

$ rspec spec/my_options_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

MyOptions
  should include :my_opt1 (FAILED - 1)
  should include :my_opt2 (FAILED - 2)

Failures:

  1) MyOptions should include :my_opt1
     Failure/Error: it { should include(:my_opt1) }
       expected {} to include :my_opt1
       Diff:
       @@ -1,2 +1 @@
       -[:my_opt1]

     # ./spec/my_options_spec.rb:6:in `block (2 levels) in <top (required)>'

  2) MyOptions should include :my_opt2
     Failure/Error: it { should include(:my_opt2) }
       expected {} to include :my_opt2
       Diff:
       @@ -1,2 +1 @@
       -[:my_opt2]

     # ./spec/my_options_spec.rb:7:in `block (2 levels) in <top (required)>'

Top 2 slowest examples (0.00381 seconds, 100.0% of total time):
  MyOptions should include :my_opt1
    0.00282 seconds ./spec/my_options_spec.rb:6
  MyOptions should include :my_opt2
    0.00099 seconds ./spec/my_options_spec.rb:7

Finished in 0.00432 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/my_options_spec.rb:6 # MyOptions should include :my_opt1
rspec ./spec/my_options_spec.rb:7 # MyOptions should include :my_opt2

golang勉強会で「cgoやってみた」という話をしてきた

Go lang勉強会 - connpassで発表してきた。
今までにblogに書いたcgoの話。

以下が資料とサンプルコード。

久しぶりに人前で発表した気がする。
当日はdemoもやるとなると時間が足りなくなるだろうという予想通りになり、ブランクを感じた。
あと、ネタ成分が不足気味だったのでリハビリしなければならない。

他の人の発表も面白かった。特にライセンスの話など。皆さんお疲れ様でした。

こんな勉強会なら、また参加したいものです。

ぼくのかんがえたさいきょうのGit Repository Browser: Gitterb をRuby2.1.2/Rails4にupgradeしてDockerImage作った話

3年ほど前に、GitterbというGitリポジトリのコミットログを可視化するツールを作った。


このアプリケーションはRuby1.9/Rails3.2 で書かれていて、今となってはもう動かないので、Ruby2.1/Rails4へupgradeした。

デモサイトはこちら http://gitterb.yuroyoro.net/


依存しているGritというRubyからGitリポジトリをホゲるGemがRuby2系では動かないので、libgit2のRubyバインディングであるRuggedに移行している。

あと、せっかくなのでCentOS7で動くDockerImageを作った。Docker Hubにおいてある。

以下のようにdocker pullした後にrunすると、port3000でサンプルが起動する。

docker pull yuroyoro/gitterb
docker run -d -p 3000:3000 -t yuroyoro/gitterb


macでboot2dcoker使ってる人は、port forwardingしてくだされ。

ssh -N -L 3000:127.0.0.1:3000 docker@localhost -p 2022

( ꒪⌓꒪) あばばばばばばばば

ʕ  ゚皿゚ ʔ GolangのASTを可視化するツールを作った

はじめてのGo Runtime。
ということで、GoのAST(抽象構文木)を可視化するツールを書いた。

yuroyoro/goast-viewer · GitHub

goast.yuroyoro.net にデモがある。

go/astパッケージを使うと、GoのソースコードからAST(抽象構文木)を得ることができる。
あとはこれをAngulerJSとか使ってみて可視化してみただけ。

f:id:yuroyoro:20140630220303p:plain

ソースコードをアップロードするか、入力してparseボタンを押すと、右側にASTが展開される。マウスオーバーするとASTのnodeに該当するコードが選択状態になる。

以下の手順でインストールできるます

$ go get -d github.com/yuroyoro/goast-viewer
$ cd $GOPATH/src/github.com/yuroyoro/goast-viewer
$ make install

GoよりAngulerJSの方が難しかったʕ  ゚皿゚ ʔ