Hatena::ブログ(Diary)

kelkronsoの日記 RSSフィード

2011-04-25 poi 名前定義 取得

POIで名前定義されているセルを取得する話


環境
ソース
final Name name = workbook.getName("名前");
final AreaReference areaReference = new AreaReference(name.getRefersToFormula());
final CellReference firstCell = areaReference.getFirstCell();
final CellReference lastCell = areaReference.getLastCell();
  • RangeAddressクラスがどっかのversionで消えてしまっていて困っていたんだけど、AreaReferenceを使えば解決!

-

2009-01-29

HowToEnsureValidAttributesInFormData



attr_accessibleと可変長引数の話


rails:pages:howtoensurevalidattributesinformdata=> Rails Wiki
ちょっと記事が古いなぁ。今なら違う方法でできるかも。

# @params['user'] contains the attributes we want to add

# allowed_attr specifies which attributes are allowed
if some_flag
  allowed_attr = ['name','email','password','status','balance']
else
  allowed_attr = ['name','email','password']
end

# use delete_if so the hash only contains valid attributes
@params['user'].delete_if {|k,v| not allowed_attr.include? k}

# mass assign our cleaned attributes to the object
user = User.new(@params['user'])


active_supportの力があれば!!1

# @params['user'] contains the attributes we want to add

# allowed_attr specifies which attributes are allowed
if some_flag
  allowed_attr = ['name','email','password','status','balance']
else
  allowed_attr = ['name','email','password']
end

# mass assign our cleaned attributes to the object
user = User.new(@params['user'].slice(*allowed_attr)) #←ここに注目


--------
ぼくも早く2.2に上げて
Object#present?でびゅーしてunlessからおさらばしたいです。
unless 〜 else とか分かりにくいからやめてほしいよ><

2008-11-15

private!

privateの意味は、メソッドを関数形式でだけ呼び出せるようにし、レシーバー形式では呼び出せないようにするという意味です。したがって、可視性がprivateなメソッドは、自クラス及びサブクラスからしか参照できません。

protectedも同様に、自クラス及びサブクラスからしか参照できませんが、関数形式でもレシーバー形式でも呼び出せます。

メソッドのカプセル化に必要な機能です。

Ruby Reference Manual - るりま

なん…だと…。 こんな基本的なことに今まで気づかなかったとは><


というわけでid:koumiya氏に教えていただいた通り、アクションをまるごとトランザクションにぶち込むのは以下で可能でした。

def perform_action_without_rescue_with_tx
  ActiveRecord::Base.transaction do
    perform_action_without_rescue_without_tx
  end
end
alias_method_chain :perform_action_without_rescue, :tx

create後にraiseしてロールバックされてることも確認しました。 やっほい。

2008-11-14

perform_action!


論文なんかよりコードが書きたい!と思ってたらこれができた。


追記2:
private! - kelkronsoの日記


追記:
とおもったらだめだった

こいつでしっかりキャッチされてた><

      def perform_action_with_rescue #:nodoc:
        perform_action_without_rescue
      rescue Exception => exception
        rescue_action_with_handler(exception) || rescue_action(exception)
      end



class ApplicationController < ActionController::Base
 
  def perform_action_with_tx
    puts "before tx"
    ActiveRecord::Base.transaction do
      perform_action_without_tx
    end
    puts "after tx"
  end
  alias_method_chain :perform_action, :tx
end

class Controller << App///

  before_filter do 
    puts "before filter"
  end
  
  after_filter do
    puts "afiter filter"
  end
  
  def index
    
    puts "index in action"
  end

end

れっつあくせす!

before tx
before filter
index in action
afiter filter
after tx


まぁ囲んだだけなんだけどね…

2008-10-31

Rubyっぽいコードを書く



#rubyはじめて30分位の人が書きそうなコード
if hoge != nil
fuga = hoge
end

#nil?くらいは使いたい
if !hoge.nil?
fuga = hoge
end

#!(否定)を使わない
unless hoge.nil?
fuga = hoge
end

#nilはfalseである
if hoge
fuga = hoge
end

#1行で書けるときは1行で
fuga = hoge if hoge

#hogeがnilじゃなかったらfugaに代入する、nilの場合はdefalutを代入する
fuga = []
if hoge
  fuga = hoge
else
  fuga = default_array#default的な何か
end

#ifはブロックを作らない
if hoge
  fuga = hoge
else
  fuga = default_array
end

fuga.each{...} #これはぬるぽにならない

#評価式をうまく
fuga = hoge || default_array


------
まったく関係ないけど、injectってresultをちゃんと返してやらないと駄目なんだね><
resultはなぜか知らないけどもっと広いスコープなのかと思ってました。

つまりこんなコードを書いて頭をうならせていたぼくはunkってことです

a = [1,2,3,4,5]
a.inject(0){|result, i| result += i if i>3 } #nilが+できないうぇうぇと言われる

a.inject(0) do |result, i|
  result += i if i>3
  result #resultを毎回返してやればok
end