Enviar búsqueda
Cargar
gsub with ActiveSupport::SafeBuffer
•
1 recomendación
•
3,065 vistas
Aki Ariga
Seguir
Railsのhelperでnamed captureを使ったgsubを使ったらハマった話
Leer menos
Leer más
Tecnología
Denunciar
Compartir
Denunciar
Compartir
1 de 12
Descargar ahora
Descargar para leer sin conexión
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
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
taisei2219
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
danielhu54
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
Toru Tamaki
Más contenido relacionado
Más 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
Más 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と機械学習の現状
Último
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
taisei2219
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
danielhu54
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
Toru Tamaki
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
sugiuralab
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Yuma Ohgami
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
Toru Tamaki
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
Toru Tamaki
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
iPride Co., Ltd.
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
Ryo Sasaki
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
Hiroki Ichikura
Último
(10)
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
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!!!"!
Descargar ahora