📜 ⬆️ ⬇️

Ruby - class method proxying

Sometimes it becomes necessary to wrap (wrap) the class methods into your code.

For example:


So let's say we have a class that pulls information about a human hacker:
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
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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

We will use it like this:
Copy Source | Copy HTML
  1. info = Habr. new ( 'tenkoff' )
  2. p info.get_rating
  3. p info.get_posts

The simplest solution is to transfer a new instance of the class to our proxy class:
Copy Source | Copy HTML
  1. proxy = proxy. new (ProxedClass.new)

To do this, we need the method method_missing :
Copy Source | Copy HTML
  1. class proxy
  2. def initialize (obj)
  3. @ class = obj || self
  4. end
  5. def method_missing (name, * args, & block)
  6. p "before ## {name}"
  7. result = @ class .send name, * args, & block
  8. p "after ## {name}"
  9. result
  10. end
  11. end

Now our previous code can be used like this:
Copy Source | Copy HTML
  1. info = proxy. new (Habr. new ( 'tenkoff' ))
  2. p info.get_rating
  3. p info.get_posts

If you really want to, then you can use the Proxy class as the base class for inheritance, for this we need the method_added method (we’ll have to google it here):
Copy Source | Copy HTML
  1. class proxy
  2. def initialize (obj = '' )
  3. @ class = obj || self
  4. end
  5. def method_missing (name, * args, & block)
  6. p "before ## {name}"
  7. result = @ class .send name, * args, & block
  8. p "after ## {name}"
  9. result
  10. end
  11. def self .method_added (method)
  12. if self .public_method_defined? method
  13. private method
  14. end
  15. end
  16. end

To ensure that the methods of your class fall into our method_missing method wrapper, we use the private method to make all public methods private.

You will also have to take care of the proper initialization of the base class by changing the proxied class like this:
Copy Source | Copy HTML
  1. def initialize (user)
  2. @user = user
  3. @uid = get_uid
  4. super ( self )
  5. end

')
In reality, this is all black magic that will not work 100% in all cases of life.

So how does it work?
method_missing - this method works when you request a non-existent or inaccessible class method, it takes three parameters:
method name
arguments
block

Then you can use the send method, which allows you to call any method of any class, for example:
Copy Source | Copy HTML
  1. irb (main): 001 : 0 > Kernel .send: print , 'hello world'
  2. hello world => nil


method_added - this method is triggered at the moment when a new method is added to the class, starting from where you described it, i.e. if after method_added there are some other methods of yours, then he will catch them too.

So, all together, method_missing, allows us to catch non-existing or inaccessible class methods, to make real-life class methods that inherit this black magic from the base class, we use method_added, which registers the addition of new methods and hides them from external access through the private method thus ensures that they fall into method_missing.

In which, you can wrap them in any additional logic, without touching the class you are working on, without changing the program code in which the proxied class is called.

Source: https://habr.com/ru/post/53888/


All Articles