ä¼ç¤¾ã§è°è«ã«ãªããæªã ã«ç§ã®ä¸ã§æ±ºçãã¤ãã¦ããªãåé¡ãåãä¸ãã¦ã¿ã¾ãã
Rubyã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ãç´æ¥åç §ãããã¨ã«ã¤ãã¦ã§ãã
ä¾ãã°ãã³ã³ã¹ãã©ã¯ã¿ã§ã¤ã³ã¹ã¿ã³ã¹å¤æ°ãè¨å®ãã¾ãã
ããã¦ãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã§ãã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ãç´æ¥åç §ãã¦å¦çããã¦å¤ãè¿å´ããã¨ãããã¨ããã¾ãã
è¯ãä¾ã§ã¯ãªãããããã¾ãããããããªæãã®ã¤ã¡ã¼ã¸ã§ãã
class PriceCalculation def initialize(price, tax) @price = price @tax = tax end def tax_included_price @price * (1 + @tax) end end
åæã¨ãã¦ã@priceã@taxã¯ãã®ã¯ã©ã¹å ã®ã¿ã§ä½¿ç¨ãããå¤é¨ããã¯åç §ãããªããã®ã¨ãã¾ãã
ããã«å¯¾ããååã®ãã¤ã人ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ãç´æ¥åç §ããã®ã¯è¯ããªããããattr_readerã使ãã¨ææãã¦ãã¾ããã å½¼ãè¨ãã«ã¯ãããªæãã§ãã
class PriceCalculation attr_reader :price, :tax def initialize(price, tax) @price = price @tax = tax end def tax_included_price price * (1 + tax) end end
ããããç§ã¯ãattr_readerã使ã£ã¦ãã¾ãã¨ãPriceCalculationã®å¤é¨ããpriceãtaxãåç §ãããå¿ è¦ãç¡ãã«ããããããåç §ã§ãã¦ãã¾ãããã¡ãã®ã»ããããããç¡ãã®ã§ã¨æãã¾ããã
ã¤ã³ã¹ã¿ã³ã¹å¤æ°ããã®ã¾ã¾åç §ããæ¹æ³ã§ãattr_readerã使ããªãå ´åã¯åç §ãããã¨ã¯ã§ãã¾ããã
ï¼ãã¡ããrubyãªã®ã§åç §ã§ããªãããã§ã¯ãªãã§ããï¼
ä¾1: ãã®æ¹æ³ã ã¨å¼ã³åºããªã
[1] pry(main)> class PriceCalculation [1] pry(main)* def initialize(price, tax) [1] pry(main)* @price = price [1] pry(main)* @tax = tax [1] pry(main)* end [1] pry(main)* [1] pry(main)* def tax_included_price [1] pry(main)* @price * (1 + @tax) [1] pry(main)* end [1] pry(main)* end => :tax_included_price [2] pry(main)> pc = PriceCalculation.new(200, 0.08) => #<PriceCalculation:0x007f9b41ae0ae0 @price=200, @tax=0.08> [3] pry(main)> pc.price NoMethodError: undefined method `price' for #<PriceCalculation:0x007f9b41ae0ae0 @price=200, @tax=0.08> from (pry):12:in `__pry__' [4] pry(main)> pc.tax NoMethodError: undefined method `tax' for #<PriceCalculation:0x007f9b41ae0ae0 @price=200, @tax=0.08> from (pry):13:in `__pry__' [5] pry(main)> # rubyãªã®ã§èªã¿åºãæ¹æ³ã¯ãã¡ãããããã»ã»ã»ã [6] pry(main)> pc.instance_eval("@price") => 200
ä¾1: ãã®æ¹æ³ã ã¨å¼ã³åºãã
[1] pry(main)> class PriceCalculation [1] pry(main)* attr_reader :price, :tax [1] pry(main)* [1] pry(main)* def initialize(price, tax) [1] pry(main)* @price = price [1] pry(main)* @tax = tax [1] pry(main)* end [1] pry(main)* [1] pry(main)* def tax_included_price [1] pry(main)* price * (1 + tax) [1] pry(main)* end [1] pry(main)* end => :tax_included_price [2] pry(main)> pc = PriceCalculation.new(200, 0.08) => #<PriceCalculation:0x007fa20222e830 @price=200, @tax=0.08> [3] pry(main)> pc.price => 200 [4] pry(main)> pc.tax => 0.08
å½¼ã®è¨ãåã¨ãã¦ã¯ãããã¯ãã¿ã¼ã³ãªã®ã ã¨ãattr_readerã使ã£ãã»ããå¤åã«èãããã½ã¼ã¹ã³ã¼ããªã®ã ã¨ãããè¨ã£ã¦ãã¾ãããããã¦ãSandi Metzã®æ¬ãåç §ãã¦ãã¾ããã GitHubããå¼ç¨ããã¦ããã ãã¾ãã poodr/chapter_2.rb at master · skmetz/poodr · GitHub
############## Page 24 ############## class Gear def initialize(chainring, cog) @chainring = chainring @cog = cog end def ratio @chainring / @cog.to_f # <-- road to ruin end end ############## Page 25 ############## class Gear attr_reader :chainring, :cog # <------- def initialize(chainring, cog) @chainring = chainring @cog = cog end def ratio chainring / cog.to_f # <------- end end
road to ruinã¨ã¯ä¸ç´ç·ã«ç ´æ» ã«åããã¨ããæå³ã§ãã
ãã®çç±ã¨ãã¦ã¯ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ãç·¨éãã¦ã¯ã©ã¹ã§å ±éã«ä½¿ããããªãã¨ãã«ã¡ã½ããã ãä¿®æ£ããã°ããããå¤åã«èãããã¨ãããã¨ãæãããã¦ãã¾ããã
############## Page 25 ############## # a simple reimplementation of cog def cog @cog * unanticipated_adjustment_factor end ############## Page 25 ############## # a more complex one def cog @cog * (foo? ? bar_adjustment : baz_adjustment) end
ãããªé¢¨ã«@cogããã®ã¾ã¾ä½¿ãã®ã§ã¯ãªããç·¨éãã¦ä½¿ãããæã«ãattr_readerã使ã£ãã»ããè¯ãã¨ãããã¨ã§ããã
ãã ãç§å人ã®æè¦ã¨ãã¦ã¯ãattr_readerã§å¤ããè¦ããããã«ããã¨ãã©ãã§ä½¿ããã¦ãããããã«ã¯ããããªããªããä¿å®æ§ãä¸ãã£ã¦ãã¾ããã¨ãã¾ãããã®ä¾ã§è¨ã@cogãPage 25ã®ä¾ã®ããã«ä½ãå¦çããã¦ä½¿ããããªãå ´åã¯ãã»ã¨ãã©ãªãã¨æããã¨ããã£ãã¨ãã¦ããã¹ã¦ã®cogãç·¨éããcogã§ä½¿ãããã¨æãã±ã¼ã¹ã¯ãªãã¨æãã®ã§ããã®å©ç¹ã«ã¤ãã¦ã¯ã¾ã£ããå©ç¹ã¨æãã¾ããã§ããããããããªã«ãå¦çããããå ´åã¯ãã®å¦çãæå³ããååãå ããã¡ã½ããåãã¤ãã¦ãããªãã¨ä½ãæå³ããã®ããçç®åãããªããªã£ã¦ãã¾ãã¾ãã
def some_decorated_cog @cog * (foo? ? bar_adjustment : baz_adjustment) end
ã®ããã«å¦çã®æå³ãä»ãå ãã¦ãããªãã¨å¯èªæ§ãè½ã¡ã¾ãã
â»ã³ã³ã¹ãã©ã¯ã¿ã§è¨å®ããã¨ãããã¨ãã§ãã¾ãããããã¯è¯ãã®ã§ãããããæªãã®ã§ããããã»ã»ã»ã
def initialize(chainring, cog) @chainring = chainring @cog = cog * (foo? ? bar_adjustment : baz_adjustment) end
ãã®ããããç§ã¯Sandi Metzã®ä¸»å¼µãããã¿ã¼ã³ã«ã¯åæãããã¾ããSandi Metzã®æ¬ã«ã¯ããã®ãã¿ã¼ã³ã«ã¤ãã¦ï¼ã¤ã®åé¡ç¹ãæãããã¦ãã¾ãããä»æå ã«ç¡ãã®ã§è©³ç´°ã¯è¦ãã¦ããªãã®ã§ãããããããããããã¡ãã®åé¡ç¹ã®ã»ããéè¦ãªã®ã§ã¯ãªããã¨ããæãã¾ããã
ããã§ãããã¤ãåèä¾ãè¦ã¦ã¿ããã¨æãDraperã®ã³ã¼ããããã£ã¦ã¿ã¾ããã
draper/factory.rb at master · drapergem/draper · GitHub
ããã¨ãattr_readerãprivateã«ããã¨ããããæ¹ããã¦ãã¾ããã
確ãã«ããã ã¨å¤é¨ããåç §ãããªããã¨ãç´æãããã®ã§ãä¿å®æ§ãé«ã¾ãã¾ããï¼é»éè¡ã使ãããªãåæãï¼
[1] pry(main)> class PriceCalculation [1] pry(main)* def initialize(price, tax) [1] pry(main)* @price = price [1] pry(main)* @tax = tax [1] pry(main)* end [1] pry(main)* [1] pry(main)* def tax_included_price [1] pry(main)* price * (1 + tax) [1] pry(main)* end [1] pry(main)* [1] pry(main)* private [1] pry(main)* [1] pry(main)* attr_reader :price, :tax [1] pry(main)* end => nil [2] pry(main)> pc = PriceCalculation.new(200, 0.08) => #<PriceCalculation:0x007fb6739cc3c8 @price=200, @tax=0.08> [3] pry(main)> pc.price NoMethodError: private method `price' called for #<PriceCalculation:0x007fb6739cc3c8 @price=200, @tax=0.08> from (pry):16:in `__pry__' [4] pry(main)> pc.tax NoMethodError: private method `tax' called for #<PriceCalculation:0x007fb6739cc3c8 @price=200, @tax=0.08> from (pry):17:in `__pry__' [5] pry(main)> pc.tax_included_price => 216.0
æ£ç´ãç§ã¯ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ãªã«ãã®å¦çããã¦ååã®ã²ãã¿ã¼ã¡ã½ããã§ãããå¼ã³åºããã¨ã«å¤§ããªæµæãæããã®ã§ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ãç´æ¥åç §ãããã¨ã«ããã¹ã¦ã©ã®ãããã®å©ç¹ãããã®ã ããã¨æãã¾ããã ã²ãã¿ã¼ã¡ã½ããçµç±ã§åç §ãããã®ã§å¦çãå°ãé ããªãã¾ãã
ãã®ãããã®è°è«ãrubyã§ã¯ãªãã§ãããC++ã§ã®è°è«ãããã¾ãããã³ã¡ã³ãã§ã®è°è«ãå«ãã¦èãããããã¾ããããç§ã¯C++ã®ç´ é¤ãã¼ããªã®ã§ãã¾ãç解ã§ãã¦ããã¾ããã»ã»ã»ã
ãã®è¾ºããåèã«ãªãããã§ãã http://qiita.com/Yahagi_pg/items/1bf59fc75d7f17c3b731
æ£ç´rubyã§ã©ãæ¸ãã®ããã¹ããªã®ããçµè«ã¥ãããã¦ãã¾ããã»ã»ã»ã
ãªããè»å ã§æ¥æ¬äººã¨ã³ã¸ãã¢ï¼äººã»ã©ã¨è°è«ããã¨ãããçã¤ã³ã¹ã¿ã³ã¹å¤æ°ç´æ¥åç §ããã°è¯ãã¨ããæè¦ã§ããã
â»å¿µã®ããç¹°ãè¿ãã¾ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ãå¤é¨ããåç §ãããå¿ è¦ãç¡ãå ´åã§ããå¤é¨ããåç §ãããå ´åã¯ãç´ ç´ã«attr_readerã使ãã°è¯ãã§ãã