gem ãä½ã£ã¦ã¿ã#1
newgem ã使ã£ãä½æ³ãå¦ã³ã¾ãï¼
å¿ è¦ããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«
% sudo gem install rubigen % sudo gem install newgem
ãã³ãã¬ã¼ãã®ä½æ
% newgem map_by_method
ã¢ã¸ã¥ã¼ã«ã®ä½æ
ããããä½ãã®ã¯ MapByMethod ã¢ã¸ã¥ã¼ã«ãªã®ã§ï¼lib/map_by_method.rb ãæåã«å¼ã³åºããã¾ãï¼
- lib/map_by_method.rb
module MapByMethod VERSION = "0.0.1" def self.included(base) super base.module_eval <<-EOS def method_missing(method, *arg, &block) super rescue NoMethodError error = $! begin re = /(map|collect|select|each|reject)_([\\w\\_]+\\??)/ if (match = method.to_s.match(re)) iterator, callmethod = match[1..2] return self.send(iterator){|item| item.send callmethod} end return self.map{|item| item.send method.to_s.singularize.to_sym} rescue NoMethodError nil end end EOS end end unless String.instance_methods.include? "singularize" class String def singularize self.gsub(/e?s\Z/, '') end end end Array.send :include, MapByMethod
åä½ç¢ºèª
ä¸å¿åããã¨ã確èªãã¦ã¿ãï¼
% irb >> require 'lib/map_by_method' => true >> a = (1..10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> a.map_to_s => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
ããã±ã¼ã¸ä½æ
rake package ã³ãã³ãã§ä½æãã¾ãï¼
% rake package
ããã±ã¼ã¸ãã¡ã¤ã«ã¯ pkg é ä¸ã«ä½æããã¾ãï¼
% ls pkg/ map_by_method-0.0.1 map_by_method-0.0.1.gem map_by_method-0.0.1.tgz
ã¤ã³ã¹ãã¼ã«ã»å®è¡ã»ã¢ã³ã¤ã³ã¹ãã¼ã«
% sudo gem install pkg/map_by_method-0.0.1.gem % irb >> require 'map_by_method' => true >> a = (1..10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> a.map_to_s => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] % ls /usr/lib/ruby/gems/1.8/gems/map_by_method-0.0.1 History.txt Manifest.txt PostInstall.txt README.rdoc Rakefile lib script test % sudo gem uninstall map_by_method Successfully uninstalled map_by_method-0.0.1 % ls /usr/lib/ruby/gems/1.8/gems/map_by_method-0.0.1 ls: cannot access /usr/lib/ruby/gems/1.8/gems/map_by_method-0.0.1: No such file or directory