= key.split("/") filename_parts = [] filename_parts.unshift(key_path.pop) unique_key = key_path.pop filename_parts.unshift(unique_key) if unique_key filename_parts.join("/") end end end
= key.split("/") filename_parts = [] filename_parts.unshift(key_path.pop) unique_key = key_path.pop filename_parts.unshift(unique_key) if unique_key filename_parts.join("/") end end end ŇP0ʢpMFOBNFͱ͍͏ϝιουͳͷʹ TͷLFZؚ͕·ΕͯΔͱεϥογϡͰ۠ͬͨ ࠷ޙͷཁૉͱɺ͏ҰͭΛ࣍ͷཁૉΛ ϑΝΠϧ໊ͱͯ͠ฦͯ͠Δ
= key.split("/") filename_parts = [] filename_parts.unshift(key_path.pop) unique_key = key_path.pop # filename_parts.unshift(unique_key) if unique_key filename_parts.join("/") end end end app/config/initializers/fix_carrier_wave_direct.rb ڧҾʹpMFOBNFͷΈΛฦ͢Α͏ʹͨ͠
end end ϓϩδΣΫτͷதʹ ΦʔϓϯΫϥεΛ͍ͯ͠Δ ίʔυ͕ࠞͬͯ͡Δͱશମ ʹద༻͞Εͯ͠·͏ app/models/some_model.rb class SomeController < ApplicationController def index "param_#{params[:page]}" #=> param_test end end app/controller/some_controller.rb
A do # refineの後にモンキーパッチしたいクラス def a # => 渡したブロックでクラス定義ができる "b" end end end A.new.a # => "a" using B # usingキーワードでモジュールを指定すると # それ以降モンキーパッチが適用される A.new.a # => "b"
end class SomeController def save # バリデーションが行われる # ... end using SkipValidation def force_save # バリデーションがスキップされる # ... end end 3FpOFNFOU͢Δ͜ͱʹΑͬͯ ͲͷΑ͏ͳϞϯΩʔύον͕ͳ͞ΕͯΔ ͔ͷώϯτʹͳͬͯൣғݶఆͰ͖Δͷ ͕ϝϦοτʁ
def &(other) SQL.and(self, other) end end end class DSL using StringOperator def self.where(&block) # 現状blockの中にまでusingは適用できない instance_eval(&block) end end # より抽象度の高い記述が簡単なコードで実現できる DSL.where { "a" | "b" & "c" } "a" | "b" # NoMethodError
works with Numeric elements. # # We tried shimming it to attempt the fast native method, rescue TypeError, # and fall back to the compatible implementation, but that's much slower than # just calling the compat method in the first place. if Array.instance_methods(false).include?(:sum) && !(%w[a].sum rescue false) # Using Refinements here in order not to expose our internal method using Module.new { refine Array do alias :orig_sum :sum end } class Array def sum(init = nil, &block) #:nodoc: if init.is_a?(Numeric) || first.is_a?(Numeric) init ||= 0 orig_sum(init, &block) else super end end end end
2.4〜 using Module.new { refine Array do alias :orig_sum :sum # Ruby 2.4のsumメソッドにorig_sumとaliasをつける end } class Array def sum(init = nil, &block) #:nodoc: if init.is_a?(Numeric) || first.is_a?(Numeric) # 要素がNumericの時は Ruby 2.4 のsumを呼び出す init ||= 0 orig_sum(init, &block) else # それ以外の時は Rails のsumを呼び出す super end end end end
このModuleはローカル変数なので、このブロックでのみしか存 在しない refine Array do alias :orig_sum :sum # orig_sumはここよりファイルの末尾のみしかアクセスで きない end } class Array def sum(init = nil, &block) #:nodoc: if init.is_a?(Numeric) || first.is_a?(Numeric) init ||= 0 orig_sum(init, &block) else super end end end end
Array do alias :orig_sum :sum end } class Array def sum(init = nil, &block) #:nodoc: if init.is_a?(Numeric) || first.is_a?(Numeric) init ||= 0 orig_sum(init, &block) else super end end end end