Enviar pesquisa
Carregar
gsub with ActiveSupport::SafeBuffer
•
1 gostou
•
3,065 visualizações
Aki Ariga
Seguir
Railsのhelperでnamed captureを使ったgsubを使ったらハマった話
Leia menos
Leia mais
Tecnologia
Vista de apresentação de diapositivos
Denunciar
Compartilhar
Vista de apresentação de diapositivos
Denunciar
Compartilhar
1 de 12
Baixar agora
Baixar para ler offline
Recomendados
Rails3.1rc4を試してみた
Rails3.1rc4を試してみた
Takahiro Hidaka
Angular jsとsinatraでturbolinks
Angular jsとsinatraでturbolinks
Minori Tokuda
はじめて翻訳記事を書いたら300ブクマ超えた話
はじめて翻訳記事を書いたら300ブクマ超えた話
Aki Ariga
Gong anyware
Gong anyware
Aki Ariga
Juliaを使った機械学習
Juliaを使った機械学習
Aki Ariga
Introduction to Kanagawa Ruby Kaigi01 #kana01
Introduction to Kanagawa Ruby Kaigi01 #kana01
Aki Ariga
Julia 100 exercises #JuliaTokyo
Julia 100 exercises #JuliaTokyo
Aki Ariga
素人がDeep Learningと他の機械学習の性能を比較してみた
素人がDeep Learningと他の機械学習の性能を比較してみた
Toru Imai
Recomendados
Rails3.1rc4を試してみた
Rails3.1rc4を試してみた
Takahiro Hidaka
Angular jsとsinatraでturbolinks
Angular jsとsinatraでturbolinks
Minori Tokuda
はじめて翻訳記事を書いたら300ブクマ超えた話
はじめて翻訳記事を書いたら300ブクマ超えた話
Aki Ariga
Gong anyware
Gong anyware
Aki Ariga
Juliaを使った機械学習
Juliaを使った機械学習
Aki Ariga
Introduction to Kanagawa Ruby Kaigi01 #kana01
Introduction to Kanagawa Ruby Kaigi01 #kana01
Aki Ariga
Julia 100 exercises #JuliaTokyo
Julia 100 exercises #JuliaTokyo
Aki Ariga
素人がDeep Learningと他の機械学習の性能を比較してみた
素人がDeep Learningと他の機械学習の性能を比較してみた
Toru Imai
Challenges for machine learning systems toward continuous improvement
Challenges for machine learning systems toward continuous improvement
Aki Ariga
Managing Machine Learning workflows on Treasure Data
Managing Machine Learning workflows on Treasure Data
Aki Ariga
仕事ではじめる機械学習
仕事ではじめる機械学習
Aki Ariga
主人が外資系IT企業に転職して4ヶ月が過ぎました
主人が外資系IT企業に転職して4ヶ月が過ぎました
Aki Ariga
R&D at Foodtech company - #CookpadTechConf 2016
R&D at Foodtech company - #CookpadTechConf 2016
Aki Ariga
Why I started Machine Learning Casual Talks? #MLCT
Why I started Machine Learning Casual Talks? #MLCT
Aki Ariga
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
Aki Ariga
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Aki Ariga
Machine Learning Casual Talks Intro #MLCT
Machine Learning Casual Talks Intro #MLCT
Aki Ariga
Make Julia more popular in Japan!!1 #JuliaTokyo
Make Julia more popular in Japan!!1 #JuliaTokyo
Aki Ariga
Refrection of kawasaki.rb
Refrection of kawasaki.rb
Aki Ariga
Introduction and benchmarking of MeCab.jl #JapanR
Introduction and benchmarking of MeCab.jl #JapanR
Aki Ariga
Recommendation for iruby #tqrk08
Recommendation for iruby #tqrk08
Aki Ariga
The book that changed me
The book that changed me
Aki Ariga
Introduction of Mecab.jl #JuliaTokyo
Introduction of Mecab.jl #JuliaTokyo
Aki Ariga
Machine Learning Casual Talks opening talk
Machine Learning Casual Talks opening talk
Aki Ariga
なぜ科学計算にはPythonか?
なぜ科学計算にはPythonか?
Aki Ariga
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Aki Ariga
Rubyと機械学習の現状
Rubyと機械学習の現状
Aki Ariga
Mais conteúdo relacionado
Mais de Aki Ariga
Challenges for machine learning systems toward continuous improvement
Challenges for machine learning systems toward continuous improvement
Aki Ariga
Managing Machine Learning workflows on Treasure Data
Managing Machine Learning workflows on Treasure Data
Aki Ariga
仕事ではじめる機械学習
仕事ではじめる機械学習
Aki Ariga
主人が外資系IT企業に転職して4ヶ月が過ぎました
主人が外資系IT企業に転職して4ヶ月が過ぎました
Aki Ariga
R&D at Foodtech company - #CookpadTechConf 2016
R&D at Foodtech company - #CookpadTechConf 2016
Aki Ariga
Why I started Machine Learning Casual Talks? #MLCT
Why I started Machine Learning Casual Talks? #MLCT
Aki Ariga
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
Aki Ariga
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Aki Ariga
Machine Learning Casual Talks Intro #MLCT
Machine Learning Casual Talks Intro #MLCT
Aki Ariga
Make Julia more popular in Japan!!1 #JuliaTokyo
Make Julia more popular in Japan!!1 #JuliaTokyo
Aki Ariga
Refrection of kawasaki.rb
Refrection of kawasaki.rb
Aki Ariga
Introduction and benchmarking of MeCab.jl #JapanR
Introduction and benchmarking of MeCab.jl #JapanR
Aki Ariga
Recommendation for iruby #tqrk08
Recommendation for iruby #tqrk08
Aki Ariga
The book that changed me
The book that changed me
Aki Ariga
Introduction of Mecab.jl #JuliaTokyo
Introduction of Mecab.jl #JuliaTokyo
Aki Ariga
Machine Learning Casual Talks opening talk
Machine Learning Casual Talks opening talk
Aki Ariga
なぜ科学計算にはPythonか?
なぜ科学計算にはPythonか?
Aki Ariga
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Aki Ariga
Rubyと機械学習の現状
Rubyと機械学習の現状
Aki Ariga
Mais de Aki Ariga
(19)
Challenges for machine learning systems toward continuous improvement
Challenges for machine learning systems toward continuous improvement
Managing Machine Learning workflows on Treasure Data
Managing Machine Learning workflows on Treasure Data
仕事ではじめる機械学習
仕事ではじめる機械学習
主人が外資系IT企業に転職して4ヶ月が過ぎました
主人が外資系IT企業に転職して4ヶ月が過ぎました
R&D at Foodtech company - #CookpadTechConf 2016
R&D at Foodtech company - #CookpadTechConf 2016
Why I started Machine Learning Casual Talks? #MLCT
Why I started Machine Learning Casual Talks? #MLCT
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
クックパッドサマーインターン2015 機械学習・自然言語処理 実習課題
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Rubyistがgemの前にPypiデビューするのは間違っているだろうか
Machine Learning Casual Talks Intro #MLCT
Machine Learning Casual Talks Intro #MLCT
Make Julia more popular in Japan!!1 #JuliaTokyo
Make Julia more popular in Japan!!1 #JuliaTokyo
Refrection of kawasaki.rb
Refrection of kawasaki.rb
Introduction and benchmarking of MeCab.jl #JapanR
Introduction and benchmarking of MeCab.jl #JapanR
Recommendation for iruby #tqrk08
Recommendation for iruby #tqrk08
The book that changed me
The book that changed me
Introduction of Mecab.jl #JuliaTokyo
Introduction of Mecab.jl #JuliaTokyo
Machine Learning Casual Talks opening talk
Machine Learning Casual Talks opening talk
なぜ科学計算にはPythonか?
なぜ科学計算にはPythonか?
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Active ModelとNaive Bayesを仲良くする baby_faceを使ってみた
Rubyと機械学習の現状
Rubyと機械学習の現状
gsub with ActiveSupport::SafeBuffer
1.
Railsのhelperでnamed capture使ったらハマった話 chezou kawasaki.rb #010
2.
正規表現とnamed capture • 正規表現のグループに名前をつけて、後方参 照できる pat
= /(?<good>good|nice|greate) catch/! #=> /(?<good>good|nice|greate) catch/! pat === 'nice catch!!'! #=> true! Regexp.last_match! #=> #<MatchData "nice catch" good:"nice">! Regexp.last_match[:good]! #=> "nice"!
3.
helperでnamed capture • Railsのhelperでnamed
capture使ったgsubした ら… module FooHelper! def replace_awesome(str)! str.gsub(pat){|m| "#{m}!!!" if Regexp.last_match[:good]}! end! end! ! - str = 'nice catch'! = replace_awesome(str) #=> "nice catch!!!"! # こういうのがやりたい!
4.
現実 = replace_awesome(str)! NoMethodError: undefined
method `[]' for nil:NilClass ( д) ゚ ゚
5.
なぜだろう? => ActiveSupport::SafeBuffer module FooHelper! def
replace_awesome(str)! p(str.class) ! str.gsub(pat){|m| "#{m}!!!" if Regexp.last_match[:good]}! end! end!
6.
AS::SafeBufferとは • RailsのviewではHTMLの特殊文字(<,>,&,")を自 動的にエスケープしてくれる • その時に出力されるクラスがAS::SafeBuffer "".html_safe
+ "<" #=> "<"! ("".html_safe + "<").class #=> ActiveSupport::SafeBuffer! http://guides.rubyonrails.org/active_support_core_extensions.html#extensions-to-string
7.
SafeBufferのgsubを見た rails/activesupport/lib/active_support/core_ext/string/output_safety.rb module ActiveSupport! class SafeBuffer
< String! UNSAFE_STRING_METHODS = %w(! capitalize chomp chop delete downcase gsub lstrip next reverse rstrip! slice squeeze strip sub succ swapcase tr tr_s upcase prepend! )! …! UNSAFE_STRING_METHODS.each do |unsafe_method|! if unsafe_method.respond_to?(unsafe_method)! class_eval <<-EOT, __FILE__, __LINE__ + 1! def #{unsafe_method}(*args, &block) # def capitalize(*args, &block)! to_str.#{unsafe_method}(*args, &block) # to_str.capitalize(*args, &block)! end # end! ! def #{unsafe_method}!(*args) # def capitalize!(*args)! @html_safe = false # @html_safe = false! super # super! end # end! EOT! end! end!
8.
Rubyのみの再現コード def test(*args, &block)! 'hogehoge'.gsub(/h/,
&block)! p Regexp.last_match! end! ! test do |matched|! p Regexp.last_match! end!
9.
Rubyのみの再現コード def test(*args, &block)! 'hogehoge'.gsub(/h/,
&block)! p Regexp.last_match #=> <MatchData "h">! end! ! test do |matched|! p Regexp.last_match #=> nil! end!
10.
仕様です
11.
Rubyのみの再現コード def test(*args, &block)! 'hogehoge'.gsub(/h/,
&block)! p Regexp.last_match #=> <MatchData "h">! end! ! test do |matched|! p Regexp.last_match #=> nil! end! block内のscopeと違うので取れません
12.
回避方法 module FooHelper! def replace_awesome(str)! str.to_str.gsub(pat){|m|
"#{m}!!!" if $~[:good]}! end! end! ! - str = 'nice catch'! = replace_awesome(str) #=> "nice catch!!!"!
Baixar agora