From: yamataka@...08.itscom.net Date: 2019-07-04T18:03:27+09:00 Subject: [ruby-list:50804] Re: [質問] メソッド内で、クラス new するのは良くない? On Wed, 03 Jul 2019 23:35:11 +0900, masayoshi takahashi wrote: > > 高橋征義です。 高橋さん、ご教示ありがとうございます > いえ、そのsingleton methodは「インスタンスがそもそも1つのみ」という意味ではないのでした。 > 言ってみれば「1つしかないREXML::Documentクラスオブジェクト」自体のメソッドなのでsingleton method、 > といったような意味です。 > (クラスメソッドと呼ばれているメソッドが、ここでのsingleton methodに該当します) 理解が深まりました。 > 元々の質問については、具体的な状況がわからないのでなんともなのですが、 > > > doc = REXML::Document.new(xml) > > のxmlが毎回違う内容であれば特に問題ないかと思いますし、 まさに、知りたかった事です。 > 毎回同じ内容で、変更もしないのであれば、docを@docにして使い回すようにした方がよいかもしれません。 そうですね。 > > > ■ 自作コード(抜粋) > > > > > > require 'rexml/document' > > > > > > def get_text_from_page_source_xml(**resource_ids) > > > > > > xml = @cbelement.page_source > > > doc = REXML::Document.new(xml) > > > > > > resource_ids.each do |rid,v| > > > arid = "com.aspiro.tidal:id/" + rid.to_s # arid: actual resource id > > > xpath = "//*[@resource-id='#{arid}']" # need ' at before and end #{id} > > > > > > begin > > > text = doc.elements[xpath].attributes["text"] > > > resource_ids[rid] = text > > > rescue => e > > > resource_ids[rid] = nil > > > end > > > end > > > > > > return resource_ids > > > end > > > > > > id0 = :artistName > > > id1 = :mediaItemTitle > > > texts = get_text_from_page_source_xml(id0 => :nil, id1 => :nil) > > >