<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Ruby on 答えを知りたい</title><link>https://hack.ironsand.net/tags/ruby/</link><description>Recent content in Ruby on 答えを知りたい</description><generator>Hugo -- 0.147.9</generator><language>en-US</language><lastBuildDate>Fri, 21 Jun 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://hack.ironsand.net/tags/ruby/index.xml" rel="self" type="application/rss+xml"/><item><title>楽天銀行の特定の名義の入出金をrubyで合計</title><link>https://hack.ironsand.net/2024/%E6%A5%BD%E5%A4%A9%E9%8A%80%E8%A1%8C%E3%81%AE%E7%89%B9%E5%AE%9A%E3%81%AE%E5%90%8D%E7%BE%A9%E3%81%AE%E5%85%A5%E5%87%BA%E9%87%91%E3%82%92ruby%E3%81%A7%E5%90%88%E8%A8%88/</link><pubDate>Fri, 21 Jun 2024 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2024/%E6%A5%BD%E5%A4%A9%E9%8A%80%E8%A1%8C%E3%81%AE%E7%89%B9%E5%AE%9A%E3%81%AE%E5%90%8D%E7%BE%A9%E3%81%AE%E5%85%A5%E5%87%BA%E9%87%91%E3%82%92ruby%E3%81%A7%E5%90%88%E8%A8%88/</guid><description>&lt;p>楽天銀行の入出金明細は全期間を CSV でダウンロードできるので、その CSV から特定の名義についての振込を抽出したかったので書きました。&lt;/p></description></item><item><title>More Than 1 Row in Migrator Tableへの対処法</title><link>https://hack.ironsand.net/2024/more-than-1-row-in-migrator-table%E3%81%B8%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</link><pubDate>Mon, 10 Jun 2024 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2024/more-than-1-row-in-migrator-table%E3%81%B8%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</guid><description>&lt;p>最近は小さいサービスをSinatra + Sequel + Dockerの構成でやっている。
今回は&lt;code>Error: Sequel::Migrator::Error: More than 1 row in migrator table&lt;/code>というエラーが出てきてよくわからなかったのでその対処法を残しておく。&lt;/p></description></item><item><title>Pryのプロンプトに色付けする方法</title><link>https://hack.ironsand.net/2023/how-to-change-pry-prompt-color/</link><pubDate>Tue, 12 Sep 2023 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2023/how-to-change-pry-prompt-color/</guid><description>&lt;p>本当は良くないんだけど、production環境で直接&lt;code>rails console&lt;/code>を使って作業する事があるのでうっかりローカル環境と間違えないように&lt;code>pry&lt;/code>の色を変えておきたい。&lt;/p></description></item><item><title>SequelでModelをmigrateする手順</title><link>https://hack.ironsand.net/2023/how-to-write-migration-for-model-with-sequel/</link><pubDate>Tue, 07 Mar 2023 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2023/how-to-write-migration-for-model-with-sequel/</guid><description>&lt;p>Railsほど大きいフレームワークを使いたくない時に&lt;code>Sinatra&lt;/code> + &lt;code>Sequel&lt;/code>でやろうとしたら意外と躓くポイントがあったので記録として残しておきます。今回はModelを作ってmigrationする流れについて。&lt;/p></description></item><item><title>letter_openerをWSL環境で使う方法</title><link>https://hack.ironsand.net/2022/how-to-use-letter-opener-in-wsl/</link><pubDate>Thu, 27 Oct 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/how-to-use-letter-opener-in-wsl/</guid><description>&lt;p>メールのプレビューをブラウザ上で表示できるGEM &lt;code>open_letter&lt;/code>ですが、デフォルトのWSL環境のUbuntuでは&lt;code>launchy&lt;/code>がエラーを吐いて終了してしまいます。少し設定をしておけば問題なく使えるのですが、毎回その設定方法を忘れてしまうのでメモっておきます。&lt;/p></description></item><item><title>Object#present?や#blank?でinvalid Byte Sequence in UTF 8になる時の対処法</title><link>https://hack.ironsand.net/2022/present%E3%82%84blank%E3%81%A7invalid-byte-sequence-in-utf-8%E3%81%AB%E3%81%AA%E3%82%8B%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</link><pubDate>Fri, 17 Jun 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/present%E3%82%84blank%E3%81%A7invalid-byte-sequence-in-utf-8%E3%81%AB%E3%81%AA%E3%82%8B%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6%E6%B3%95/</guid><description>&lt;p>Rails で&lt;code>Object#present?&lt;/code>や&lt;code>Object#blank?&lt;/code>で&lt;code>ArgumentError: invalid byte sequence in UTF-8&lt;/code>が出て少し困ったのでその原因と対処法を残しておきます。&lt;/p></description></item><item><title>RubocopがPathnameで+演算子を使ってると誤警告を出してくる対策</title><link>https://hack.ironsand.net/2022/rubocop%E3%81%8Cpathname%E3%81%A7+%E6%BC%94%E7%AE%97%E5%AD%90%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%82%8B%E3%81%A8%E8%AA%A4%E8%AD%A6%E5%91%8A%E3%82%92%E5%87%BA%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8B%E5%AF%BE%E7%AD%96/</link><pubDate>Thu, 02 Jun 2022 15:42:35 +0900</pubDate><guid>https://hack.ironsand.net/2022/rubocop%E3%81%8Cpathname%E3%81%A7+%E6%BC%94%E7%AE%97%E5%AD%90%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%82%8B%E3%81%A8%E8%AA%A4%E8%AD%A6%E5%91%8A%E3%82%92%E5%87%BA%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8B%E5%AF%BE%E7%AD%96/</guid><description>&lt;p>Rubocopを使うと変数と文字列を結合するときに&lt;code>foo + 'bar'&lt;/code>形式で記述してると式展開を使った&lt;code>&amp;quot;#{foo}bar&amp;quot;&lt;/code>を推奨してくるのですが、&lt;code>foo&lt;/code>の場合が&lt;code>Pathname&lt;/code>クラスであっても同様の警告を出してきます。&lt;/p></description></item><item><title>RubyでのSelenium WebDriverのheadlessの指定方法</title><link>https://hack.ironsand.net/2022/ruby%E3%81%A7%E3%81%AEselenium-webdriver%E3%81%AEheadless%E3%81%AE%E6%8C%87%E5%AE%9A%E6%96%B9%E6%B3%95/</link><pubDate>Thu, 26 May 2022 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2022/ruby%E3%81%A7%E3%81%AEselenium-webdriver%E3%81%AEheadless%E3%81%AE%E6%8C%87%E5%AE%9A%E6%96%B9%E6%B3%95/</guid><description>&lt;p>&lt;code>Selenium::WebDriver.for(:chrome)&lt;/code>の headless のためのオプション設定方法がややこしくてよく忘れるのでメモっておきます。&lt;/p></description></item><item><title>Selenium::WebDriverでデフォルトで引数を渡すようにする</title><link>https://hack.ironsand.net/2021/selenium-webdriver%E3%81%A7%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E3%81%A7%E5%BC%95%E6%95%B0%E3%82%92%E6%B8%A1%E3%81%99%E3%82%88%E3%81%86%E3%81%AB%E3%81%99%E3%82%8B/</link><pubDate>Sat, 22 May 2021 13:46:35 +0900</pubDate><guid>https://hack.ironsand.net/2021/selenium-webdriver%E3%81%A7%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E3%81%A7%E5%BC%95%E6%95%B0%E3%82%92%E6%B8%A1%E3%81%99%E3%82%88%E3%81%86%E3%81%AB%E3%81%99%E3%82%8B/</guid><description>&lt;p>SeleniumでChromeをheadlessで使おうとすると複数のオプションがあって、バージョンによって必要なオプションが変わるなど面倒なことが山盛りなので、せめて一箇所で設定できるようにデフォルトで読み込むオプション設定を決めることにしました。&lt;/p></description></item><item><title>FerrumをMacOSのローカルで使う</title><link>https://hack.ironsand.net/2021/ferrum%E3%82%92macos%E3%81%AE%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%81%A7%E4%BD%BF%E3%81%86/</link><pubDate>Thu, 20 May 2021 10:03:02 +0900</pubDate><guid>https://hack.ironsand.net/2021/ferrum%E3%82%92macos%E3%81%AE%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%81%A7%E4%BD%BF%E3%81%86/</guid><description>&lt;p>ChromeのアップデートのためにChrome本体とChromeDriverの相性問題が出るのが嫌になったのでFerrumを使うことにしました。Macのローカルだとバイナリを探してくれずエラーになったので対処方法を記録しておきます。&lt;/p></description></item><item><title>has_oneでhas_manyの最新のレコードを取得する方法</title><link>https://hack.ironsand.net/2021/has_one%E3%81%A7has_many%E3%81%AE%E6%9C%80%E6%96%B0%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</link><pubDate>Fri, 05 Feb 2021 19:27:58 +0900</pubDate><guid>https://hack.ironsand.net/2021/has_one%E3%81%A7has_many%E3%81%AE%E6%9C%80%E6%96%B0%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</guid><description>&lt;p>たとえば&lt;code>User&lt;/code>モデルが&lt;code>has_many&lt;/code>で&lt;code>ArtWork&lt;/code>を持っているときに、最新の&lt;code>ArtWork&lt;/code>を&lt;code>has_one&lt;/code>でも持ちたい場合の実装方法を解説します。&lt;/p></description></item><item><title>ChromeDriverがMacのセキュリティのせいで開けない時の対処</title><link>https://hack.ironsand.net/2020/ChromeDriver%E3%81%8CMac%E3%81%AE%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3%83%86%E3%82%A3%E3%81%AE%E3%81%9B%E3%81%84%E3%81%A7%E9%96%8B%E3%81%91%E3%81%AA%E3%81%84%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6/</link><pubDate>Fri, 03 Jul 2020 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2020/ChromeDriver%E3%81%8CMac%E3%81%AE%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3%83%86%E3%82%A3%E3%81%AE%E3%81%9B%E3%81%84%E3%81%A7%E9%96%8B%E3%81%91%E3%81%AA%E3%81%84%E6%99%82%E3%81%AE%E5%AF%BE%E5%87%A6/</guid><description>&lt;p>久しぶりにローカルで&lt;code>Selenium&lt;/code>を使おうと思ったら&lt;code>chromedriver&lt;/code>を新しいMacに入れてなかったのでこんなエラーが出た。&lt;/p></description></item><item><title>spring が原因で rails server が動かない時の対処法</title><link>https://hack.ironsand.net/2019/howto-solve-rails-server-error-caused-by-spring/</link><pubDate>Tue, 21 May 2019 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2019/howto-solve-rails-server-error-caused-by-spring/</guid><description>&lt;p>&lt;code>undefined method 'application' for Rails:Module&lt;/code>というエラーが出るので対策を調べました。springが原因だったようです。&lt;/p></description></item><item><title>Faviconを取得する簡単な方法</title><link>https://hack.ironsand.net/2018/how-to-get-favicon-easily/</link><pubDate>Mon, 29 Jan 2018 20:00:47 +0000</pubDate><guid>https://hack.ironsand.net/2018/how-to-get-favicon-easily/</guid><description>&lt;p>faviconを取得したければ&lt;code>&amp;lt;link rel=&amp;quot;icon&amp;quot; href=&amp;quot;favicon.ico&amp;quot;&amp;gt;&lt;/code>を探すなどの正攻法がありますが、&lt;/p>
&lt;p>色々と例外があるようなのでめんどくさい時はGoogleさんに任せてしまいましょう。&lt;/p>
&lt;pre>&lt;code>URI('https://www.google.com/s2/favicons?domain_url=stackoverflow.com').read
&lt;/code>&lt;/pre>
&lt;p>でとれます。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="https://stackoverflow.com/questions/5119041/how-can-i-get-a-web-sites-favicon">https://stackoverflow.com/questions/5119041/how-can-i-get-a-web-sites-favicon&lt;/a>&lt;/p></description></item><item><title>invalid value for BigDecimal が Ruby 2.4から出るようになった</title><link>https://hack.ironsand.net/2017/invalid-value-for-bigdecimal-in-ruby-2-4/</link><pubDate>Tue, 26 Dec 2017 03:32:49 +0000</pubDate><guid>https://hack.ironsand.net/2017/invalid-value-for-bigdecimal-in-ruby-2-4/</guid><description>&lt;p>&lt;code>Ruby 2.3&lt;/code> までは&lt;code>Bigdecimal&lt;/code>への変換を行う &lt;code>String#to_d&lt;/code>が数字ではない文字列だった場合に&lt;code>String#to_i&lt;/code>と同じ&lt;code>0.0&lt;/code>を返す仕様だったのに&lt;code>2.4&lt;/code>からは例外を吐くようになったようです。&lt;/p>
&lt;h3 id="ruby-23">Ruby 2.3&lt;/h3>
&lt;pre tabindex="0">&lt;code>require &amp;#39;bigdecimal&amp;#39;
require &amp;#39;bigdecimal/util&amp;#39;
&amp;#39;abc&amp;#39;.to_d
#=&amp;gt; 0.0
&lt;/code>&lt;/pre>&lt;h3 id="ruby-24">Ruby 2.4&lt;/h3>
&lt;pre tabindex="0">&lt;code>require &amp;#39;bigdecimal&amp;#39;
require &amp;#39;bigdecimal/util&amp;#39;
&amp;#39;abc&amp;#39;.to_d
ArgumentError: invalid value for BigDecimal(): &amp;#34;abc&amp;#34;
&lt;/code>&lt;/pre>&lt;h2 id="対策">対策&lt;/h2>
&lt;p>バグでした。&lt;code>gem update bigdecimal&lt;/code> して、&lt;code>Gemfile&lt;/code>を設定しましょう。&lt;/p>
&lt;p>&lt;a href="https://github.com/ruby/bigdecimal/issues/51">https://github.com/ruby/bigdecimal/issues/51&lt;/a>&lt;/p></description></item><item><title>simple_formatを適用させるとauto_linkの target: ‘_blank’が効かない</title><link>https://hack.ironsand.net/2017/auto_link-target_blank-does-not-work-with-simple_format/</link><pubDate>Wed, 01 Nov 2017 14:07:45 +0000</pubDate><guid>https://hack.ironsand.net/2017/auto_link-target_blank-does-not-work-with-simple_format/</guid><description>&lt;h3 id="状態">状態&lt;/h3>
&lt;p>こういうふうに書いたら&lt;code>target: '_blank'&lt;/code>が効かなくて困った。&lt;/p>
&lt;pre>&lt;code>simple_format(auto_link(text, :target =&amp;gt; &amp;quot;_blank&amp;quot; ))
&lt;/code>&lt;/pre>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>&lt;code>simple_format&lt;/code>は&lt;code>a&lt;/code>タグの&lt;code>target&lt;/code>属性を取り除いてしまうので順番を入れ替えて、こう書くとOK。&lt;/p>
&lt;pre>&lt;code>auto_link(simple_format(text), :target =&amp;gt; &amp;quot;_blank&amp;quot; )
&lt;/code>&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="https://github.com/tenderlove/rails_autolink/issues/20#issuecomment-24818734">https://github.com/tenderlove/rails_autolink/issues/20#issuecomment-24818734&lt;/a>&lt;/p></description></item><item><title>Net::HTTPを使ってリモートにあるサーバーの最終更新日を取得</title><link>https://hack.ironsand.net/2017/how-to-get-last-modified-from-remote-url-by-using-net-http/</link><pubDate>Mon, 08 May 2017 07:17:27 +0000</pubDate><guid>https://hack.ironsand.net/2017/how-to-get-last-modified-from-remote-url-by-using-net-http/</guid><description>&lt;p>更新されてる時だけファイルをダウンロードしたいときなどに使う目的で。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-.rb" data-lang=".rb">&lt;span style="display:flex;">&lt;span>uri &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">URI&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;http://example.com/foo.xls&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">Net&lt;/span>&lt;span style="color:#f92672">::&lt;/span>&lt;span style="color:#66d9ef">HTTP&lt;/span>&lt;span style="color:#f92672">.&lt;/span>get_response(uri)&lt;span style="color:#f92672">[&lt;/span>&lt;span style="color:#e6db74">&amp;#39;last-modified&amp;#39;&lt;/span>&lt;span style="color:#f92672">]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74">&amp;#34;Mon, 08 May 2017 01:34:46 GMT&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Rubyの配列から添字を使って複数の値をピックアップする</title><link>https://hack.ironsand.net/2016/pick-up-multiple-values-from-ruby-array/</link><pubDate>Mon, 02 May 2016 18:07:17 +0000</pubDate><guid>https://hack.ironsand.net/2016/pick-up-multiple-values-from-ruby-array/</guid><description>&lt;pre tabindex="0">&lt;code>foo = [1,2,3,4,5]
foo.values_at(2,4) #=&amp;gt; 3, 5
&lt;/code>&lt;/pre>&lt;p>いつも忘れるので自分が忘れた時に検索ですぐ引っかかるようなタイトルにしてます。&lt;/p></description></item><item><title>複数のカラムの値が重複するオブジェクトのIDを取得する</title><link>https://hack.ironsand.net/2016/%E8%A4%87%E6%95%B0%E3%81%AE%E3%82%AB%E3%83%A9%E3%83%A0%E3%81%AE%E5%80%A4%E3%81%8C%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AEid%E3%82%92%E5%8F%96/</link><pubDate>Wed, 30 Mar 2016 02:32:30 +0000</pubDate><guid>https://hack.ironsand.net/2016/%E8%A4%87%E6%95%B0%E3%81%AE%E3%82%AB%E3%83%A9%E3%83%A0%E3%81%AE%E5%80%A4%E3%81%8C%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AEid%E3%82%92%E5%8F%96/</guid><description>&lt;p>複数カラムの値が重複してるオブジェクトの情報を取るために&lt;/p>
&lt;pre class="lang:ruby decode:true " >posts = Post.select(:title, :user_id, :date).group(:title, :user_id, :date).having("count(*) &amp;gt; 1").all&lt;/pre>
&lt;p>とするとこんな感じの&lt;code>id&lt;/code>が&lt;code>nil&lt;/code>の配列が返ってくるので&lt;/p>
&lt;pre class="lang:ruby decode:true " >:id =&amp;gt; nil,
:user_id =&amp;gt; 1345,
:title =&amp;gt; 'foo',
:date =&amp;gt; Tue, 01 Sep 2015&lt;/pre>
&lt;pre class="lang:ruby decode:true " >posts.each do |post|
duplicated_posts = Post.where(user_id: post.user_id, title: post.title, date: post.date)
# なんか処理する
end&lt;/pre>
&lt;p>な感じで適当に処理しましょう。&lt;/p>
&lt;h3 id="追記">追記&lt;/h3>
&lt;p>こちらのほうがスッキリ書けますね。&lt;/p>
&lt;pre class="lang:ruby decode:true " >posts.each do |post|
duplicated_posts = Post.where(post.attributes.except('id'))
# なんか処理する
end&lt;/pre></description></item><item><title>File.write みたいに File.append も使いたい</title><link>https://hack.ironsand.net/2016/file-write-%E3%81%BF%E3%81%9F%E3%81%84%E3%81%AB-file-append-%E3%82%82%E4%BD%BF%E3%81%84%E3%81%9F%E3%81%84/</link><pubDate>Mon, 28 Mar 2016 01:35:03 +0000</pubDate><guid>https://hack.ironsand.net/2016/file-write-%E3%81%BF%E3%81%9F%E3%81%84%E3%81%AB-file-append-%E3%82%82%E4%BD%BF%E3%81%84%E3%81%9F%E3%81%84/</guid><description>&lt;p>&lt;code>File.write(filename, text)&lt;/code>でファイルに書き込みできるの便利ですよね。&lt;/p>
&lt;p>でも、&lt;code>File.append&lt;/code>は用意されてないので&lt;code>File.open(filename, 'a'){|f| f.write(text)}&lt;/code>のように書かなきゃいけません。めんどくさい。&lt;/p>
&lt;p>なのでめんどくさくないように定義しましょう。やっぱり既存クラスのメソッド拡張ができるRubyはいいですね。&lt;/p>
&lt;pre class="lang:ruby decode:true " title="file_append.rb" >class File
def self.append(filename, text)
File.open(filename, 'a'){|f| f.write(text)}
end
end&lt;/pre></description></item><item><title>Hashのキーをリネームする</title><link>https://hack.ironsand.net/2016/hash%E3%81%AE%E3%82%AD%E3%83%BC%E3%82%92%E3%83%AA%E3%83%8D%E3%83%BC%E3%83%A0%E3%81%99%E3%82%8B/</link><pubDate>Mon, 15 Feb 2016 06:08:20 +0000</pubDate><guid>https://hack.ironsand.net/2016/hash%E3%81%AE%E3%82%AD%E3%83%BC%E3%82%92%E3%83%AA%E3%83%8D%E3%83%BC%E3%83%A0%E3%81%99%E3%82%8B/</guid><description>&lt;p>Hashのキーをリネームしたかったけどデフォルトのメソッドはないようなので書いた&lt;/p>
&lt;pre class="lang:ruby decode:true " >class Hash
def rename_key(old:, new:)
return unless has_key?(old)
return if has_key?(new)
self[new] = self.delete(old)
self
end
end&lt;/pre>
&lt;h3 id="使い方">使い方&lt;/h3>
&lt;pre class="lang:ruby decode:true " >h = {a: 1, b: 2}
{
:a =&amp;gt; 1,
:b =&amp;gt; 2
}
[92] pry(main)&amp;gt; h.rename_key(old: :a, new: :c)
{
:b =&amp;gt; 2,
:c =&amp;gt; 1
}&lt;/pre>
&lt;p>変更前のキーが存在しない時と、変更後のキーが存在する時は何もしない。&lt;/p>
&lt;p>実際の入れ替え処理は&lt;code>self[new] = self.delete(old)&lt;/code>だけで終わってます。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="http://kmckelvin.com/blog/2014/06/renaming-items-in-a-ruby-hash">http://kmckelvin.com/blog/2014/06/renaming-items-in-a-ruby-hash&lt;/a>&lt;/p></description></item><item><title>rake task の多重起動を禁止する排他処理を行う</title><link>https://hack.ironsand.net/2015/rake-task-%E3%81%AE%E5%A4%9A%E9%87%8D%E8%B5%B7%E5%8B%95%E3%82%92%E7%A6%81%E6%AD%A2%E3%81%99%E3%82%8B%E6%8E%92%E4%BB%96%E5%87%A6%E7%90%86%E3%82%92%E8%A1%8C%E3%81%86/</link><pubDate>Thu, 13 Aug 2015 08:13:13 +0000</pubDate><guid>https://hack.ironsand.net/2015/rake-task-%E3%81%AE%E5%A4%9A%E9%87%8D%E8%B5%B7%E5%8B%95%E3%82%92%E7%A6%81%E6%AD%A2%E3%81%99%E3%82%8B%E6%8E%92%E4%BB%96%E5%87%A6%E7%90%86%E3%82%92%E8%A1%8C%E3%81%86/</guid><description>&lt;p>rake task で長時間起動するため排他処理をしておかないと複数起動してしまう物があったので多重起動を禁止にしました。&lt;/p>
&lt;pre class="lang:ruby decode:true " >namespace :foo do
task :update_all =&amp;gt; :environment do |task|
process_count = `ps -ef | grep #{task.name} | grep -v grep | wc -l`.to_i
next if process_count &amp;gt; 1
end
end&lt;/pre>
&lt;p>でOK。呼び出し部分をメソッド化とかしたかったけど、上手くいかなかったのでとりあえずこれで。タスクのブロック引数として&lt;code>task&lt;/code>をちゃんと書いておかないと&lt;code>task.name&lt;/code>が使えないので注意しましょう。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ShellScript – Rakeタスク実行時、プロセス起動数を確認して排他制御する – Qiita][1]&lt;/p></description></item><item><title>rake task を途中で抜けるときはreturnじゃなくてnext</title><link>https://hack.ironsand.net/2015/use-next-instead-of-return-go-out-from-rake-task/</link><pubDate>Thu, 13 Aug 2015 07:37:22 +0000</pubDate><guid>https://hack.ironsand.net/2015/use-next-instead-of-return-go-out-from-rake-task/</guid><description>&lt;pre class="lang:ruby decode:true " >task :do_something do
return if some_condition?
do_job
end
&lt;/pre>
&lt;p>というように条件に合致しない時に &lt;code>return&lt;/code>を使って処理を抜けようとするとこんなエラーになります。&lt;/p>
&lt;pre class="lang:default decode:true " >LocalJumpError: unexpected return&lt;/pre>
&lt;p>これは rake task がメソッドではなくブロックだから起きるので &lt;code>next&lt;/code> を使ってやりましょう。それで抜けれます。&lt;/p>
&lt;pre class="lang:ruby decode:true " >task :do_something do
next if some_condition?
do_job
end
&lt;/pre>
&lt;p>ちなみに&lt;code>next&lt;/code>は気持ち悪いんのでせめて&lt;code>break&lt;/code>を使いたいと思ったんですが、それだと&lt;/p>
&lt;pre class="lang:default decode:true " >LocalJumpError: break from proc-closure
&lt;/pre>
&lt;p>になってしまいます。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – How do I return early from a rake task? – Stack Overflow][1]&lt;/p></description></item><item><title>Arrayから重複する要素の配列を抜き出す</title><link>https://hack.ironsand.net/2015/array%E3%81%8B%E3%82%89%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E8%A6%81%E7%B4%A0%E3%81%AE%E9%85%8D%E5%88%97%E3%82%92%E6%8A%9C%E3%81%8D%E5%87%BA%E3%81%99/</link><pubDate>Wed, 18 Mar 2015 00:08:08 +0000</pubDate><guid>https://hack.ironsand.net/2015/array%E3%81%8B%E3%82%89%E9%87%8D%E8%A4%87%E3%81%99%E3%82%8B%E8%A6%81%E7%B4%A0%E3%81%AE%E9%85%8D%E5%88%97%E3%82%92%E6%8A%9C%E3%81%8D%E5%87%BA%E3%81%99/</guid><description>&lt;p>やり過ぎはダメだと言われても、やっぱり基底クラスの拡張は楽しい！&lt;/p>
&lt;pre class="lang:ruby decode:true " >class Array
def duplicated
select{ |v| count(v) &amp;gt; 1}.uniq
end
end&lt;/pre>
&lt;h3 id="使い方">使い方&lt;/h3>
&lt;pre class="lang:ruby decode:true " >a = [3, 5, 3, 4]
=&amp;gt; [3, 5, 3, 4]
[15] pry(main)&amp;gt; a.duplicated
=&amp;gt; [3]
[16] pry(main)&amp;gt; a = [3, 5, 3, 4, 5]
=&amp;gt; [3, 5, 3, 4, 5]
[17] pry(main)&amp;gt; a.duplicated
=&amp;gt; [3, 5]&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Ruby: How to find and return a duplicate value in array? – Stack Overflow][1]&lt;/p></description></item><item><title>日付の配列の中から直近の日付を選択する</title><link>https://hack.ironsand.net/2015/%E6%97%A5%E4%BB%98%E3%81%AE%E9%85%8D%E5%88%97%E3%81%AE%E4%B8%AD%E3%81%8B%E3%82%89%E7%9B%B4%E8%BF%91%E3%81%AE%E6%97%A5%E4%BB%98%E3%82%92%E9%81%B8%E6%8A%9E%E3%81%99%E3%82%8B/</link><pubDate>Fri, 13 Feb 2015 10:20:29 +0000</pubDate><guid>https://hack.ironsand.net/2015/%E6%97%A5%E4%BB%98%E3%81%AE%E9%85%8D%E5%88%97%E3%81%AE%E4%B8%AD%E3%81%8B%E3%82%89%E7%9B%B4%E8%BF%91%E3%81%AE%E6%97%A5%E4%BB%98%E3%82%92%E9%81%B8%E6%8A%9E%E3%81%99%E3%82%8B/</guid><description>&lt;p>日付の配列がある時に&lt;/p>
&lt;pre class="lang:ruby decode:true " >pry(main)&amp;gt; dates = (1..10).map{|i| Date.new(2015,i) }
=&amp;gt; [Thu, 01 Jan 2015, Sun, 01 Feb 2015, Sun, 01 Mar 2015, Wed, 01 Apr 2015, Fri, 01 May 2015, Mon, 01 Jun 2015, Wed, 01 Jul 2015, Sat, 01 Aug 2015, Tue, 01 Sep 2015, Thu, 01 Oct 2015]&lt;/pre>
&lt;p>現在の日付よりも新しく、かつ一番近い日付を選択したい時の処理&lt;/p>
&lt;pre class="lang:ruby decode:true " >pry(main)&amp;gt; Date.today
=&amp;gt; Fri, 13 Feb 2015
pry(main)&amp;gt; dates.find{|date| date &amp;gt; Date.today}
=&amp;gt; Sun, 01 Mar 2015
&lt;/pre>
&lt;p>&lt;code>find&lt;/code>メソッドはやっぱり便利！&lt;/p></description></item><item><title>既にインデックスをつけているカラムにUnique制限をつける</title><link>https://hack.ironsand.net/2015/add-unique-constraint-to-a-already-indexed-column/</link><pubDate>Mon, 02 Feb 2015 08:18:21 +0000</pubDate><guid>https://hack.ironsand.net/2015/add-unique-constraint-to-a-already-indexed-column/</guid><description>&lt;p>Railsで既に&lt;code>index&lt;/code>をつけているカラムに&lt;code>unique&lt;/code>制限をもうけるには、ユニーク制限だけを付加することができないので一旦インデックス自体を削除してから新規に unique な index を追加します。&lt;/p>
&lt;pre>&lt;code>def change
remove_index :editabilities, [:user_id, :list_id]
add_index :editabilities, [:user_id, :list_id], unique: true
end
&lt;/code>&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – How to add &lt;code>unique&lt;/code> constraint to already existing index by migration – Stack Overflow][1]&lt;/p></description></item><item><title>ActiveSupport::Inflectorで定義した頭字語が`has_many`を解釈してくれない時の解決法</title><link>https://hack.ironsand.net/2015/howto-fix-when-acronym-is-not-recognized-defined-in-activesupport-inflector/</link><pubDate>Mon, 02 Feb 2015 07:23:20 +0000</pubDate><guid>https://hack.ironsand.net/2015/howto-fix-when-acronym-is-not-recognized-defined-in-activesupport-inflector/</guid><description>&lt;p>Rubyの命名規則としてクラス名は通常は&lt;code>CamelCase&lt;/code>で記述しますが、&lt;code>HTTP&lt;/code>などの頭字語(Acronym)はそのまますべて大文字で記述します。&lt;/p>
&lt;p>Railsで頭字語のクラス名を使うときはモデル等を生成する前に設定で以下の記述を行っておくと自動的に認識してくれます。&lt;/p>
&lt;h4 id="configinitializersinflectionsrb">config/initializers/inflections.rb&lt;/h4>
&lt;pre tabindex="0">&lt;code>ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym &amp;#39;GNU&amp;#39;
end
&lt;/code>&lt;/pre>&lt;p>ただ&lt;code>has_many&lt;/code>でこのクラスを持とうとすると以下のエラーになってしまいます。&lt;/p>
&lt;blockquote>
&lt;p>LoadError: Unable to autoload constant Gnu, expected&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>/Users/ironsand/dev/myproject/app/models/gnu.rb to define it&lt;/p>&lt;/blockquote>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>クラス名を明示的に指定するとOKです。&lt;/p>
&lt;pre tabindex="0">&lt;code>has_many :gnus, class_name =&amp;gt; &amp;#34;GNU&amp;#34;
&lt;/code>&lt;/pre></description></item><item><title>NokogiriとSelenium-WebDriverのxpathの違い</title><link>https://hack.ironsand.net/2014/difference-between-nokogiri-and-selenium-webdriver-about-xpath/</link><pubDate>Sun, 04 May 2014 01:39:37 +0000</pubDate><guid>https://hack.ironsand.net/2014/difference-between-nokogiri-and-selenium-webdriver-about-xpath/</guid><description>&lt;p>&lt;code>Selenium-WebDriver&lt;/code>と&lt;code>Nokogiri&lt;/code>で同じファイルを開いても違う&lt;code>xpath&lt;/code>を書かないといけないとダメで少しハマったのでメモ。&lt;/p>
&lt;h2 id="違いの原因">違いの原因&lt;/h2>
&lt;p>同じHTMLを解析しても&lt;code>Selenium-WebDriver&lt;/code>で&lt;code>Chrome&lt;/code>や&lt;code>Firefox&lt;/code>で開いた時は任意の要素である&lt;code>tbody&lt;/code>が挿入されるために違いが発生します。&lt;/p>
&lt;h2 id="実際の例">実際の例&lt;/h2>
&lt;p>例えば&lt;/p>
&lt;pre class="lang:xhtml decode:true " >&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;&lt;/pre>
&lt;p>とある時に&lt;/p>
&lt;h2 id="nokogiri-の場合">Nokogiri の場合&lt;/h2>
&lt;pre class="lang:ruby decode:true " >require 'nokogiri'
doc = Nokogiri::HTML(File.read("test.html"))
doc.xpath("//table/tr")&lt;/pre>
&lt;h2 id="selenium-webdriver-の場合">Selenium WebDriver の場合&lt;/h2>
&lt;pre class="lang:ruby decode:true " >require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.get("file:///C:/Users/ironsand/test.html")
driver.find_element(:xpath, "//table/tbody/tr")&lt;/pre>
&lt;h2 id="基本的な対策">基本的な対策&lt;/h2>
&lt;p>大体の場合は&lt;code>//table//tr&lt;/code>とどちらにも対応できるように書いたらいいわけですが、汚いHTMLを解析するときに&lt;/p>
&lt;p>&lt;code>//tr/td/font[contains(text(), 'Keyword')]/../../following-sibling::tr[3]/td&lt;/code>&lt;/p>
&lt;p>な感じで正確なタグの数が必要になることがあるのでそういう時に気をつけましょう。&lt;/p>
&lt;h2 id="参考">参考&lt;/h2>
&lt;p>[ruby – How to get element that have same parent and n th different position by xpath – Stack Overflow][1]&lt;/p></description></item><item><title>irbで`require “foobar”`するのがめんどくさいので楽にする</title><link>https://hack.ironsand.net/2014/require-library-easily-in-irb/</link><pubDate>Wed, 30 Apr 2014 15:37:39 +0000</pubDate><guid>https://hack.ironsand.net/2014/require-library-easily-in-irb/</guid><description>&lt;p>例えば nokogiri でちゃんとデータの取得ができてるか確認するために&lt;/p>
&lt;p>&lt;code>irb&lt;/code>を使うために&lt;/p>
&lt;p>&lt;code>$ irb&lt;/code> してから&lt;/p>
&lt;p>&lt;code>require &amp;quot;nokogiri&amp;quot;&lt;/code>と&lt;/p>
&lt;p>自分はいつもしてたんですが、めんどくさい。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>$ irb -r nokogiri&lt;/code>な感じでライブラリが読み込めます。ステキ！&lt;/p></description></item><item><title>rubyの`FileIO.readline`に改行を含ませないようにする</title><link>https://hack.ironsand.net/2014/without-line-break-with-fileio-readline-in-ruby/</link><pubDate>Sat, 26 Apr 2014 18:17:15 +0000</pubDate><guid>https://hack.ironsand.net/2014/without-line-break-with-fileio-readline-in-ruby/</guid><description>&lt;p>こんなふうにファイルを読み込むと標準では改行が入ってしまいます。&lt;/p>
&lt;pre class="lang:ruby decode:true " >File.open(filename,"r") do |f|
f.readlines
end&lt;/pre>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>改行はいらないので消しちゃいましょう。&lt;/p>
&lt;pre class="lang:ruby decode:true " >File.open(filename,"r") do
|f| f.readlines.map &amp;:chomp
end&lt;/pre></description></item><item><title>rubyで配列のMerge `(array1 + array2).uniq` を更に簡単に</title><link>https://hack.ironsand.net/2014/merge-arrays-in-ruby/</link><pubDate>Sat, 26 Apr 2014 17:59:26 +0000</pubDate><guid>https://hack.ironsand.net/2014/merge-arrays-in-ruby/</guid><description>&lt;p>&lt;code>array1 | array2&lt;/code> で &lt;code>(array1 + array2).uniq&lt;/code> と同じように配列を重複なしで結合してくれます。ステキ！&lt;/p></description></item><item><title>irb上でrubyスクリプトの実行速度を測る</title><link>https://hack.ironsand.net/2014/benchmark-ruby-script-in-irb/</link><pubDate>Sat, 26 Apr 2014 16:59:23 +0000</pubDate><guid>https://hack.ironsand.net/2014/benchmark-ruby-script-in-irb/</guid><description>&lt;p>例えば &lt;code>foo.rb&lt;/code>というRubyスクリプトの実行時間をシェル上で知りたかったら&lt;code>time foo.rb&lt;/code>とすればいいわけですが、残念ながら&lt;code>irb&lt;/code>上では&lt;code>time&lt;/code>コマンドをそのまま使うことができません。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>標準で用意されている&lt;code>Benchmark&lt;/code>ライブラリを使えばOK.&lt;/p>
&lt;pre class="lang:ruby decode:true " >irb(main)&amp;gt; require 'benchmark'
irb(main)&amp;gt; puts Benchmark.measure { "a"*1_000_000_000 }
0.350000 0.400000 0.750000 ( 0.835234)&lt;/pre>
&lt;p>こんな感じで表示されます。&lt;/p>
&lt;p>&lt;code>0.750000&lt;/code>がCPUを使った時間で&lt;code>0.835234&lt;/code>が実際にかかった時間。&lt;/p>
&lt;p>気にしないといけないのはCPUを実際に使ってたじかんなので前者のほうになります。&lt;/p>
&lt;p>ちなみに自分の環境だと裏で動画のエンコードを走らせながらVirtualBox上のOSでやってたのでCPU使用時間の50倍ぐらい実際にかかってしまってました。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Module: Benchmark (Ruby 2.1.1)][1]&lt;/p>
&lt;p>[When benchmarking, what causes a lag between CPU time and &amp;ldquo;elapsed real time&amp;rdquo;? – Stack Overflow][2]&lt;/p></description></item><item><title>Railsで使うpostgresqlのスーパーユーザーを作成する</title><link>https://hack.ironsand.net/2014/create-postgres-superuser-for-rails/</link><pubDate>Fri, 25 Apr 2014 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2014/create-postgres-superuser-for-rails/</guid><description>&lt;p>RailsでPostgresqlを使うときに毎回そのためのユーザーの作成方法を忘れてしまうのでメモ。&lt;/p>
&lt;!-- more -->
&lt;h3 id="まずユーザーを作成">まずユーザーを作成&lt;/h3>
&lt;p>Windowsで&lt;code>Chocolatey&lt;/code>を使って&lt;code>postgresql&lt;/code>をインストールしてると初期パスワードは&lt;code>Postgres1234&lt;/code>になってるので、&lt;/p>
&lt;pre tabindex="0">&lt;code>$ psql.exe -U postgres
ユーザ postgres のパスワード: ←ここでパスワードを入力
psql (9.2.1)
&amp;#34;help&amp;#34; でヘルプを表示します.
postgres=# create user deployer;
CREATE ROLE
postgres=# alter user deployer with superuser;
ALTER ROLE
&lt;/code>&lt;/pre>&lt;p>これでパスワード無しの&lt;code>deployer&lt;/code>ユーザーが必要な権限付きで作成される。&lt;/p>
&lt;p>※ Linux系のOSなら&lt;code>sudo -u postgres psql&lt;/code>で&lt;code>psql&lt;/code>コマンドを使いましょう。&lt;/p>
&lt;h3 id="パスワード無しユーザーでもokにする">パスワード無しユーザーでもOKにする&lt;/h3>
&lt;p>これだけだと&lt;code>fe_sendauth: no password supplied&lt;/code>というエラーが出てしまうので&lt;/p>
&lt;p>&lt;code>C:\PostgreSQL\data\pg_hba.conf&lt;/code>を開いて&lt;/p>
&lt;pre tabindex="0">&lt;code># IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
&lt;/code>&lt;/pre>&lt;p>を&lt;/p>
&lt;pre tabindex="0">&lt;code># IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
&lt;/code>&lt;/pre>&lt;p>に書き換える。&lt;/p></description></item><item><title>WindowsのRailsでtwitter-bootstrap-railsを使う方法</title><link>https://hack.ironsand.net/2014/how-to-use-twitter-bootstrap-rails-in-windows/</link><pubDate>Sun, 20 Apr 2014 09:13:20 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-use-twitter-bootstrap-rails-in-windows/</guid><description>&lt;p>&lt;code>twitter-bootstrap-rails&lt;/code>を普通にWindowsで使おうとすると&lt;code>libv8がないからアカンで～&lt;/code>的なエラーが出てきてインストールに失敗します。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>特定のバージョンはインストールできるのでそれをインストールしましょう。&lt;/p>
&lt;p>&lt;code>gem install libv8 --version 3.11.8.0&lt;/code>&lt;/p>
&lt;p>あとは&lt;code>Gemfile&lt;/code>に&lt;code>gem 'therubyracer', platforms: :ruby&lt;/code>のコメントアウトを外して&lt;code>gem twitter-bottstrap-rails&lt;/code>を追加すれば万事OK！&lt;/p>
&lt;h3 id="追記より良い解決策">追記、より良い解決策&lt;/h3>
&lt;p>そもそも&lt;code>bootstrap-sass&lt;/code>を使えばよいのでは？&lt;/p></description></item><item><title>Rails+Angular.js で InvalidAuthenticityTokenエラーが出るときの対処法</title><link>https://hack.ironsand.net/2014/how-to-handle-invalidauthenticitytoken-error-in-rails-and-angular/</link><pubDate>Thu, 17 Apr 2014 18:37:04 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-handle-invalidauthenticitytoken-error-in-rails-and-angular/</guid><description>&lt;p>RailsのDBからGETでデータの呼び出しはできるけど、PUTで値の追加ができなくてこんなエラーに成った時の対処法&lt;/p>
&lt;pre class="lang:default decode:true " >Completed 422 Unprocessable Entity in 126ms
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:&lt;/pre>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>CSRF対策です。&lt;/p>
&lt;h3 id="ダメな対策">ダメな対策&lt;/h3>
&lt;p>モデルにこれを追加&lt;/p>
&lt;pre class="lang:ruby decode:true " >skip_before_filter :verify_authenticity_token&lt;/pre>
&lt;p>サイバーノーガード戦法です。おすすめしません。&lt;/p>
&lt;h3 id="良さそうな対策">良さそうな対策&lt;/h3>
&lt;p>gem の&lt;code>&amp;lt;a href=&amp;quot;https://github.com/xrd/ng-rails-csrf&amp;quot;&amp;gt;ns-csrf-rails&amp;lt;/a&amp;gt;&lt;/code>を使う。わからないことは全部gemにまかせてしまいましょう。&lt;/p></description></item><item><title>rbenvのインストール方法と入れとくと便利なプラグイン</title><link>https://hack.ironsand.net/2014/rbenv-and-plugins-install/</link><pubDate>Fri, 04 Apr 2014 16:11:27 +0000</pubDate><guid>https://hack.ironsand.net/2014/rbenv-and-plugins-install/</guid><description>&lt;p>git は便利なんだけど、リポジトリ名が長くて暗記できなのがツラい所ですよね。&lt;/p>
&lt;p>忘れて毎回プラグインのリンクを探すのがめんどくさいのでいつも入れるものをメモっておきます。&lt;/p></description></item><item><title>rubyで全角半角を考慮して文字の幅を取得</title><link>https://hack.ironsand.net/2014/get-string-size-considering-double-width-and-normal-width-ruby/</link><pubDate>Thu, 03 Apr 2014 14:54:36 +0000</pubDate><guid>https://hack.ironsand.net/2014/get-string-size-considering-double-width-and-normal-width-ruby/</guid><description>&lt;p>文字幅をしるために&lt;code>全角=2&lt;/code>,&lt;code>半角=1&lt;/code>で文字サイズが取得したかったので&lt;/p>
&lt;h3 id="やり方">やり方&lt;/h3>
&lt;pre class="lang:ruby decode:true " >"日本語とEnglish".each_char.map{|c| c.ascii_only? ? 1 : 2}.inject(:+)&lt;/pre>
&lt;p>Rubyばんざーーい！&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[2013/02/20/Rubyで全半角混在文字の文字幅を計算する方法 – ヽ（´・肉・｀）ノログ][1]&lt;/p></description></item><item><title>Deviseのユーザーを`rake db:seed`で追加する</title><link>https://hack.ironsand.net/2014/how-to-add-devise-user-by-rake-db-seed/</link><pubDate>Sun, 30 Mar 2014 21:08:01 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-add-devise-user-by-rake-db-seed/</guid><description>&lt;p>&lt;code>db/seeds.rb&lt;/code>に&lt;code>User.create&lt;/code>で&lt;code>encrypted_password&lt;/code>を与えてDeviseのユーザーを作ろうとしたら失敗した。&lt;/p>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>passwordが存在しないために&lt;code>validation&lt;/code>に引っかかってしまってる。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>&lt;code>password&lt;/code>を平文で打てば解決するらしいけど、平文のパスワードは使いたくないのでバリデーションの方を無効化してしまいましょう。&lt;/p>
&lt;p>&lt;code>User.new(email: someone@example.com).save(validate: false)&lt;/code>&lt;/p>
&lt;p>でOKです。&lt;code>encrypted_password&lt;/code>の取得方法は色々あるでしょうが、自分の場合は一度Deviseで作ってからそれを&lt;code>rails console&lt;/code>使って&lt;code>User.first.encrypted_password&lt;/code>を取ってきました。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Cannot create Devise account using rake db:seed for Rails 3.0 – Stack Overflow][1]&lt;/p></description></item><item><title>Wheneverを使うRailsプロジェクトをWindowsで触るときの注意点</title><link>https://hack.ironsand.net/2014/rails-project-with-whenever-in-windows/</link><pubDate>Fri, 28 Mar 2014 07:29:47 +0000</pubDate><guid>https://hack.ironsand.net/2014/rails-project-with-whenever-in-windows/</guid><description>&lt;p>&lt;code>/usr/bin/env: ruby.exe: そのようなファイルやディレクトリはありません&lt;/code>&lt;/p>
&lt;p>というエラーが&lt;code>whenever&lt;/code>で作ったcronのログとして出てきて困ってた。&lt;/p>
&lt;p>&lt;code>bin/rails&lt;/code>の実行権限とか&lt;code>/usr/bin/env ruby --version&lt;/code> でちゃんとrubyが動いてるかどうか確認したりとか色々。&lt;/p>
&lt;h3 id="答え">答え&lt;/h3>
&lt;p>Windowsで作ったRailsプロジェクトだったので &lt;code>ruby&amp;lt;strong&amp;gt;.exe&amp;lt;/strong&amp;gt;&lt;/code>になってた。なんてことだ…。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>bin/rails&lt;/code>の一行目&lt;code>usr/bin/env ruby.exe&lt;/code>の&lt;code>.exe&lt;/code>を削除しておきましょう。ふぁっく。&lt;/p></description></item><item><title>%w(foo bar) みたいな感じでsymbolを作る方法</title><link>https://hack.ironsand.net/2014/create-a-array-of-symbol-like-string-w/</link><pubDate>Thu, 27 Mar 2014 13:57:39 +0000</pubDate><guid>https://hack.ironsand.net/2014/create-a-array-of-symbol-like-string-w/</guid><description>&lt;p>rubyは文字列の配列を作るときに&lt;code>[&amp;quot;foo&amp;quot;, &amp;quot;bar&amp;quot;]&lt;/code> と書くのがめんどくさいからという理由で &lt;code>%w(foo bar)&lt;/code> みたいな感じで書けちゃいます。&lt;/p>
&lt;p>同じような感じで&lt;code>symbol&lt;/code>もかけないかと思って探してたら&lt;code>ruby 2.0.0&lt;/code>から&lt;code>%i&lt;/code>が使えるようになったので&lt;/p>
&lt;p>&lt;code>%i(foo bar)&lt;/code> で [:foo, :bar] が書けちゃいます。ステキ！&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – Is there way to create a symbol's array like string with %w? – Stack Overflow][1]&lt;/p></description></item><item><title>rbenv: version `2.0.0′ is not installed の対処法</title><link>https://hack.ironsand.net/2014/rbenv-version-2-0-0-is-not-installed/</link><pubDate>Thu, 20 Mar 2014 05:15:06 +0000</pubDate><guid>https://hack.ironsand.net/2014/rbenv-version-2-0-0-is-not-installed/</guid><description>&lt;p>&amp;ldquo;Everyday Rails Testing with RSpec&amp;quot;を購入して読んでると&lt;/p>
&lt;p>サンプルプロジェクトが紹介されてたので&lt;code>git clone&lt;/code>でダウンロードしてきて &lt;code>bundle install&lt;/code> をすると&lt;/p>
&lt;p>&lt;code>rbenv: version '2.0.0' is not installed&lt;/code>&lt;/p>
&lt;p>と出ちゃって困る。しかも &lt;code>ruby -v&lt;/code> と確認しようと思ったら再び&lt;/p>
&lt;p>&lt;code>rbenv: version '2.0.0' is not installed&lt;/code>&lt;/p>
&lt;p>？？？？？？&lt;/p>
&lt;p>「まさか rbenv が壊れた！？」と思って調べてたら、どうやらカレントディレクトリに&lt;code>.ruby-version&lt;/code>があるとそのバージョンの Ruby を参照しようとするために起きるらしい。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>自分の場合は&lt;code>2.0.0.p247&lt;/code>が入っているので該当のディレクトリで&lt;code>rbenv local&lt;/code>すると&lt;code>.ruby-version&lt;/code>の中身が書き換えられて上手く動くようになりました。&lt;/p>
&lt;p>Version 自体が異なる場合は上手く動かないと困るので該当のバージョンの ruby を&lt;code>rbenv install 2.x.x&lt;/code>でインストールしましょう。&lt;/p></description></item><item><title>gem `mail` と sendmail を使って簡単にRubyでメール送信</title><link>https://hack.ironsand.net/2014/send-mail-with-gem-mail-and-sendmail-command-in-ruby/</link><pubDate>Tue, 18 Mar 2014 12:29:20 +0000</pubDate><guid>https://hack.ironsand.net/2014/send-mail-with-gem-mail-and-sendmail-command-in-ruby/</guid><description>&lt;p>&lt;code>net/smtp&lt;/code> ライブラリを使うよりも &lt;code>gem mail&lt;/code> を使えば簡単にメールが送れるらしいので調べてみた。&lt;/p>
&lt;h3 id="方法">方法&lt;/h3>
&lt;p>まず &lt;code>mail&lt;/code> gem が必要なので &lt;code>$ gem install mail&lt;/code> でインストールする。&lt;/p>
&lt;p>んで、下のように書けばOK!&lt;/p>
&lt;pre class="lang:ruby decode:true " >require 'mail'
mail = Mail.new do
from 'me@ironsand.net'
to 'recipient@exmaple.com'
subject 'Here is the image you wanted'
body 'Body'
end
mail.delivery_method :sendmail
mail.deliver&lt;/pre>
&lt;h3 id="注意点">注意点&lt;/h3>
&lt;p>Macでは動かなかったので、VPS上の&lt;code>CentOS 6.4&lt;/code>で試してます。&lt;/p>
&lt;p>25版ポートを開けるのを忘れずに。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[email – Ruby Mail gem: Connection refused – connect(2) (Errno::ECONNREFUSED) – Stack Overflow][1]&lt;/p></description></item><item><title>datatimeとtimestampの違いとRailsで知っておくべきこと</title><link>https://hack.ironsand.net/2014/difference-between-datatime-and-timestamp-in-rails/</link><pubDate>Mon, 17 Mar 2014 19:56:14 +0000</pubDate><guid>https://hack.ironsand.net/2014/difference-between-datatime-and-timestamp-in-rails/</guid><description>&lt;p>何回か調べたけど未だに覚えられてないのでもう一度調べ直しました。&lt;/p>
&lt;p>確か前回調べたときは英語読むのがめんどくさいから日本語の情報を見て納得したんですが、今回はめんどくさがらずに英語圏で検索したらいつも通りStackOverflowにて大変わかりやすい答えが見つかりました。ありがたや〜。 (人´∀｀)&lt;/p>
&lt;h3 id="datatime-と-timestamp-の違い">datatime と timestamp の違い&lt;/h3>
&lt;p>&lt;code>datatime&lt;/code> が 1000年から9999年までを表現して &lt;code>timestamp&lt;/code> は &lt;code>unix timestamp&lt;/code>のフロントエンドに過ぎないので 1970年から2038年までしか扱えない。&lt;/p>
&lt;p>扱う範囲が違うので&lt;code>datatime&lt;/code>が8バイト&lt;code>timestamp&lt;/code>が4バイト使う。&lt;/p>
&lt;h3 id="rails-における扱い">Rails における扱い。&lt;/h3>
&lt;p>これが一番重要な気がするけど、Railsでは&lt;strong>どちらを指定してもデータベースには&lt;code>DATATIME&lt;/code>型で保存される&lt;/strong>。&lt;/p>
&lt;p>何故かこのことについて述べてるサイトがあんまりなかったのでこの記事を書いておきました。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date? – Stack Overflow][1]&lt;/p></description></item><item><title>複数の引数を取る関数に配列で値を渡す</title><link>https://hack.ironsand.net/2014/pass-array-as-individual-arguments/</link><pubDate>Mon, 17 Mar 2014 17:54:36 +0000</pubDate><guid>https://hack.ironsand.net/2014/pass-array-as-individual-arguments/</guid><description>&lt;p>rubyで &lt;code>def foo(arg1, arg2, arg3)&lt;/code> な関数があった時に &lt;code>values = [1, 2, 3]&lt;/code> という値を渡そうとして&lt;/p>
&lt;p>&lt;code>foo values&lt;/code> とすると &lt;code>TypeError: no implicit conversion of Array into Integer&lt;/code> というエラーになります。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>この配列をいうなれば”展開”して渡したいわけですが、やり方がわからなかったので調べました。&lt;/p>
&lt;p>&lt;code>foo *values&lt;/code> のようにまるでC言語みたいな渡し方をすればOK.&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[caiustheory.com/sending-array-elements-as-individual-arguments-in-ruby][1]&lt;/p></description></item><item><title>rubyで文字列を正規表現で分類してグループ分けする</title><link>https://hack.ironsand.net/2014/group-strings-by-similar-pattern-with-ruby/</link><pubDate>Mon, 17 Mar 2014 15:58:02 +0000</pubDate><guid>https://hack.ironsand.net/2014/group-strings-by-similar-pattern-with-ruby/</guid><description>&lt;p>最後の数字だけ違う文字列をグループ分けしたかったんですが、自分で書いてみるとこんな面倒くさい感じになって (´・ω・｀)ｼｮﾎﾞｰﾝ&lt;/p>
&lt;pre class="lang:ruby decode:true " >a1 = %w(foo1 foo2 foo3 bar1 bar3 buz2)
a2 = a1.map{|v| v.sub(/[0-9]$/,"")}.uniq
tmp = Array.new(a2.size)
a2.each_with_index do |v2, i|
a1.each do |v1|
if v1.sub(/[0-9]$/, "") == v2
tmp[i].nil? ? tmp[i] = [v1] : tmp[i].push(v1)
end
end
end
p tmp # =&amp;gt; [["foo1", "foo2", "foo3"], ["bar1", "bar3"], ["buz2"]]&lt;/pre>
&lt;p>なので、調べてみたら非常に簡単な方法が見つかりました。&lt;/p>
&lt;pre class="lang:ruby decode:true " >a1 = %w(foo1 foo2 foo3 bar1 bar3 buz2)
p a1.group_by{|x| x[/[a-zA-Z]+/] }.values&lt;/pre>
&lt;p>Rubyすげぇ！&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[arrays – Group strings with similar pattern in Ruby – Stack Overflow][1]&lt;/p></description></item><item><title>Macで`gem install rmagick`がコケる時の対処法</title><link>https://hack.ironsand.net/2014/gem-install-rmagic-in-maverick/</link><pubDate>Mon, 27 Jan 2014 09:24:19 +0000</pubDate><guid>https://hack.ironsand.net/2014/gem-install-rmagic-in-maverick/</guid><description>&lt;p>画像編集をRailsでしたかったので &lt;code>ImageMagick&lt;/code> を Railsから使うgem &lt;code>rmagick&lt;/code> をインストールしようと思ったらこんなエラーが出てしまった。環境は Maverick。&lt;/p>
&lt;pre class="lang:default decode:true " >Can't install RMagick 2.13.1. Can't find the ImageMagick library or one of the dependent libraries. Check the mkmf.log file for more detailed information.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.&lt;/pre>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>最新版の &lt;code>2.13.2&lt;/code> をインストールすればOK。ちなみにライブラリのリンクを作るという方法も紹介されてましたがこっちは失敗。&lt;/p>
&lt;p>&lt;code>gem install rmagick -v '2.13.2'&lt;/code> でインストールしましょう。&lt;/p></description></item><item><title>rbenvでインストールしたgemをEmacsから使う</title><link>https://hack.ironsand.net/2014/how-to-use-gem-in-rbenv-with-emacs/</link><pubDate>Thu, 23 Jan 2014 08:04:42 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-use-gem-in-rbenv-with-emacs/</guid><description>&lt;p>rspec書いてる時にEmacsから簡単に呼び出せたら便利だと思って&lt;code>smart-compile&lt;/code>の設定をこんな風に書き換えたんだけど、&lt;/p>
&lt;pre class="lang:lisp decode:true " >(setq smart-compile-alist
(append
'(("\_spec.rb$" . "rspec %f"))
'(("\\.rb$" . "ruby %f"))
'(("\\.php$" . "php %f"))
'(("\\.coffee$" . "coffee -p %f"))
'(("\Gemfile$" . "bundle install"))
smart-compile-alist))
&lt;/pre>
&lt;p>でちゃんと&lt;code>Compile command: rspec foo_spec.rb&lt;/code>は呼び出せるものの実行すると&lt;/p>
&lt;pre class="lang:default decode:true " >-*- mode: compilation; default-directory: "~/dev/zombie/spec/lib/" -*-
Compilation started at Thu Jan 23 16:50:59
rspec zombie_spec.rb
/bin/bash: rspec: コマンドが見つかりません
Compilation exited abnormally with code 127 at Thu Jan 23 16:51:00&lt;/pre>
&lt;p>な感じでエラーになってしまう。&lt;/p>
&lt;p>どうやら rbenv の環境がEmacs側で認識されてないので rbenv で入れた gem のコマンドも使えないのが原因らしい。&lt;/p></description></item><item><title>Controllerで出る`syntax error, unexpected ‘:’, expecting keyword_end`エラー</title><link>https://hack.ironsand.net/2014/syntax-error-unexpected-expecting-keyword_end-in-controller/</link><pubDate>Mon, 13 Jan 2014 09:44:32 +0000</pubDate><guid>https://hack.ironsand.net/2014/syntax-error-unexpected-expecting-keyword_end-in-controller/</guid><description>&lt;pre class="lang:default decode:true " >yntax error, unexpected ':', expecting keyword_end&lt;/pre>
&lt;p>といつもの見慣れたようなTypoのエラーが出たのでControllerを確認したけどない。何度見てもちゃんとカッコ閉じてるし &lt;code>end&lt;/code> も使ってる。なのにエラーが消えない！&lt;/p>
&lt;h3 id="解決">解決&lt;/h3>
&lt;p>モデルのほうで &lt;code>has_many: words&lt;/code> って書いてました。 正しくは &lt;code>has_many :words&lt;/code> ですね。悲しい。こんなものに結構な時間を着いたしたのが悲しい。&lt;/p></description></item><item><title>Capyparaで `undefined method `visit’`と出る時の対処法</title><link>https://hack.ironsand.net/2014/how-to-solve-capypara-undefined-method-visit/</link><pubDate>Thu, 09 Jan 2014 08:55:19 +0000</pubDate><guid>https://hack.ironsand.net/2014/how-to-solve-capypara-undefined-method-visit/</guid><description>&lt;p>「Rspecよくわかんねー。」という思いを胸に抱き続けてきましたが、やっぱりやらないとダメっぽいので使おうとしたら早速 &lt;code>undefined method 'visit'&lt;/code>と言われて困る。&lt;/p>
&lt;h3 id="原因">原因&lt;/h3>
&lt;p>このエラーの原因はCapybaraが最近のアップデートでこっそりと対象のディレクトリを &lt;code>requests&lt;/code> から &lt;code>features&lt;/code> に変えたのが原因らしいです。やめてほしいそういうの。&lt;/p>
&lt;p>しかも [Google group][1] にこっそり報告してるだけ。もっと こえを おおきく。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>spec_helper.rb&lt;/code> に以下をたす解決策もあるそうですが、将来的に requests じゃなくてfeatures を使うという意向ならそうしたほうが楽そうなので &lt;code>mv spec/requests/ spec/features&lt;/code> して自分は対策しました。&lt;/p>
&lt;p>ちなみに &lt;code>spec_helper.rb&lt;/code>に足す場合はこちら。&lt;/p>
&lt;pre class="lang:default decode:true " title="spec/spec_helper.rb" >require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – Capybara: undefined method 'visit' – Stack Overflow][2]&lt;/p>
&lt;p>[rails 3.2でRspecを導入したけど NameError – undefined method `visit' って出るときの対策 – どぶんけーブログ][3]&lt;/p></description></item><item><title>rbenv を最新の状態にupdate する</title><link>https://hack.ironsand.net/2014/rbenv-update/</link><pubDate>Tue, 07 Jan 2014 13:46:26 +0000</pubDate><guid>https://hack.ironsand.net/2014/rbenv-update/</guid><description>&lt;p>ruby 2.1.0 も出たことだし、とりあえずさくらVPSにおいてるCentOS上に入れておこうと思ったけど、ちょっと古いバージョンしか &lt;code>rbenv install --list&lt;/code> で出てくれない。&lt;/p>
&lt;p>&lt;code>rbenv update&lt;/code>という便利なコマンドはないようなのでアップデート方法を調べてアップデートしてみた。&lt;/p>
&lt;h3 id="アップデート方法">アップデート方法&lt;/h3>
&lt;p>どうやってインストールしたかは全く覚えてなかったけど調べてみるとどうやら &lt;code>git clone&lt;/code> でインストールしたっぽいので &lt;code>cd ~/.rbenv; git pull&lt;/code> するだけでOKらしい。&lt;/p>
&lt;p>無事にアップデートできたのでもう一度インストールできるrubyのバージョンを確認したら ない。ない。&lt;/p>
&lt;p>どうやらrubyのインストールには &lt;code>ruby-build&lt;/code>を使ってるのでそっちをアップデートしないとインストール可能なバージョンは更新されないらしい。ちなみに存在しないバージョンのインストールを試みるとこんな感じに教えてくれる。&lt;/p>
&lt;pre class="lang:sh decode:true " >ironsand@SarakuVPS ~$ rbenv install 2.0.0-p247
ruby-build: definition not found: 2.0.0-p247
You can list all available versions with `rbenv install --list'.
If the version you're looking for is not present, first try upgrading
ruby-build. If it's still missing, open a request on the ruby-build
issue tracker: https://github.com/sstephenson/ruby-build/issues&lt;/pre>
&lt;h3 id="ruby-build-のアップデート">ruby-build のアップデート&lt;/h3>
&lt;p>ruby-buildも&lt;code>git clone&lt;/code>してるだけなので、 &lt;code>cd .rbenv/plugins/ruby-build/; git pull&lt;/code> すれば最新の状態に反映される。後はいつも通り &lt;code>rbenv install --list&lt;/code> して &lt;code>2.1.0&lt;/code> があることを確認して &lt;code>rbenv install 2.1.0&lt;/code> でインストールできる。時間がかかるので放置しときましょう。&lt;/p></description></item><item><title>request.fullpathでparamを使わない</title><link>https://hack.ironsand.net/2014/request-fullpath-without-params/</link><pubDate>Sun, 05 Jan 2014 03:24:33 +0000</pubDate><guid>https://hack.ironsand.net/2014/request-fullpath-without-params/</guid><description>&lt;p>Railsのrequest.fullpath だと param 以降も受け取ってしまうので、paramの値を使わない時の方法。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>splitで邪魔なものをとる方法&lt;/p>
&lt;pre class="lang:ruby decode:true " >request.fullpath.split("?")[0]&lt;/pre>
&lt;p>&lt;code>path&lt;/code> を使う方法。こっちのほうがよさそう。&lt;/p>
&lt;pre class="lang:ruby decode:true " >request.path&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – request.fullpath with no parameters – Stack Overflow][1]&lt;/p></description></item><item><title>`config/routes.rb`内のブロックはブロックじゃない。</title><link>https://hack.ironsand.net/2014/config-routes-is-not-pure-ruby/</link><pubDate>Fri, 03 Jan 2014 06:19:02 +0000</pubDate><guid>https://hack.ironsand.net/2014/config-routes-is-not-pure-ruby/</guid><description>&lt;pre class="lang:ruby decode:true " >scope "api" do
resources :entries
end&lt;/pre>
&lt;p>このコードをワンラインで、&lt;/p>
&lt;pre class="lang:ruby decode:true " >scope "api" { resources :entries }&lt;/pre>
&lt;p>とかくと動かないので、routes.rb は DSLなのでブロックの書き方に影響が出てるようです。何でこうしてるかの詳しい理由はよくわからない。&lt;/p></description></item><item><title>Maverickで`rails new` が `atomic`のインストールでこける時の対処法</title><link>https://hack.ironsand.net/2014/fail-to-bundle-install-in-maverick/</link><pubDate>Fri, 03 Jan 2014 04:07:31 +0000</pubDate><guid>https://hack.ironsand.net/2014/fail-to-bundle-install-in-maverick/</guid><description>&lt;p>&lt;code>rails new&lt;/code> したらbundle install が &lt;code>atomic&lt;/code> の所でコケた。何か前も同じエラーに引っかかって結構検索した記憶があったんだけど今回も対処法を探すのに結構な時間がかかってしまった。&lt;/p>
&lt;p>エラーメッセージはこんなの。&lt;/p>
&lt;pre class="lang:sh decode:true " >run bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies.........
Installing rake (10.1.1)
Installing i18n (0.6.9)
Installing minitest (4.7.5)
Installing multi_json (1.8.2)
Installing atomic (1.1.14)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/ironsand/.rbenv/versions/2.0.0-p247/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/ironsand/.rbenv/versions/2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:430:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:515:in `try_link0'
from /Users/ironsand/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:813:in `try_run'
from extconf.rb:26:in `&amp;lt;main&amp;gt;'
Gem files will remain installed in /Users/ironsand/dev/raffler/vendor/bundle/gems/atomic-1.1.14 for inspection.
Results logged to /Users/ironsand/dev/raffler/vendor/bundle/gems/atomic-1.1.14/ext/gem_make.out
An error occurred while installing atomic (1.1.14), and Bundler cannot continue.
Make sure that `gem install atomic -v '1.1.14'` succeeds before bundling.
ironsand@Air-2 dev$ gem update
&lt;/pre>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>エラーメッセージからだと全くわからないんだけれども、どうやらこれは Xcodeのバージョンをアップとか新規インストールした時に一度立ち上げて利用規約にOKと答えておかないとコンパイラーが使えずにでるらしい。&lt;/p></description></item><item><title>Zurb Foundation のトップバーの右上のメニューを消えないようにする</title><link>https://hack.ironsand.net/2014/zurb-foundation-top-bar-menu/</link><pubDate>Wed, 01 Jan 2014 14:05:29 +0000</pubDate><guid>https://hack.ironsand.net/2014/zurb-foundation-top-bar-menu/</guid><description>&lt;p>Zurb Foundationのトップバーの右上のメニューですが、デフォルトでは幅が940pxを切るとMenuに折りたたまれてしまうという結構広い設定になっています。&lt;/p>
&lt;p>もうちょっと幅が狭くても出るようにしたかったので修正しました。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>設定ファイルを以下のように書き換えます。環境はRailsです。&lt;/p>
&lt;pre class="lang:sass decode:true " title="app/assets/stylesheets/foundation_and_overrides.scss" >// $topbar-breakpoint: 940 !default; // Change to 9999px for always mobile layout
$topbar-breakpoint: emCalc(600px); &lt;/pre>
&lt;p>ちなみに[Menu]自体がでなくて少し困ったのですが&lt;code>toggle-topbar menu-icon&lt;/code> を設定してなかったのが原因だったようです。&lt;/p>
&lt;pre class="lang:xhtml decode:true " title="app/views/layouts/_header.html.erb" >&amp;lt;ul class="title-area"&amp;gt;
&amp;lt;li class="name"&amp;gt;
&amp;lt;h1&amp;gt;&amp;lt;%= link_to "Site Title", root_path %&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/h1&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class="toggle-topbar menu-icon"&amp;gt;&amp;lt;a href="#"&amp;gt;&amp;lt;span&amp;gt;Menu&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/pre></description></item><item><title>since_id parameter is invalid. と言われた時の対処法</title><link>https://hack.ironsand.net/2013/how-to-treat-since_id-parameter-is-invalid/</link><pubDate>Fri, 27 Dec 2013 19:14:15 +0000</pubDate><guid>https://hack.ironsand.net/2013/how-to-treat-since_id-parameter-is-invalid/</guid><description>&lt;p>Railscasts のTwitter の項目通りに書いてると &lt;code>since_id parameter is invalid.&lt;/code> のエラーでつまづきました。&lt;/p>
&lt;p>どうやらTwitterAPI1.1からの変更で &lt;code>since_id&lt;/code> に `` を指定できなくなったのが原因のようです。&lt;/p>
&lt;p>もちろんレコードにすでにつぶやきが入ってれば問題ないんですが初回の何も入ってない時にこけてしまうわけです。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;pre class="lang:ruby decode:true " >user.twitter.list_timeline(list_id, since_id: [maximum(:tweet_id),"1"].max)&lt;/pre>
&lt;p>と返り値に最低でも&lt;code>1&lt;/code>が入るようにしておけばOKです。文字列にしてるのは &lt;code>twitter_id&lt;/code>を文字列で保持してるからです。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[API v1.1 statuses/user_timeline since_id parameter is invalid | Twitter Developers][1]&lt;/p></description></item><item><title>attr_accessible は Rails4では使えなくなりました</title><link>https://hack.ironsand.net/2013/attr_accessible-is-no-more-available-in-rails4/</link><pubDate>Mon, 23 Dec 2013 09:33:40 +0000</pubDate><guid>https://hack.ironsand.net/2013/attr_accessible-is-no-more-available-in-rails4/</guid><description>&lt;p>&lt;code>attr_accessible&lt;/code>はRails4では使えなくなったのにユーザー管理の定番 gem &lt;code>devise&lt;/code>ではまだ使っているようなので新しい昨日の Strong parameter に書き換えておきましょう。&lt;/p>
&lt;h3 id="具体例">具体例&lt;/h3>
&lt;p>devise の設定で User モデルにこんなのが書いてると思うので&lt;/p>
&lt;pre class="lang:ruby decode:true " >attr_accessible :email, :password, :password_confirmation&lt;/pre>
&lt;p>その行を削除して下の方にこれを追加しましょう。&lt;/p>
&lt;pre class="lang:ruby decode:true " title="app/models/user.rb" >private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[attr_accessible][1]&lt;/p></description></item><item><title>OmniauthとDeviseの設定ファイルを両方同時に使うとルーティングがおかしくなる</title><link>https://hack.ironsand.net/2013/must-not-use-omniauthdevise-config-file-sametime/</link><pubDate>Mon, 23 Dec 2013 09:28:29 +0000</pubDate><guid>https://hack.ironsand.net/2013/must-not-use-omniauthdevise-config-file-sametime/</guid><description>&lt;p>何故だか理由はさっぱりわかりませんが、どうにもおかしな動きが &lt;code>route.rb&lt;/code> をいくらいじってもなおらないので色々情報を探してたら、config の &lt;code>omniauth.rb&lt;/code> を消したら動きました。&lt;/p>
&lt;p>consumer_key とかは devise.rb に書いておきましょう。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails 3 – Authentication failure : Devise + OmniAuth + Twitter – Stack Overflow][1]&lt;/p></description></item><item><title>Omniauth+DeviseでTwitterとかでログインもできるし連携もできるサンプル</title><link>https://hack.ironsand.net/2013/social-login-in-rails/</link><pubDate>Mon, 23 Dec 2013 08:39:04 +0000</pubDate><guid>https://hack.ironsand.net/2013/social-login-in-rails/</guid><description>&lt;p>&lt;a href="https://github.com/mohitjain/social-login-in-rails">https://github.com/mohitjain/social-login-in-rails&lt;/a>&lt;/p>
&lt;p>ここにあります。&lt;/p></description></item><item><title>Railsで ‘not null’ の制限をマイグレーションで取り除く方法</title><link>https://hack.ironsand.net/2013/remove-constraints-from-a-user-model-in-rails/</link><pubDate>Mon, 23 Dec 2013 05:25:01 +0000</pubDate><guid>https://hack.ironsand.net/2013/remove-constraints-from-a-user-model-in-rails/</guid><description>&lt;p>devise と omniauthを組みわせて使うと default の &lt;code>email&lt;/code> と &lt;code>encrypted_password&lt;/code> が必ず値を持たなければならない &lt;code>null =&amp;gt; false&lt;/code> の制限が邪魔だったのでMigrationで削除しようとしたけど方法がわからなかったので調べてみた。&lt;/p>
&lt;h3 id="やりかた">やりかた&lt;/h3>
&lt;pre class="lang:ruby decode:true " >class RemoveConditionFromUser &amp;lt; ActiveRecord::Migration
def change
change_column :users, :email, :string, null: true
change_column :users, :encrypted_password, :string, null: true
end
end&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Rails Migration: Remove constraint – Stack Overflow][1]&lt;/p></description></item><item><title>Rails開発してる私が使ってるgemの一覧</title><link>https://hack.ironsand.net/2013/gem-list-for-rails/</link><pubDate>Fri, 20 Dec 2013 02:10:27 +0000</pubDate><guid>https://hack.ironsand.net/2013/gem-list-for-rails/</guid><description>&lt;p>Macの環境構築をやり直したので、入れてるgemの一覧を出してみました。&lt;/p>
&lt;pre class="lang:default decode:true " title="`gem list`の出力結果" >actionmailer (4.0.0)
actionpack (4.0.0)
activemodel (4.0.0)
activerecord (4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.0)
addressable (2.3.5)
arel (4.0.0)
atomic (1.1.14)
bigdecimal (1.2.0)
buftok (0.2.0)
builder (3.1.4)
bundler (1.3.5)
celluloid (0.15.2)
chunky_png (1.2.8)
coderay (1.1.0)
compass (0.12.2)
compass-normalize (1.4.3)
compass-recipes (0.3.0)
descendants_tracker (0.0.3)
diff-lcs (1.2.5)
equalizer (0.0.8)
erubis (2.7.0)
factory_girl (4.3.0)
factory_girl_rails (4.3.0)
faker (1.2.0)
faraday (0.8.8)
ffi (1.9.3)
formatador (0.2.4)
fssm (0.2.10)
guard (2.2.4)
guard-rspec (4.2.0)
hike (1.2.3)
http (0.5.0)
http_parser.rb (0.5.3)
i18n (0.6.5)
io-console (0.4.2)
json (1.8.1, 1.7.7)
listen (2.2.0)
lumberjack (1.0.4)
mail (2.5.4)
memoizable (0.2.0)
method_source (0.8.2)
mime-types (1.25)
minitest (4.3.2)
multi_json (1.8.0)
multipart-post (1.2.0)
polyglot (0.3.3)
pry (0.9.12.4)
psych (2.0.0)
rack (1.5.2)
rack-test (0.6.2)
rails (4.0.0)
railties (4.0.0)
rake (10.1.0, 0.9.6)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
rcodetools (0.8.5.0)
rdoc (4.0.0)
rspec (2.14.1)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
rspec-mocks (2.14.4)
rubygems-update (2.1.5)
sass (3.2.12)
simple_oauth (0.2.0)
slop (3.4.7)
spork (0.9.2)
sprockets (2.10.0)
sprockets-rails (2.0.0)
test-unit (2.0.0.0)
thor (0.18.1)
thread_safe (0.1.3)
tilt (1.4.1)
timers (1.1.0)
treetop (1.4.15)
twitter (5.1.1)
tzinfo (0.3.37)&lt;/pre>
&lt;p>なので、だいたい名前のわかるやつだけ入れることにします。わかんないのは依存関係のgemか全く使ってないgemなので必要になったらその時に入れることにします。&lt;/p></description></item><item><title>create_or_update 的なメソッドをRailsで使いたいとき</title><link>https://hack.ironsand.net/2013/create_or_update-method-in-rails/</link><pubDate>Sun, 15 Dec 2013 13:50:02 +0000</pubDate><guid>https://hack.ironsand.net/2013/create_or_update-method-in-rails/</guid><description>&lt;p>既に該当するモデルの値が存在していたら update で値を更新して、まだ存在しない場合は create で新しくモデルのインスタンスを作成したい時ありませんか？ 少なくとも自分にはありましたし、検索でここに辿り着いたあなたもきっとあると思います。&lt;/p>
&lt;p>Railsならきっと簡単な方法があると思ったので調べてみたらありました。&lt;/p>
&lt;h3 id="やり方">やり方。&lt;/h3>
&lt;p>こんな感じでOK。&lt;/p>
&lt;pre class="lang:ruby decode:true " >my_class = ClassName.find_or_initialize_by_name(name)
my_class.update_attributes(
:street_address =&amp;gt; self.street_address,
:city_name =&amp;gt; self.city_name,
:federalid =&amp;gt; self.federalid,
:state_prov_id =&amp;gt; self.state_prov_id,
:zip_code =&amp;gt; self.zip_code
)&lt;/pre>
&lt;h3 id="注意点">注意点&lt;/h3>
&lt;p>ちなみに &lt;code>create_or_update&lt;/code> は &lt;code>ActiveRecord::Callbacks&lt;/code> の private 関数として用意されてるご様子。&lt;/p>
&lt;p>[create_or_update (ActiveRecord::Callbacks) – APIdock][1]&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – create_or_update method in rails – Stack Overflow][2]&lt;/p></description></item><item><title>respond_to を ブロックを使わずに書く。</title><link>https://hack.ironsand.net/2013/respond_to_without_block_in_rails/</link><pubDate>Sun, 15 Dec 2013 09:29:01 +0000</pubDate><guid>https://hack.ironsand.net/2013/respond_to_without_block_in_rails/</guid><description>&lt;p>Ajaxの処理をRailsでするときはControllerに&lt;/p>
&lt;pre class="lang:ruby decode:true " >respond_to do |format|
format.js
end&lt;/pre>
&lt;p>と書いてたんですが、いちいちブロック引数に format を使って処理するのは無駄な気がしたので一行で書ける方法を調べてみた。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;pre class="lang:ruby decode:true " >respond_to :js&lt;/pre>
&lt;p>でOKでした。&lt;/p>
&lt;pre class="lang:ruby decode:true " >respond_to :html, :js&lt;/pre>
&lt;p>な書き方もOKなようです。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[respond_to (ActionController::MimeResponds::InstanceMethods) – APIdock][1]&lt;/p></description></item><item><title>Gemfileを開いてたらsmart-compileで`bundle install`を実行する</title><link>https://hack.ironsand.net/2013/run-bundle-install-to-gemfile-with-smart-compile-in-emacs/</link><pubDate>Sun, 15 Dec 2013 07:33:43 +0000</pubDate><guid>https://hack.ironsand.net/2013/run-bundle-install-to-gemfile-with-smart-compile-in-emacs/</guid><description>&lt;p>開いてるファイルをそのまま実行できる&lt;code>smart-compile&lt;/code>でGemfileを開いてたら&lt;code>bundle install&lt;/code>を実行したかったのでやり方を調べてみました。&lt;/p>
&lt;h3 id="設定方法">設定方法&lt;/h3>
&lt;p>どうやらsmart compile は自動的にコマンドの実行ディレクトリをファイルがある場所に移動してくれるようなので単純に &lt;code>Gemfile&lt;/code>というファイル名と&lt;code>bundle install&lt;/code>のコマンドを他のファイルと同じように紐付ければOKのようです。&lt;/p>
&lt;pre class="lang:lisp decode:true " title="~/.emacs.c/inits/30-smart-compile.el" >(global-set-key (kbd "C-c C-x") 'smart-compile)
(setq smart-compile-alist
(append
'(("\\.rb$" . "ruby %f"))
'(("\\.php$" . "php %f"))
'(("\Gemfile$" . "bundle install"))
smart-compile-alist))&lt;/pre>
&lt;p>もちろん ruby や php を使ってなかったら該当行は必要ありません。&lt;/p>
&lt;h3 id="使い方">使い方&lt;/h3>
&lt;p>これで Gemfile を開いて &lt;code>C-c C-x&lt;/code> するとこんな風に &lt;code>bundle install&lt;/code> が実行されます。&lt;/p>
&lt;pre class="lang:ps decode:true " >-*- mode: compilation; default-directory: "~/dev/foo/" -*-
Compilation started at Sun Dec 15 16:29:42
bundle install
Using rake (10.1.0)
Using i18n (0.6.9) from git://github.com/svenfuchs/i18n.git (at master)
Using minitest (4.7.5)
Using multi_json (1.8.2)
Using atomic (1.1.14)
Using thread_safe (0.1.3)
Using tzinfo (0.3.38)
Using activesupport (4.0.0)
Using builder (3.1.4)
Using erubis (2.7.0)
Using rack (1.5.2)
Using rack-test (0.6.2)
Using actionpack (4.0.0)
Using mime-types (1.25.1)
Using polyglot (0.3.3)
Using treetop (1.4.15)
Using mail (2.5.4)
Using actionmailer (4.0.0)
Using activemodel (4.0.0)
Using activerecord-deprecated_finders (1.0.3)
Using arel (4.0.1)
Using activerecord (4.0.0)
Using sass (3.2.12)
Using bootstrap-sass (3.0.3.0)
Using will_paginate (3.0.5)
Using bootstrap-will_paginate (0.0.10)
Using mini_portile (0.5.2)
Using nokogiri (1.6.0)
Using xpath (2.0.0)
Using capybara (2.2.0)
Using timers (1.1.0)
Using celluloid (0.15.2)
Using ffi (1.9.3)
Using childprocess (0.3.9)
Using coderay (1.1.0)
Using coffee-script-source (1.6.3)
Using execjs (2.0.2)
Using coffee-script (2.2.0)
Using thor (0.18.1)
Using railties (4.0.0)
Using coffee-rails (4.0.1)
Using diff-lcs (1.2.5)
Using factory_girl (4.3.0)
Using factory_girl_rails (4.3.0)
Using faker (1.2.0)
Using formatador (0.2.4)
Using friendly_id (5.0.2)
Using rb-fsevent (0.9.3)
Using rb-inotify (0.9.2)
Using listen (2.4.0)
Using lumberjack (1.0.4)
Using method_source (0.8.2)
Using slop (3.4.7)
Using pry (0.9.12.4)
Using guard (2.2.4)
Using rspec-core (2.14.7)
Using rspec-expectations (2.14.4)
Using rspec-mocks (2.14.4)
Using rspec (2.14.1)
Using guard-rspec (4.2.0)
Using spork (1.0.0rc4)
Using guard-spork (1.5.1)
Using hike (1.2.3)
Using jbuilder (1.5.3)
Using jquery-rails (3.0.4)
Using json (1.8.1)
Using bundler (1.3.5)
Using tilt (1.4.1)
Using sprockets (2.10.1)
Using sprockets-rails (2.0.1)
Using rails (4.0.0)
Using rdoc (3.12.2)
Using rspec-rails (2.14.0)
Using sass-rails (4.0.1)
Using sdoc (0.3.20)
Using spork-rails (4.0.0)
Using sqlite3 (1.3.8)
Using turbolinks (2.0.0)
Using uglifier (2.3.3)
[32mYour bundle is complete![0m
[32mIt was installed into ./vendor/bundle[0m
Compilation finished at Sun Dec 15 16:29:45
&lt;/pre></description></item><item><title>Railsのカラム名の語頭に数字を使ってはいけない</title><link>https://hack.ironsand.net/2013/do-not-use-number-for-column-name-in-rails/</link><pubDate>Sat, 14 Dec 2013 08:08:39 +0000</pubDate><guid>https://hack.ironsand.net/2013/do-not-use-number-for-column-name-in-rails/</guid><description>&lt;p>Railsの学習がてらに2chもどきをつくろうと思って `rails new 2ch` してみるとエラーになる。これはいいんだけど、`rails generate Model 2ch` ってエラーを吐かないのに実際は使えないので修正がめんどくさくなるので気をつけましょう。&lt;/p></description></item><item><title>[deprecated] RailsでI18n.enforce_available_locales とエラーが出る時の対処法</title><link>https://hack.ironsand.net/2013/deprecated-i18n-enforce_available_locales/</link><pubDate>Fri, 13 Dec 2013 15:50:45 +0000</pubDate><guid>https://hack.ironsand.net/2013/deprecated-i18n-enforce_available_locales/</guid><description>&lt;p>Railsでこんなdeprecatedの警告が出てきたので対処方法を調べた。&lt;/p>
&lt;pre class="lang:default decode:true " title="lib/taskの中のファイルを実行した時のエラー" >[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.&lt;/pre>
&lt;h3 id="対策">対策&lt;/h3>
&lt;pre class="lang:ruby decode:true " title="config/application.rb" >config.i18n.enforce_available_locales = true&lt;/pre>
&lt;p>でOK, `locale validation` をきちんとするなら true、どうでも良いならfalseらしい。よくわかんない。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby – Rails I18n validation deprecation warning – Stack Overflow][1]&lt;/p></description></item><item><title>link_to で内部にタグがあったりする時はブロックを使いましょう</title><link>https://hack.ironsand.net/2013/link_to-with-block/</link><pubDate>Wed, 11 Dec 2013 06:54:04 +0000</pubDate><guid>https://hack.ironsand.net/2013/link_to-with-block/</guid><description>&lt;p>Zurb-Foundation とか Twitter bootstrap を使ってると、アイコンをリンク内に含めたくなったりしますよね。こんな感じに&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>でもこれをそのまま `link_to` で囲んで&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= link_to &amp;ldquo;&lt;i class='general foundicon-refresh'>&lt;/i> Reload&amp;rdquo;,&amp;ldquo;&lt;a href="http://example.com/reload%22">http://example.com/reload&amp;quot;&lt;/a> %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>とすると`&lt;i>`タグが HTML エスケープされて悲しい事態になるんです。&lt;/p>
&lt;h3 id="対策">対策&lt;/h3>
&lt;p>なのでそういう時は `link_to` さんはブロックが取れるのでブロックを使って処理しましょう。ブロックの返り値がリンクのテキストになります。&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= link_to &amp;ldquo;&lt;a href="http://example.com/reload%22">http://example.com/reload&amp;quot;&lt;/a>, class: &amp;lsquo;reload&amp;rsquo; do %&amp;gt;&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>&amp;lt;% end %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>ちなみに `button_to` も同じような関数なのでこういう風にかけます。&lt;/p>
&lt;p>[crayon lang=&amp;ldquo;html&amp;rdquo;]&lt;/p>
&lt;p>&amp;lt;%= button_to &amp;ldquo;&amp;rdquo;, class: &amp;lsquo;small radius&amp;rsquo; do %&amp;gt;&lt;/p>
&lt;p>&lt;i class='general foundicon-refresh'>&lt;/i> Reload&lt;/p>
&lt;p>&amp;lt;% end %&amp;gt;&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;p>これ知らない時に必死に HTML エスケープされないために何をすればよいのかを探しまくったのは今では良い思い出でもなんでもありません。時間返せ。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – link_to in helper with block – Stack Overflow][1]&lt;/p></description></item><item><title>既に存在するindexにunique制限をかけるmigrationの書き方。</title><link>https://hack.ironsand.net/2013/how-to-add-unique-constraint-to-already-existing-index-by-migration/</link><pubDate>Mon, 09 Dec 2013 10:22:53 +0000</pubDate><guid>https://hack.ironsand.net/2013/how-to-add-unique-constraint-to-already-existing-index-by-migration/</guid><description>&lt;p>そのまま unique 条件付の index を追加しようとするとエラーになるので&lt;/p>
&lt;p>まず既にある index を削除してから追加しましょう。&lt;/p>
&lt;p>[crayon title=&amp;ldquo;db/migrate/add_unique_constraint.rb&amp;rdquo;]&lt;/p>
&lt;p>def change&lt;/p>
&lt;p>remove_index :editabilities, [:user_id, :list_id]&lt;/p>
&lt;p>add_index :editabilities, [:user_id, :list_id], unique: true&lt;/p>
&lt;p>end&lt;/p>
&lt;p>[/crayon]&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[ruby on rails – How to add &lt;code>unique&lt;/code> constraint to already existing index by migration – Stack Overflow][1]&lt;/p></description></item><item><title>RailsでModelが不規則名詞だった場合のControllerの命名規則</title><link>https://hack.ironsand.net/2013/naming-convention-rails-imodel-irregular-verb/</link><pubDate>Tue, 21 May 2013 07:35:49 +0000</pubDate><guid>https://hack.ironsand.net/2013/naming-convention-rails-imodel-irregular-verb/</guid><description>&lt;p>Rails の Model は常に単数名詞、そして Controller はその名詞の複数形という規則がありますが、Model が”Child&amp;quot;などの不規則変化名詞だった時に Controller 名は&lt;strong>Children なのか、Childs なのかがわからなかった&lt;/strong>ので調べました。&lt;/p>
&lt;h3 id="答え">答え&lt;/h3>
&lt;p>プログラム的には当然簡単に処理できる Childs だと思ったけども実際はちゃんと不規則変化にも対応してるらしく Children が正解です。&lt;/p>
&lt;h3 id="その他の不規則名詞">その他の不規則名詞&lt;/h3>
&lt;p>他の不規則名詞は&lt;/p>
&lt;p>&lt;a href="https://github.com/rails/rails/blob/4-0-0/activesupport/lib/active_support/inflections.rb">https://github.com/rails/rails/blob/4-0-0/activesupport/lib/active_support/inflections.rb&lt;/a>&lt;/p>
&lt;p>に載っているんですが、&lt;/p>
&lt;pre class="wrap:true lang:default decode:true " >inflect.irregular('person', 'people')
inflect.irregular('man', 'men')
inflect.irregular('child', 'children')
inflect.irregular('sex', 'sexes')
inflect.irregular('move', 'moves')
inflect.irregular('cow', 'kine')
inflect.irregular('zombie', 'zombies')
&lt;/pre>
&lt;p>zombie って不規則名詞なの…？&lt;/p>
&lt;p>cow→kine の変化はそもそも英単語がわかりませんでした、、&lt;/p>
&lt;h3 id="zombie-が不規則名詞の理由-20131214-追記">Zombie が不規則名詞の理由 ([2013/12/14] 追記)&lt;/h3>
&lt;p>単数形から複数形への変化は規則通りですが、複数形から単数形への変化 zombies -&amp;gt; zombie が不規則で、規則通り変化させてしまうと zombies -&amp;gt; zomby になってしまうから不規則名詞らしいです。英語は難しいですね。&lt;/p>
&lt;h3 id="情報元">情報元&lt;/h3>
&lt;p>[Model and Controller's naming rule for irregular noun in Rails – Stack Overflow][1]&lt;/p></description></item><item><title>Railsで新規プロジェクトを作ろうとすると`expand_path’: non-absolute homeというエラーが出る</title><link>https://hack.ironsand.net/2013/expand_path-non-absolute-home-error/</link><pubDate>Wed, 01 May 2013 03:12:23 +0000</pubDate><guid>https://hack.ironsand.net/2013/expand_path-non-absolute-home-error/</guid><description>&lt;p>&lt;code>rails new hoge&lt;/code>&lt;/p>
&lt;p>とすると&lt;/p>
&lt;p>&lt;code>`expand_path': non-absolute home&lt;/code>&lt;/p>
&lt;p>というエラーが出てしまって「Windowsだしなあ。」と諦めかけてたけど、このhomeというのは環境変数のHOMEのことらしく、確かにcygwinを使う時のためにHOMEに /cygdrive/ から始まる値を入れてたのを思い出したので環境変数 HOME を削除してみたら動いた。&lt;/p>
&lt;p>ちなみに irb もHOMEに入ってる値によっては動かないらしい。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>`expand_path': non-absolute home&lt;br>
&lt;a href="http://devnet.jetbrains.com/thread/437313">http://devnet.jetbrains.com/thread/437313&lt;/a>&lt;/p></description></item><item><title>JqueryとHTML5でajaxなファイルのアップロード</title><link>https://hack.ironsand.net/2013/ajax-file-upload-by-jquery-and-html5/</link><pubDate>Wed, 27 Feb 2013 09:16:07 +0000</pubDate><guid>https://hack.ironsand.net/2013/ajax-file-upload-by-jquery-and-html5/</guid><description>&lt;p>画面遷移無しでファイルのアップロードしようと思ったらiframeを使った技とかややこしい情報が色々出てきて、なかなかやりたいことに辿りつけなかったのでメモ。&lt;/p>
&lt;h3 id="やりたい事と条件">やりたい事と条件&lt;/h3>
&lt;p>・ajaxなテキストファイルのアップロード&lt;/p>
&lt;p>・テキストをRubyで編集して返す&lt;/p>
&lt;p>・画面遷移せずに結果を表示&lt;/p>
&lt;p>・IEは諦める。&lt;/p>
&lt;h3 id="必要最低限のコード">必要最低限のコード&lt;/h3>
&lt;p>まずHTMLを用意&lt;/p>
&lt;p>&lt;strong>ajaxupload.html&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>&lt;html>&lt;br>
&lt;head>&lt;br>
&lt;meta http-equiv="content-type" content="text/html;charset=utf-8">&lt;br>
&lt;script type="text/javascript" src="jquery-1.7.min.js" charset="utf8">&lt;/script>&lt;br>
&lt;pre>&lt;code>&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;ajaxupload.js&amp;quot; charset=&amp;quot;utf8&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;
&lt;/code>&lt;/pre>
&lt;/head>&lt;br>
&lt;style>textarea{height:80%;width:100%;}&lt;/style>&lt;br>
&lt;body>&lt;br>
&lt;input type=file id=file />&lt;br>
&lt;input type=button id=upload value="upload">&lt;br>&lt;br>
&lt;textarea id=output>&lt;/textarea>&lt;br>
&lt;/body>&lt;br>
&lt;/html>&lt;br>
&lt;/pre>
&lt;p>`
&lt;p>つぎにjavascript&lt;/p>
&lt;p>&lt;strong>ajaxupload.js&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>$(function(){
$("#upload").click(function(){
var file = $("#file")[0].files[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = showFile;
});
function showFile(event) {
$.ajax({
type: "POST",
url: "ajaxupload.rb",
data: { filedata:event.target.result}
}).done(function(msg) {
$("#output").html(msg);
});
}
});
&lt;/pre>
&lt;p>`
&lt;p>んで何か処理をするRubyのスクリプト。ここではファイルの中身をそのまま返してるだけ。(パス名は適宜変更してください。)&lt;/p>
&lt;p>&lt;strong>ajaxupload.rb&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>#!P:\Dropbox\bin\RailsPortable\App\Rails\bin\ruby.exe
require "cgi"
cgi = CGI.new
print "Content-Type: text/html\n\n"
print cgi['filedata'][0]&lt;/pre>
&lt;p>`
&lt;h3 id="デモ">デモ&lt;/h3>
&lt;p>サンプルを置いておきます。(さくらのレンタルサーバーはRubyの拡張子がrbではなくcgiじゃなくと動かなかったのでファイル名を変えています。&lt;/p></description></item><item><title>VimでRubyのインデント設定を変える</title><link>https://hack.ironsand.net/post/2012/2012-07-11-vim%E3%81%A7ruby%E3%81%AE%E3%82%A4%E3%83%B3%E3%83%87%E3%83%B3%E3%83%88%E8%A8%AD%E5%AE%9A%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/</link><pubDate>Wed, 11 Jul 2012 23:54:00 +0900</pubDate><guid>https://hack.ironsand.net/post/2012/2012-07-11-vim%E3%81%A7ruby%E3%81%AE%E3%82%A4%E3%83%B3%E3%83%87%E3%83%B3%E3%83%88%E8%A8%AD%E5%AE%9A%E3%82%92%E5%A4%89%E3%81%88%E3%82%8B/</guid><description>&lt;p>Lionでgvimをインストールしてrubyのスクリプトを書きはじめてみると&lt;br>
インデント幅が自分には大きすぎて、それにテキスト全体にインデントを適用する方法がわからなかったので調べました。&lt;/p></description></item><item><title>Macでhttpdを動かす</title><link>https://hack.ironsand.net/post/2012/2012-07-11-mac%E3%81%A7httpd%E3%82%92%E5%8B%95%E3%81%8B%E3%81%99/</link><pubDate>Wed, 11 Jul 2012 09:42:00 +0900</pubDate><guid>https://hack.ironsand.net/post/2012/2012-07-11-mac%E3%81%A7httpd%E3%82%92%E5%8B%95%E3%81%8B%E3%81%99/</guid><description>&lt;p>rubyやpythonで書いたCGIを動かすためにやっぱりローカルでもテスト環境がほしいのでMacで使えるhttpdを探したら、BSDが元になってるのではじめからApacheが入ってるらしい。&lt;/p>
&lt;p>「システム環境設定」→「共有」→「Web共有」をONにするだけでホームフォルダのしたに「サイト」というフォルダが自動的に作成されて、そこに http://localhost/~user/ でアクセスできるようになる様子。こりゃ楽だ。ひゃほーい。&lt;/p>
&lt;p>全体の公開フォルダは&lt;br>
/Library/WebServer/Documents&lt;br>
/Library/WebServer/CGI-Executables&lt;br>
の2つになるらしい。&lt;/p>
&lt;p>とはいえこんなフォルダを使いたくないのでホームフォルダの下でCGIが実行できるように変更します。&lt;/p>
&lt;p>&lt;code>sudo vi /private/etc/apache2/users/[ユーザ名].conf&lt;/code>&lt;/p>
&lt;p>&lt;code>で設定ファイルを開いて&lt;/code>&lt;/p>
&lt;p>&lt;code>&amp;lt;Directory &amp;quot;/Users/[ユーザ名]/Sites/&amp;quot;&amp;gt;     Options All     AllowOverride All     Order allow,deny     Allow from all     AddHandler cgi-script .cgi .rb .py &amp;lt;/Directory&amp;gt;&lt;/code>&lt;/p>
&lt;p>のように書き換えて、&lt;/p>
&lt;p>「Web共有」の設定を一旦切ってまたONにしてApacheを再起動。&lt;/p>
&lt;p>&lt;code>cd Sites&lt;/code>&lt;br>
&lt;code>vi test.rb&lt;/code>&lt;/p>
&lt;p>で「サイト」フォルダの下に新しいファイルを作って、その中に&lt;/p>
&lt;p>&lt;code>#!/usr/bin/ruby&lt;/code>&lt;br>
&lt;code>print &amp;quot;Content-Type: text/plain\n\n&amp;quot;&lt;/code>&lt;br>
&lt;code>print &amp;quot;hello&amp;quot;&lt;/code>&lt;/p>
&lt;p>と書いて&lt;/p>
&lt;p>&lt;code>chmod 705 test.rb&lt;/code>&lt;/p>
&lt;p>でファイルを実行可能にしてブラウザで&lt;/p>
&lt;p>http://localhost/~[ユーザ名]/test.rb&lt;/p>
&lt;p>で test と表示されれば成功です。&lt;/p>
&lt;p>何故か途中から敬語になってますが、&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>Apacheを起動する - Mac OS X LeopardでAMP環境を構築しよう - Apache, MySQL, PHP, Perlで作る開発環境 - futomi&amp;rsquo;s CGI Cafe&lt;br>
&lt;a href="http://www.futomi.com/lecture/macosx/apache.html">http://www.futomi.com/lecture/macosx/apache.html&lt;/a>&lt;/p>
&lt;p>CGI動作環境の構築 - Mac OS X LeopardでLAMP環境を構築しよう - Apache, MySQL, PHP, Perlで作る開発環境 - futomi&amp;rsquo;s CGI Cafe&lt;br>
&lt;a href="http://www.futomi.com/lecture/macosx/cgi.html">http://www.futomi.com/lecture/macosx/cgi.html&lt;/a>&lt;/p></description></item><item><title>NHK語学のラジオ講座をさくらのVPSで自動ダウンロード その2</title><link>https://hack.ironsand.net/2012/upload-mp3-file-to-sugarsync-by-sakura-vps/</link><pubDate>Wed, 06 Jun 2012 05:08:52 +0000</pubDate><guid>https://hack.ironsand.net/2012/upload-mp3-file-to-sugarsync-by-sakura-vps/</guid><description>&lt;p>前回の記事の続きです。&lt;/p>
&lt;p>NHK 語学のラジオ講座をさくらの VPS で自動ダウンロード&lt;/p>
&lt;p>&lt;a href="http://hack.ironsand.net/2012/auto-download-nhk-language-mp3-by-vps/">http://hack.ironsand.net/2012/auto-download-nhk-language-mp3-by-vps/&lt;/a>&lt;/p>
&lt;p>今回は&lt;strong>さくら VPS にダウンロードした mp3 を Sugarsync にアップロードする&lt;/strong>について解説します。&lt;/p>
&lt;p>さくらの VPS 上に SugarSync や Dropbox を常駐させるのはできるのかどうかわかりませんでしたし、できてもメモリを食いそうなので今回は SugarSync が提供する API を使用します。&lt;/p>
&lt;p>自動的に SugarSync にファイルをアップロードさせるための手順は以下の流れになります。&lt;/p>
&lt;ol>
&lt;li>
&lt;p>API を使うために SugarSync の Developer に登録&lt;/p>
&lt;/li>
&lt;li>
&lt;p>upload_nhkradio.rb の設定&lt;/p>
&lt;/li>
&lt;li>
&lt;p>cron の設定&lt;/p>
&lt;/li>
&lt;/ol>
&lt;h3 id="1-api-を使うために-sugarsync-の-developer-に登録">1. API を使うために SugarSync の Developer に登録&lt;/h3>
&lt;p>まず SugarSync の API を使うために Developer として登録を行います。&lt;/p>
&lt;p>&lt;a href="http://www.sugarsync.com/developer/signup">http://www.sugarsync.com/developer/signup&lt;/a>&lt;/p>
&lt;p>もし Sugarsync のアカウント自体を持ってない方がいたら、まず[こちら][1]から SugarSync 自体のアカウントを作ってください。&lt;/p>
&lt;p>Developer 登録をすると &lt;span style="color:red;">Access Key ID&lt;/span> と &lt;span style="color:red;">Private Access Key&lt;/span> が手に入りますのでメモっていてください。あとで必要になります。&lt;/p>
&lt;p>次に[Create App]から新しいアプリの登録を行いアプリケーションキーを取得してください。&lt;/p>
&lt;p>新しいアプリの作成には以下の 5 つの情報を入力する必要があります。&lt;/p></description></item><item><title>NHK語学のラジオ講座をさくらのVPSで自動ダウンロード</title><link>https://hack.ironsand.net/2012/auto-download-nhk-language-mp3-by-vps/</link><pubDate>Mon, 04 Jun 2012 05:41:35 +0000</pubDate><guid>https://hack.ironsand.net/2012/auto-download-nhk-language-mp3-by-vps/</guid><description>&lt;p>NHK ラジオの語学講座を毎週ダウンロードしてるんですが、&lt;/p>
&lt;p>外国語を勉強してるのは海外に行くためでして、海外だとダウンロードができないことがよくあります。&lt;/p>
&lt;p>なのでせっかくさくらの VPS をお金払って借りてるんだから&lt;/p>
&lt;p>勝手にダウンロードするように設定しましたので参考にどうぞ。&lt;/p>
&lt;p>月曜日の 12 時頃には決めておいたフォルダに mp3 ファイルができてます。&lt;/p>
&lt;p>実際に先週から動かしてみてますが、こりゃ楽でいいです。&lt;/p>
&lt;p>SugarSync にアップロードしてるので海外にいるときに iPhone があるだけで新しい音声を聞くことも出来ます。&lt;/p>
&lt;p>記事が長くなるので今回の&lt;/p>
&lt;p>&lt;strong>さくら VPS を使って自動的に NHK ラジオ講座をダウンロード&lt;/strong>&lt;/p>
&lt;p>次回の&lt;/p>
&lt;p>&lt;strong>さくら VPS にダウンロードした mp3 を Sugarsync にアップロードする&lt;/strong>&lt;/p>
&lt;p>の 2 つにわけて解説していきます。&lt;/p>
&lt;p>自動的に NHK ラジオ講座をダウンロードするには以下の 4 つの手順が必要です。&lt;/p>
&lt;ol>
&lt;li>
&lt;p>flvstreamer のダウンロード、コンパイル&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Ruby のインストール&lt;/p>
&lt;/li>
&lt;li>
&lt;p>CaptureStream の設定&lt;/p>
&lt;/li>
&lt;li>
&lt;p>cron の設定&lt;/p>
&lt;/li>
&lt;/ol>
&lt;h3 id="1-flvstreamer-のダウンロードコンパイル">1. flvstreamer のダウンロード、コンパイル&lt;/h3>
&lt;p>flvstreamer をインストールするんですが、&lt;/p>
&lt;p>まずさくらの VPS が 32 ビットなのか 64 ビットなのか確認します。&lt;/p>
&lt;p>&lt;code>$ cat /proc/cpuinfo | grep flags&lt;/code>&lt;/p>
&lt;p>して値に lm が含まれていれば 64bit の CPU です。&lt;/p></description></item><item><title>rubyでwin32-open3を使う</title><link>https://hack.ironsand.net/2011/win32-open3-with-ruby/</link><pubDate>Tue, 17 May 2011 20:31:40 +0000</pubDate><guid>https://hack.ironsand.net/2011/win32-open3-with-ruby/</guid><description>&lt;p>Rubyで外部のプログラムを起動させて標準エラー出力を得るには open3 を使わないとできないが、 fork() を使ってるためWindowsでは代替の win32-open3 を使って実現する。&lt;/p>
&lt;p>まず gem で win32-open3 をインストール&lt;/p>
&lt;pre>C:\Users\tetsuya>gem install win32-open3
Successfully installed win32-open3-0.3.2-x86-mswin32-60
1 gem installed
Installing ri documentation for win32-open3-0.3.2-x86-mswin32-60...
Installing RDoc documentation for win32-open3-0.3.2-x86-mswin32-60...&lt;/pre>
&lt;p>次にわざとエラを出して標準エラー出力が受け取れるか確認する。&lt;/p>
&lt;pre>require 'rubygems'
require 'win32/open3'
stdin, stdout, stderr = *Open3.popen3('ruby -error')
stderr.each{|line|
print line
}&lt;/pre>
&lt;p>その結果&lt;/p>
&lt;pre>-e:1: undefined local variable or method `rror' for main:Object (NameError)&lt;/pre>
&lt;p>以上。&lt;/p>
&lt;p>外部プログラムでエラーでしかほしい情報を返してくれないやつとかいるのでこれが使えると大変便利。&lt;/p></description></item><item><title>Rubyのスクリプトをサービスとして常駐させる</title><link>https://hack.ironsand.net/2011/how-to-run-ruby-script-as-a-deamon/</link><pubDate>Sat, 07 May 2011 11:46:31 +0000</pubDate><guid>https://hack.ironsand.net/2011/how-to-run-ruby-script-as-a-deamon/</guid><description>&lt;p>Python か Ruby のスクリプトを常駐させたかったので調べてみたら Ruby の方が簡単そうだったので試してみた。&lt;/p>
&lt;h3 id="環境">環境&lt;/h3>
&lt;p>OS:Windows 7&lt;/p>
&lt;p>ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]&lt;/p>
&lt;p>gem 1.3.4&lt;/p>
&lt;p>Portable バージョンを使ってます。&lt;/p>
&lt;h3 id="win32-service-のインストールcgem-install-win32-service">Win32-Service のインストール&lt;pre class=prettyprint>C:&amp;gt;gem install win32-service&lt;/pre>&lt;/h3>
&lt;p>するだけです。&lt;/p>
&lt;p>ただうちの環境ではこんなエラーが出てしまいました。&lt;pre class=prettyprint>P:/Dropbox/bin/RailsPortable/App/Rails/bin/ruby.exe extconf.rb checking for strncpy_s()&amp;hellip; yes creating Makefile nmake Microsoft(R) Program Maintenance Utility Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. P:\Dropbox\bin\RailsPortable\App\Rails\bin\ruby -e &amp;ldquo;puts &amp;lsquo;EXPORTS&amp;rsquo;, &amp;lsquo;Init_api&amp;rsquo;&amp;rdquo; &amp;gt; api-i386-mswin32.def cl -nologo -I. -I. -IP:/Dropbox/bin/RailsPortable/App/Rails/lib/ruby/1.8/i386-mswin32 -I. -MD -Zi -O2b2xg- -G6 -DHAVE_STRNCPY_S -c -Tcwin32/api.c cl : コマンド ライン warning D9035 : オプション &amp;lsquo;Og-&amp;rsquo; の使用は現在推奨されていません。今後のバージョンからは削除されます。 cl : コマンド ライン warning D9002 : 不明なオプション &amp;lsquo;-G6&amp;rsquo; を無視します api.c p:\dropbox\bin\railsportable\app\rails\lib\ruby\1.8\i386-mswin32\config.h(2) : fatal error C1189: #error : MSC version unmatch NMAKE : fatal error U1077: &amp;lsquo;&amp;ldquo;C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.EXE&amp;rdquo;&amp;rsquo; : Stop.&lt;/pre>&lt;/p></description></item></channel></rss>