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
Denunciar
Compartilhar
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
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
Hiroshi Tomioka
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
iPride Co., Ltd.
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
Toru Tamaki
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と機械学習の現状
Último
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
Hiroshi Tomioka
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
iPride Co., Ltd.
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
Toru Tamaki
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
Toru Tamaki
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
CRI Japan, Inc.
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
atsushi061452
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NTT DATA Technology & Innovation
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Hiroshi Tomioka
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
iPride Co., Ltd.
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
iPride Co., Ltd.
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
CRI Japan, Inc.
Último
(11)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Observabilityは従来型の監視と何が違うのか(キンドリルジャパン社内勉強会:2022年10月27日発表)
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
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