日常

ケ・セラ・セラ

Rails5 で alias_method_chain が deprecated

短くまとめてみる試み。

rails5 で alias_method_chain が deprecated となる。Module#prepend を使っていくことになる。

また、ruby 2.1 で Module#include と Module#prepend が public method になったよね。

こんな風に

# In ruby 2.0
Person.send(:prepend, Flanderizer)

# In ruby 2.1
Person.prepend(Flanderizer)

form submit ボタンにリンクっぽくfont awesomeを使う

メモです。

例えばこんな、fontawesome のハートマークをクリックしたら submit して likes/create or likes/destroy したいような状況ですが、

      <%= form_for(current_user.likes.build,
        html: { class: "form-inline" }) do |f| %>
        <div><%= hidden_field_tag :article_id, article.id %></div>
        <%= button_tag type: 'submit', class: "button-as_link" do %>
          <i class="fa fa-heart-o fa-lg c-like"></i>
        <% end %>
      <% end %>

font awesome を使いたくて、button をリンクのような見た目にしたかったのです。

こんな風に css を書いてそれっぽくなりました。(クラス名はちょっとひどいので後で整理しないといけない認識あります。。)

.button-as_link {
  background: none;
  border: none;
  padding: 0;
}

.form-inline {
  display: inline-block;
}

HerokuでRubyのバージョンを指定する

heroku に deploy したアプリがこけていて、なんだ?と思ったらぼっちオペレーターを使ったからだった。そういえば ruby のバージョン指定を忘れて 2.2.4 が使われていた。

やり方は、Gemfile に書けばよい。

source "https://rubygems.org"
ruby "2.3.1" # for heroku

現在の ruby のバージョンは以下のように見られる。

heroku run 'ruby -e "puts RUBY_VERSION"'

bundle install(with native extensions)に失敗する(Text file busy)

bundle install をすると、ことごとく with native extensions な gem が失敗するという状況に陥って困っていた。

こんなエラーメッセージが出ていて、

Text file busy @ unlink_internal - ./siteconf20160808-20166-29ij1u.rb

自分は virtual box の shared folder に指定したディレクトリ下で作業していたのだけど、どうやらそれが原因だったらしい。以下が参考になる。shared folder を避けて bundle install し直したら成功した。

最近の rails3.2 のはなし

ruby 2.3 から加わった Hash#to_proc がある。

この影響で、rails 3-2-stable ではこのような修正が merge された。これを含む 3.2系のリリースはまだされていない。

Associations do not call .to_proc on Hash #25043

4-2-stable にも master にもこのような変更が含まれていないのは、

そもそもこういった書き方は deprecated になっていて、

has_many :content_blocks, through: :page_blocks, autosave: true, source: :block, conditions: { page_blocks: { location: 'content' } }

こう書くようになっているから。という理解。

has_many :content_blocks, through: :page_blocks, autosave: true, source: :block, conditions: ->(*) { { page_blocks: { location: 'content' } } }

ここでまさに言っていたっぽい。

https://github.com/rails/rails/pull/23082

ひとこと加えておくと、好きで 3.2 を使い続けているわけではない。

ruby 2つのファイルを一行ずつ交互にマージしたい

ひとつめのファイル samplefile1

1
2
3
4
5
6
7
8
9

ふたつめのファイル samplefile2

one
two
three
four
five
six
seven
eight
nine
ten
eleven

とあった時に、こんな結果がほしい。という時。

1
one
2
two
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
ten
eleven

こうしました。

今回は samplefile1.size < samplefile2.size な時に、samplefile2 の文字列だけ後ろに出力しておきたかったので、a2.zip(a1).map(&:reverse).flatten.compact のようにしました。

f1, f2 = %w(samplefile1 samplefile2)
a1 = File.readlines(f1)
a2 = File.readlines(f2)

a = if a1.size < a2.size
  a2.zip(a1).map(&:reverse).flatten.compact
else
  a1.zip(a2).flatten
end

File.open("outfile", "w") do |f|
  a.each {|s| f.write(s) }
end

もっと良いなにかがあれば知りたい。