Submit Search
Upload
gsub with ActiveSupport::SafeBuffer
•
1 like
•
3,065 views
Aki Ariga
Follow
Railsのhelperでnamed captureを使ったgsubを使ったらハマった話
Read less
Read more
Technology
Report
Share
Report
Share
1 of 12
Download now
Download to read offline
Recommended
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
Recommended
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
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NTT DATA Technology & Innovation
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
More Related Content
More from 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
More from 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と機械学習の現状
Recently uploaded
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NTT DATA Technology & Innovation
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
博三 太田
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
akihisamiyanaga1
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
FumieNakayama
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
sugiuralab
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
Hiroshi Tomioka
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
Yuki Kikuchi
Recently uploaded
(9)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
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!!!"!
Download now