columnizeã使ã£ã¦ã¿ã[Ruby][irb][Gem]
ããã¾ãã¦ããã§ã¨ããããã¾ãï¼
æ°å¹´ä¸çºç®ã¯ã©ã¤ããªè©±é¡ãã...
ä»åã¯columnizeã¨ããgemã触ã£ã¦ã¿ã¦ãã£ãã便å©ãªæãããããã ãªã¼ã¨æã£ãã®ã§åå¿é²ã¨ãã¦ã
ã©ããªgemï¼
ãã®gemã¯ç°¡åã«è¨ãã¨é
åã®ä¸èº«ãè¦ãããæ´å½¢ãã¦ããã
gemã§ãï¼
ãããã°æãªã©ã«ä¾¿å©ãããªgemã§ããã
pryã§ãirbã§ãåé¡ãªã使ããã®ã§ããã®ãããã¨çµã¿åãããã¨å¹¸ãã«ãªããããï¼
ã¡ãªã¿ã«gemãç¥ã£ããã£ãã㯠BestGems -- Ruby Gems Download Ranking ãã¼ã¼ã£ã¨è¦ã¦ãã¦ç®ã«æ¢ã¾ã£ãããã§ãï¼
ããããã®ãµã¼ãã¹ã¯ä¾¿å©ï¼è¦ã¦ãã¦æ¥½ããã§ããï½ãããã¨ããããã¾ãï¼
ã¡ãªã¿ã«ãã®columnize
ã¯totalã©ã³ãã³ã°122ä½ã§ããã
ã¤ã³ã¹ãã¼ã«
gem columnize
ãã¡ããGemfile
ã«è¨è¿°ãã¦bundle install --path vendor/bundle
ããããgemã«ç´æ¥ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã¾ãããã
ããã¾ãã§ã:)
使ãæ¹
æ©é使ã£ã¦ã¿ã¾ãã
columnize_sample|â bundle exec irb irb(main):001:0> require 'columnize' => true
requireãã¦ããããã¨ã§columnizeãæå¹ã«ãªãã¾ãã
å½ããåã§ããpryãç¨ãã¦binding.pry
çãè¡ãã¨ãã¯ãrequireãbinding.pry
ãã以åã«requireãã¦ããã¦ãããã°ãã®ã¾ã¾ä½¿ç¨ãããã¨ãå¯è½ã§ãã
irb(main):002:0> a = (1..10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] irb(main):003:0> a.columnize => "1 2 3 4 5 6 7 8 9 10"
columnizeãrequireããã¨arrayã«columnizeã¨ããã¡ã½ãããçããããã«ãªãã¾ãã
å®è¡ããã¨ä¸è¨ã®æ§ã«é åãæååã«å±éããããããªå½¢ã«ãªãã¾ãã
ãã®å±éã®æ¹å¼ããªãã·ã§ã³ã«ãã£ã¦å¤ãã¦ããããputsã§è¡¨ç¤ºãããã¨ã§é åããã©ã¼ããããã¦ãããªãªå½¢ã§è¦ããããã«ããããã¨ããã®ãcolumnizeã®è¶£æ¨ã§ãã
irb(main):004:0> puts a.columnize :displaywidth => 10 1 5 9 2 6 10 3 7 4 8 => nil irb(main):005:0> puts a.columnize :displaywidth => 20 1 3 5 7 9 2 4 6 8 10 => nil
:displaywidthãªãã·ã§ã³ãã¤ããã¨è¡¨ç¤ºæååã®ä¸è¡ãããã®æ大æåæ°
ãæå®ãããã¨ãå¯è½ã§ãã
ã¡ãã£ã¨ãããã«ããããããã¾ããããä¾ãã°ä¸ã¤ç®ã®ä¾ã§ã®:displaywidth => 10
ã ã¨ãï¼è¡ããã10æåã¾ã§è¡¨ç¤ºã§ããç¶æ
ã§ããæãã«è¡¨ç¤ºãã¦ããã¾ãããã®ããæãã«
ã£ã¦ããã®ããªããªãè³¢ãã§ããï¼2 6 10
ã®ä¸¦ã³ã¯8æåã§ããã10ãæå®ãã¦ããã®æ§ã«è¡¨ç¤ºãã¦ããã¾ãã
:displaywidth => 20
ãæå®ããã¨ãå
ã»ã©ã¾ã§ã¯10æåã®å¶éããã£ã¦è¡¨ç¤ºã§ããªãã£ã2 4 6 8 10(14æå)
ã表示ã§ããããã«ãªãããã§ãã
(ã¡ãã£ã¨æåããããã«ããã®ã¯å ç·ãª)
irb(main):006:0> puts a.columnize :arrange_array => true, :displaywidth => 20 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] => nil
:arrange_array => true
ãæå®ããã¨ãé
åã®å½¢ã§åãåºãã¦ããã¾ãã
irb(main):007:0> g = %w(bibrons golden madascar leopard mourning suras tokay) => ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"] irb(main):008:0> puts g.columnize :displaywidth => 15 bibrons suras golden tokay madascar leopard mourning => nil
ä¸èº«ãæååã§ãOKã§ãã
irb(main):009:0> puts g.columnize :displaywidth => 19, :colsep => ' | ' bibrons | mourning golden | suras madascar | tokay leopard => nil
:colsep => <åºåãæåã¨ãã¦å©ç¨ãããæåå>
ã¨ããã¨ãããã§åã®éãåºåã£ã¦ããã¾ãã
ã¬ã¤ã¢ã¦ãããã®ã¾ã¾ã«ãããå ´åã¯colsepã®æåæ°ådisplaywidthã®å¤ãä¸æãå¢ããå¿
è¦ãããã¾ãã(ãã®ã¸ããããå°ããããªã«ã§ããããªããããã)
irb(main):062:0> puts g.columnize :displaywidth => 19, :colsep => ' | ', :ljust => false bibrons | mourning golden | suras madascar | tokay leopard => nil irb(main):063:0> puts g.columnize :displaywidth => 19, :colsep => ' | ', :ljust => true bibrons | mourning golden | suras madascar | tokay leopard => nil
:ljust
ãªãã·ã§ã³ã«ãã£ã¦å·¦æããã©ãããé¸æã§ãã¾ããããã©ã«ãã¯falseã«è¨å®ããã¦ãã¾ãã
irb(main):011:0> Columnize.columnize(a) => "1 2 3 4 5 6 7 8 9 10" irb(main):012:0> puts Columnize.columnize(a, :displaywidth => 10) 1 5 9 2 6 10 3 7 4 8 => nil
ã¯ã©ã¹ã¡ã½ããã¨ãã¦å¼ã¶ãã¨ãå¯è½ã§ãã
ã¡ãªã¿ã«putsããªãã§è¦ã¦ã¿ãã¨ãããªæãã§ãã
irb(main):071:0> g.columnize :displaywidth => 18, :colsep => ' | ', :ljust => true => "bibrons | suras\ngolden | tokay\nmadascar\nleopard \nmourning"
çµããã«
å®ãã¼ã¿ã£ã½ããã®ããµã¯ãã¨æ±ãããã¨ããªã©ã«ç¥ã£ã¦ããã¨æ¥½ãã§ãããã ãªãã¨ããå°è±¡ã®gemã§ããï¼
ä»åã®ç©ã¯å²ãã¨æåããã§ãããããã®ããã«ããããªããã¨åºä¼ã£ãgemã触ã£ã¦ã³ã¼ããèªãã§ã¿ããããã®ã楽ãããã ãªã¨æãã¾ããï¼
gem readingç¿æ £ã®æ©éï¼ï½