begin~rescue~endで処理する。 例外のメッセージにパターンがある場合はex.messageが特定文言を含むかどうか調べて人間に優しいメッセージに変更する。

   1    def destroy
   2      @article = Article.find(params[:id])
   3      begin
   4        if @article.destroy
   5          flash[:notice] = '記事を削除しました。'
   6        end
   7      rescue => ex
   8        #「violates foreign key」は外部キーの参照エラー。関連テーブルが存在するため削除できないことを意味する。
   9        if ex.message=~/violates foreign key/
  10          flash[:notice] = '該当記事を参照しているデータがあるため削除できません'
  11        else
  12          #その他の例外
  13          flash[:notice] = ex.message
  14        end
  15      end
  16      redirect_to :action => 'index'
  17    end

posted by Face ysakaki on Tue 12 Aug 2008 at 12:01

Formulaは、 数式をブログにはりつけて共有するコミュニティサービスです。

このたび、FormulaでXyMTeX記法による化学構造式が利用できるようになりました。 以下のような化学構造式を簡単にブログに埋め込むことができるようになります。

XyMTeXの記法については、以下のサイトを参照ください。

See Also

本サービスに関する各種お問い合わせは までお気軽にお寄せください。

posted by Png genki on Mon 11 Aug 2008 at 21:45

Formulaは、 数式をブログにはりつけて共有するコミュニティサービスです。

このたび、Formulaで利用できるLaTeX記法が増えました。 具体的には、eqnarray*環境で利用可能なLaTeX記法をほとんど全て利用することができます(セキュリティ上の観点から、一部のコマンドは無効化されています)

以下のように、複数行の数式を記述することもできるようになりました。

連立方程式も変数を揃えて記述できます。

posted by Png genki on Sun 10 Aug 2008 at 18:22 with 1 comment

Rubyに複数の脆弱性

Rubyに複数の脆弱性が発見されました。最新バージョンへのアップグレードを推奨します。

ということで、EC2で使ってるRubyをruby 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]にアップデートしました。

よろしくお願いします。

posted by Png genki on Fri 8 Aug 2008 at 16:28 with 1 comment

   1  class Parts < ActiveRecord::Base # STI
   2  end
   3  
   4  class Parts::Image < Parts
   5  end
   6  
   7  part = Parts::Image.create!
   8  p part[:type]

で何が返るのかの話。

before 2.1

   1  part[:type]  # => "Image"

after 2.1

   1  part[:type]  # => "Parts::Image"

互換性

2.1 では AR::Base.sti_name メソッドが返す値を利用しており、 AR::Base.store_full_sti_class で制御できる。

   1  Parts::Image.store_full_sti_class  # => true
   2  Parts::Image.sti_name              # => "Parts::Image"
   3  Parts::Image.create[:type]         # => "Parts::Image"
   4  
   5  Parts::Image.store_full_sti_class = false
   6  Parts::Image.sti_name              # => "Image"
   7  Parts::Image.create[:type]         # => "Image"

応用

2.1 のSTIを利用するとナベアツな世界を簡単に作成できる。

   1  class Person < ActiveRecord::Base; end
   2  class Nabe < Person; end
   3  class Aho < Person; end
   4  def Person.sti_name
   5    Time.now.day % 3 == 0 ? "Aho" : "Nabe"
   6  end
   7  
   8  (1..3).map{ Nabe.create[:type] }
   9  => ["Nabe", "Nabe", "Nabe"]
  10  
  11  # Stub
  12  class Time
  13    def day
  14      3
  15    end
  16  end
  17  
  18  (1..3).map{ Nabe.create[:type] }
  19  => ["Aho", "Aho", "Aho"]

posted by Png maiha on Fri 8 Aug 2008 at 13:35

Redmineで2203年の予定を立てようとしたところ、エラーが出たので原因を調べていました。 どうやら、Railsのdistance_of_time_in_wordsでエラーが出ているようです。

   1  def distance_of_time_in_words(
   2      from_time, to_time = 0, include_seconds = false)
   3    from_time = from_time.to_time if
   4      from_time.respond_to?(:to_time)
   5    to_time = to_time.to_time if
   6      to_time.respond_to?(:to_time)
   7    distance_in_minutes =
   8      (((to_time - from_time).abs)/60).round
   9    distance_in_seconds =
  10      ((to_time - from_time).abs).round

8行目あたりの減算でエラーが出ているようです。 実際、以下のような減算をしようとするとエラーが出ます。

   1  Time.now - Date.parse("Mon, 01 Aug 2203").to_time
   2  #=> RangeError: time - 7371010800.000000 out of Time range

ということで、あまりにも長い時間である場合には、 以下のような例外処理で対処するようにしました。

   1  module ActionView
   2    module Helpers
   3      module DateHelper
   4        def distance_of_time_in_words_with_limit(
   5          from_time, to_time = 0, include_seconds = false
   6        )
   7          distance_of_time_in_words_without_limit(
   8            from_time, to_time, include_seconds)
   9        rescue
  10          'a long time'
  11        end
  12        alias_method_chain :distance_of_time_in_words, :limit
  13      end
  14    end
  15  end

これで23世紀の予定も思いのままです。

posted by Png genki on Fri 8 Aug 2008 at 07:10

例外で反応するのをrespondしてるといわないでほしい・・・

   1  irb(main):001:0> 1.respond_to? :dup
   2  => true
   3  irb(main):002:0> 1.dup
   4  TypeError: can't dup Fixnum
   5          from (irb):2:in `dup'
   6          from (irb):2
   7          from :0

posted by Png technohippy on Thu 7 Aug 2008 at 16:47

考えてみりゃそりゃそうなんだけど、

   1  irb(main):001:0> true && nil
   2  => nil
   3  irb(main):002:0> true && false
   4  => false

必ず真偽値を返す演算子があってもいいのにと思った

posted by Png technohippy on Thu 7 Aug 2008 at 12:52 with 2 comments

あるフィルタが定義されているかどうかを知りたいとき、 Controller.find_filter は非常に便利で 1.x 時代から一部で重宝されていたが、 このたび Rails2.1 ではなくなってしまった。 これは obsoleted というより内部のクラス構成が変わってしまったことに起因する。

before 2.1

   1  >> ApplicationController.filter_chain.class
   2  => Array

after 2.1

   1  >> ApplicationController.filter_chain.class
   2  => ActionController::Filters::FilterChain

Controller.filter_chain は定義されたフィルタが格納されたオブジェクトであるが、これが 2.1 からは専用のFilterChainクラスに進化した。 これにより、以下のような数々のフィルタ操作メソッド

  • find_filter
  • append_filter_to_chain
  • prepend_filter_to_chain
  • ...

を直接コントローラに定義する(2.1以前)のではなく、 同クラスに持たせることができるようになった。 これは、名前空間的にも責務の明瞭化としても進化と言える。

2.1 でのフィルタの検索

従って、今後は Controller クラスに直接問い合わせるのでなく、 フィルタを管理する FilterChain インスタンスに尋ねることになる。

   1  >> ApplicationController.filter_chain.find(:login_required)
   2  => #<ActionController::Filters::BeforeFilter:0x2621914 ...

互換性

しかしながら、未だに1.2ユーザも多いプラグインなどでは後方互換性が必要である。 色んな対応策が考えられるが、 ここでは、2.1 側に涙を飲んでもらって、 せっかく奇麗にした所を悪いが find_filter を追加させてもらう。

   1  ActionController::Base.class_eval do
   2    unless respond_to?(:find_filter)
   3      def self.find_filter(name)
   4        filter_chain.find(name)
   5      end
   6    end

(vendor/plugins/xxx/init.rb)

posted by Png maiha on Thu 7 Aug 2008 at 10:49

せっかくアカウントをとったので、テストがてらプログラムを載せてみる。

お題は、どう書く.orgにあった、一番最近の問題で。

問題の内容は、 どう書く.org - LL Golf Hole 3 - 13日の金曜日を数え上げるを参照のこと。

   1  require 'date'
   2  
   3  from = DateTime.now
   4  to = DateTime.parse("2013-12-31")
   5  
   6  puts (from..to).find_all{|d| d.wday==5 and d.mday==13}.size

こんな感じになりました。

コードがちゃんとハイライトされてる! s21g blogすごくイイ!!

こんな便利なブログサービスを提供してくれている瀧内さんに感謝、感謝です。

posted by Png y_tsuda on Thu 7 Aug 2008 at 00:58 with 3 comments
Contents
Rails(Ruby)で例外処理
Formulaが化学構造式に対応しました
Formulaで使用できるLaTeX記法が増えました
Ruby-1.8.7 p71にアップデート
Rails2.1 の STI
23世紀の予定を立てる方法
Fixnum#respond_to?
falseとnilと
Rails2.1 で find_filter
どう書く.org - LL Golf Hole 3 - 13日の金曜日を数え上げる
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ