コード内でクラスメソッドをコーディング(ラップ)することが必要になる場合があります。
例:
- それらをスレッドで実行する
- 前後にフィルターを掛ける
- パフォーマンスを測定する
したがって、habrmanに関する情報を取得する特定のクラスがあるとします。
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
Copy Source | Copy HTML require 'rubygems' require 'open-uri' require 'iconv' require 'nokogiri' $KCODE = 'u' class Habr attr_accessor :user, :uid def initialize (user) @user = user @uid = get_uid end def get (url) Nokogiri::HTML( open (url)) end def get_uid html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div.karma-holder" )[ 0 ].get_attribute( 'id' ) end def get_rating result = [] html = get ( "http://#{@user}.habrahabr.ru" ) html.css( "div[@id='#{@uid}']" ).each{|el| result << el.css( 'span.mark' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'span.number' )[ 0 ].content. gsub !( ',' , '.' ).to_f result << el.css( 'dd.total' )[ 0 ].content.to_i } result end def get_posts result = [] html = get ( "http://#{@user}.habrahabr.ru/blog/" ) html.css( "div[@id='main-content'] a.topic" ).each{|el| result << { :title => el.content, :url => el.attribute( 'href' ) } } result end end
次のように使用します。
Copy Source | Copy HTML
- info = Habr。 新規 ( 'tenkoff' )
- p info.get_rating
- p info.get_posts
最も簡単な解決策は、クラスの新しいインスタンスをプロキシクラスに渡すことです。
Copy Source | Copy HTML
- プロキシ=プロキシ。 new (ProxedClass.new)
これを行うには、
method_missingメソッドが必要です。
Copy Source | Copy HTML
- クラス プロキシ
- def 初期化 (obj)
- @ class = obj || 自己
- 終わり
- def method_missing (名前、*引数、およびブロック)
- p 「## {name}の前」
- result = @ class .send name、* args、&block
- p 「## {name}の後」
- 結果
- 終わり
- 終わり
これで、以前のコードは次のように使用できます。
Copy Source | Copy HTML
- info =プロキシ。 new (Habr。new( 'tenkoff' ))
- p info.get_rating
- p info.get_posts
本当にしたい場合は、Proxyクラスを継承の基本クラスとして使用できます。そのためには、
method_addedメソッドが必要
です (ここではgoogleする必要があります)。
Copy Source | Copy HTML
- クラス プロキシ
- def 初期化 (obj = '' )
- @ class = obj || 自己
- 終わり
- def method_missing (名前、*引数、およびブロック)
- p 「## {name}の前」
- result = @ class .send name、* args、&block
- p 「## {name}の後」
- 結果
- 終わり
- def self .method_added(メソッド)
- もしself .public_method_defined? 方法
- プライベートメソッド
- 終わり
- 終わり
- 終わり
クラスのメソッドが
method_missingメソッドのラッパーに
確実に収まるように、すべてのパブリックメソッドをプライベートにする
privateメソッドを使用します。
また、プロキシクラスを次のように変更して、基本クラスの適切な初期化に注意する必要があります。
Copy Source | Copy HTML
- def initialize (ユーザー)
- @user =ユーザー
- @uid = get_uid
- スーパー ( 自己 )
- 終わり
実際、これはすべて黒魔術であり、人生のすべての場合に100%は機能しません。それでは、どのように機能しますかmethod_missing-このメソッドは、存在しないまたはアクセスできないクラスメソッドを要求するとトリガーされます。次の3つのパラメーターが必要です。
メソッド名
引数
ブロックする
次に、sendメソッドを使用して、任意のクラスのメソッドを呼び出すことができます。次に例を示します。
Copy Source | Copy HTML
- irb(メイン): 001 : 0 > カーネル .send: print 、 「hello world」
- Hello world => nil
method_added-このメソッドは、新しいメソッドがクラスに追加された瞬間にトリガーされます。これは、記述した場所、つまり、 method_addedの後に他のメソッドがある場合は、それらもキャッチします。
したがって、method_missingを使用すると、存在しないまたはアクセスできないクラスメソッドをキャッチして、基本クラスからこのブラックマジックを継承する既存のクラスメソッドを作成できます。method_addedを使用して、新しいメソッドの追加を登録し、プライベートメソッド、したがってmethod_missingに入ることを保証します。
プロキシされたクラスが呼び出されるプログラムのコードを変更することなく、作業中のクラスに触れることなく、追加のロジックでそれらをラップできます。