ããã°ã©ãã³ã°ã¯ä»ã§ãæã
è¡ã£ãŠããŸãããåŸã
ã«ãã®ã¹ã¿ã€ã«ãå€ããŠãããã¡ã¿ããã°ã©ãã³ã°ã«ãŸããŸãé¢é£ããŠããŸãã åæã«ãæ®éã®ããã°ã©ãã³ã°ãå«ã ãšã¯èšããŸããã ããã°ã©ããŒãšåãããã«ãã³ãŒãã®ã¢ãžã¥ãŒã«æ§ãç°¡æœæ§ããããããããæè»æ§ããŸããŸãé«ããæ¹æ³ã暡玢ããŠããŸããã¡ã¿ããã°ã©ãã³ã°ã§ã¯ãæªéçºã®å¯èœæ§ããããŸãïŒLispããã®ç¡éã®ç¡éã®ã€ã³ã¿ãŒãããã¡ã¿ããã°ã©ãã³ã°ã®æŽªæ°Žã«ããããããïŒã :)
Rubyã§ã®ã¡ã¿ããã°ã©ãã³ã°ã«é¢ããããã°ãå§ããããšæããŸãã
Rubyã®éžæã¯ãRubyããã°ã©ããŒã®ç°å¢ã§ã®ã¡ã¿ããã°ã©ãã³ã°ã®æåããã§ã«å€§éšåã圢æããŠãããã¡ã¿ããã°ã©ãã³ã°ã®èŠçŽ ãRubyããã°ã©ããŒã®æ¥åžžäœæ¥ã®æ§é ã«ãªã£ãŠãããšããäºå®ã«ãããã®ã§ããããã«ãä»ã®åçèšèªãããããç¥ãããŠããŸãã
ç§ã¯PhysTechã§RubyïŒRailsïŒMetaprogrammingã«é¢ããè¬çŸ©ãè¡ã£ãŠããŸãã è¬çŸ©ã®1ã€ããã®è³æã¯
ããã§åãããšãã§ã
ãŸã ã åçã«ã¯äž»ãªãã®ã®ç°¡åãªèŠçŽããããŸãã
ãã®ããã°ã§ã¯ãäžè²«ãã詳现ãªæ¹æ³ã§ãããã¯ã玹ä»ããããšããŸãã ã¿ã¹ã¯ã¯åçŽã§ã¯ãªããããäºåã«æ·±åŒåžããŸãã ããªãã®å±ãŸãã®ãã£ãŒãããã¯ãæåŸ
ããŠããŸãã
ç§ã¯ç°¡åãªãã®ããå§ããŸããã-å®çŸ©ä»ãã§ãã
ã¹ã¯ãªããèšèªã§ã®ã¡ã¿ããã°ã©ãã³ã°ã¯ãå®è¡æã«åå空éãå€æŽããæ©èœãå©çšããããã°ã©ã äœæã¹ã¿ã€ã«ã§ãã
åå空éãšã¯ãã¯ã©ã¹ãã¡ãœãããããã³å€æ°ïŒã°ããŒãã«ãããŒã«ã«ãã€ã³ã¹ã¿ã³ã¹å€æ°ãããã³ã¯ã©ã¹å€æ°ïŒãæå³ããŸãã å€æŽãšã¯ãã¯ã©ã¹ãã¡ãœãããå€æ°ãäœæãå€æŽãåé€ããããšãæå³ããŸãã
ã»ãšãã©ã®ã¹ã¯ãªããèšèªã§ã¯ãåå空éã¯ã©ã³ã¿ã€ã ã¢ãŒãã§ã®ã¿æ§ç¯ãããŸãã ããããå€ãã¯ãããèŠããŠããªãã®ã§ãå®çŸ©ã§ããã匷調ããŸããã å®çŸ©ããã©ã³ã¿ã€ã ã®äœåãªèšåãåé€ãããšããwith Benefitããšãããã¬ãŒãºãæ®ããŸãã ãããæ¬è³ªã§ãã
圹ã«ç«ããªãéããã°ã©ãã³ã°ã®äŸïŒ
eval "s = 'eval s'; eval s"
é»å
ç§ã¯ã幌å°æã«BK-0010ã®é¢æ°ãããããããããã°ã©ã ãã©ã®ããã«æžããããèŠããŠããŸãã é¢æ°ã¯ããŒãã³ãŒãã£ã³ã°ãããŠãããããã°ã©ã ã®å®è¡äžã«ãªã¹ããã1ã€ã®é¢æ°ãéžæããç¯å²[x0ãx1]ãæå®ããããšã ããå¯èœã§ãY軞ã«æ²¿ã£ãç¯å²ïŒèŠããããã°ã©ããŒã®èãïŒãã¹ãŠãšãã¹ãŠãèªååã§ããïŒãããã°ã©ã ã«ãã£ãŠèªåçã«éžæãããŸããã
ç§ã¯èªåã®BASICããã°ã©ã ãèŠãŠããšã¯ã¹ã¿ã·ãŒãçµéšããŸããã ããããããããæ²ããæããæãã€ããŸããã ããããç§ãå¿
èŠãšããé¢æ°ã®å
¬åŒãããã°ã©ã ã«çŽæ¥çµã¿èŸŒãããšãã§ããªãã®ã¯æ®å¿µãªããšã§ãã
Ndaa ... 8幎çã1992幎ãããããã§ããã¯åžã ãã以æ¥å€ãã®ããšãæµããŠããŸãããåé¡ã¯åãã§ãïŒ
ãªãã§ïŒ
以äžã¯ãRubyèšèªã®ã€ã³ã¿ã©ã¯ãã£ããªãèšç®æ©ãã®ã³ãŒãã§ãã
è¡= readline
evalïŒè¡ïŒ.inspectã眮ããŸã
çµãã
ãŸãã¯ãã以äž
whileïŒprint ">"; trueïŒããã³line = readline
evalïŒè¡ïŒ.inspectã眮ããŸã
çµãã
ãŸãã¯å³
' readline 'ã å¿
èŠ
while line = Readline .readlineïŒ " > " ïŒ
å§ãã
eval ïŒè¡ïŒ.inspectã眮ããŸã
ã¬ã¹ãã¥ãŒ => e
puts e.to_s + " \ n " + e.backtrace.joinïŒ " \ n " ïŒ
çµãã
çµãã
å®è¡äŸïŒ
artem @ laptopïŒã/ã¡ã¿ã¬ã¯ãã£ãŒ$ rubyââ console.rb
> 1 + 2
3
>ãããã«ã¡ã¯ã
ãããã«ã¡ã¯ã
> def fibïŒnïŒïŒ0..1ïŒ=== nïŒ 1ïŒfibïŒn-1ïŒ+ fibïŒn-2ïŒçµäº
ãªã
>ïŒ0 ... 10ïŒ.map {| n | fibïŒnïŒ}
[1ã1ã2ã3ã5ã8ã13ã21ã34ã55]
> 1/0
ïŒè©äŸ¡ïŒïŒ1ïŒ `/ 'ã§ïŒ0ã§é€ç®
console.rbïŒ4
ïŒè©äŸ¡ïŒïŒ1
>çµäº
ã¢ã«ãã @ã©ãããããïŒã/ã¡ã¿è¬çŸ©$
ã¹ã¯ãªããèšèªã«ã¯ãæååãååŸããçŸåšã®ã³ã³ããã¹ãã§ïŒã»ãŒïŒ
eval
åŒã³åºãããå Žæã§ããã°ã©ããŒã«ãã£ãŠæžãããããã«ããã®æååãå®è¡ãã
eval
ã¡ãœããããããŸãã
å®éã
eval
ãããªããŒã«ã¯ãã³ã³ãã€ã«ãããããã°ã©ãã³ã°èšèªã§ãå©çšã§ããŸãã
ãšããã§ãç§ã¯
eval
ã¡ãœãããã¡ã¿ããã°ã©ãã³ã°ã«åž°ããããšã¯ããããã®ã¬ãã¹ã³ã§ã¯éåžžã«æ害ãªã¡ãœãããšããåŒã³ãŸãã 察話åã«ããŒ| python | perl | ...-ã·ã§ã«ã¯ããããã䜿çšãã¹ãæ°å°ãªãäŸã®1ã€ã§ãã
eval
ã¡ãœããã®å±éºæ§
eval
以äžã§
eval
ãŸãã
attr_accessor
rubiståå¿è
ã§ãattr_accessorã³ã³ã¹ãã©ã¯ãã䜿çšããŠã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã®å±æ§ã決å®ããŸãããããããã©ããªçš®é¡ã®ç£ã§ããããåžžã«èªèããŠããããã§ã¯ãããŸããã
次ã®ã¹ããŒãã¡ã³ããã
attr_accessor
åŒã®æå³ã
attr_accessor
ãŸãïŒcode
ã¯ã©ã¹ã® æ
attr_accessor ïŒã¿ã€ãã« ãïŒ é·ã
çµãã
ïŒçµæã«ããïŒã³ãŒããšåç
ã¯ã©ã¹ã® æ
def ã¿ã€ãã«
@title
çµãã
def title = ïŒvïŒ
@title = v
çµãã
def length
@length
çµãã
def length = ïŒvïŒ
@length = v
çµãã
çµãã
å®çŸ©ã¯æ¬¡ã®ãšããã§ãïŒ
èçŒã§èŠããšã
attr_accessor
ã¯ãã³ãŒããç°¡æœã§
attr_accessor
ãã®ã«ãããšããããã°ã©ããŒã®å
éšçãªèŠæãæºââããããã䟿å©ã§ãã
attr_accessor
ã³ã³ã¹ãã©ã¯ãã¯ããã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã®æ¬¡ã®å±æ§ã®
attr_accessor
ã¡ãœãããå¿
èŠããšç¿»èš³ã§ããŸãã
attr_accessor
ã³ã³ã¹ãã©ã¯ã
attr_accessor
æªç¥ã®ç£ã§ãã
attr_accessor
ãŸããïŒèªã¿åã-çµã¿èŸŒã¿ã®èšèªã³ã³ã¹ãã©ã¯ãã¯ãããŸããïŒããèªåã§ããã°ã©ã ã§ããéåžžã®ã¡ãœããã§ãã
eval
ã¡ãœããã䜿çšããŠãããè¡ããŸãããã
def attr_accessor ïŒ*ã¡ãœããïŒ
methods.each do | æ¹æ³ |
è©äŸ¡ ïŒ
{
def ïŒ{ã¡ãœãã}
@ ïŒ{ã¡ãœãã}
çµãã
def ïŒ{ method } =ïŒvïŒ
@ ïŒ{ã¡ãœãã} = v
çµãã
}
çµãã
çµãã
ããã§ãå±æ§åã®é
åãåŒæ°ãšããŠåãåã
attr_accessor
ã¯ãå±æ§ã«å¯Ÿå¿ããsetã¡ãœãããšgetã¡ãœãããå®çŸ©ããåååã®ã³ãŒãè¡ãå®è¡ããŸãã
Rubyã«ã¯ã¯ã©ã¹ãŸãã¯ã¡ãœããå®çŸ©ã®æŠå¿µããªãã£ãããã
attr_accessor
ãããªã¡ãœããã
attr_accessor
ãç»å ŽããŸããã ã
class Song
ããšããè¡ãæžããã®ã§ãéåžžã®èšç®ãå®è¡ã§ããæ°ããã³ã³ããã¹ãã«ç§»åããŸãããã
def xxx() ... end
ããšããæ§æã¯ãåŒã®1ã€ã«éãããèšç®çµæã¯åžžã«
nil
ïŒRuby v1.8ã§ã¯ïŒ ïŒããµãŒãããŒãã£ã®å¹æã¯ããã®æ§ç¯ãå®è¡ãããã³ã³ããã¹ãã®ã¯ã©ã¹ã®ã
xxx
ãã¡ãœããã®å€èŠ³ã«çŸããŸãã
é¢æ°å®çŸ©ã¯å®äºããŸãããïŒ ã¯ã©ã¹ã³ã³ããã¹ãã«ç§»åããŸãããïŒ ã©ããªãã³ã»ã³ã¹ïŒ -ã©ãããæ¥ãC ++ããã°ã©ããŒã«å°ããŸãã ã¯ããæ£ç¢ºã«ã
ã
class Song
ãã¯ãã¯ã©ã¹å®çŸ©ãåã«ãã¬ãŒã åãããããŒã ã¹ããŒã¹ã®ã¹ã³ãŒããå€æŽãããç¹å¥ãªã³ã³ããã¹ãã«åãæ¿ããŸãã ã€ãŸãããã®ã³ã³ããã¹ãã§åŒã³åºãããšãã§ããããã€ãã®æ°ããã¡ãœããã衚瀺ãããç¹å®ã®åœä»€ã®å®è¡ã®å€ãšå¹æãå€åãããªã©ã§ãã ãªã©
ããã¹ãã
def xxx() ... end
ãã¯å®éã«ã¯åŒã§ãããRubyä»®æ³ãã·ã³ã«ãã£ãŠå®è¡ãããŸãã åæã«ãã¡ãœããå®çŸ©ã®å
éšã¯å®è¡ãããŸããããã³ãŒãã¯ãã€ãã«å€æãããã¡ãœããåã®äžã«ä¿åãããŸãã
QïŒã¯ã©ã¹ã®ã³ã³ããã¹ãã¯ã©ãããæå³ã§ããïŒ
AïŒããã¯ãåŒselfãäœããã®ã¯ã©ã¹ã«çãããããªã³ã³ããã¹ãã§ãã
次ã®ã³ãŒããå®è¡ããŸãã
ã {1 self .inspect } ããhi1ã眮ãã
ã¯ã©ã¹ abc
ã ïŒ{ self.inspect } ããhi2ã眮ãã
ããã«ã¡ã¯
ïŒ{ self .inspect } ããhi3ã眮ããŸã
çµãã
çµãã
hi1ãšhi2ã®è¡ãå°å·ãããŸãã è¿œå ãããšhi3ã®è¡ã衚瀺ãããŸã
Abc.new.hi
åèšååŸïŒ
artem @ laptopïŒã/ã¡ã¿ã¬ã¯ãã£ãŒ$ rubyââ self_in_contexts.rb
ã¡ã€ã³ããhi1
hi2 from Abc
ïŒ<AbcïŒ0xb7c3d9dc>ã®hi3
ã¢ã«ãã @ã©ãããããïŒã/ã¡ã¿è¬çŸ©$
ããªããæžããšããããç解ããå¿
èŠããããŸã
my_methodïŒarg1ãarg2ïŒ
次ã«ãæ¬è³ªçã«ã
self.
ããæé»çã«åã§çœ®æãããŸãã
self.my_methodïŒarg1ãarg2ïŒ
ãã ããããã2ã€ã®åŒã¯ãå Žåã«ãã£ãŠã¯åçã§ã¯ãããŸãããããšãã°ã my_method
ãprivate
ã¡ãœããã®å ŽåãåŒself.my_method
ã¯private
ã¡ãœããã®åŒã³åºãã§ãšã©ãŒãself.my_method
ãŸãã ãããã¯Rubyå®è£
ã®æ©èœïŒ private
ã¡ãœããïŒã§ãããããããä»ããŠåŒã³åºãããšã®ã§ããªãã¡ãœããããããŸãã
ããŠã話ãæ¢ããŠãã ããã äžèš
attr_accessor
ã³ãŒãã
attr_accessor
ããããã«ããŸãã
ã¯ã©ã¹ ã¢ãžã¥ãŒã«
def attr_accessor ïŒ*ã¡ãœããïŒ
methods.each do | æ¹æ³ |
class_eval ïŒ
{
def ïŒ{ã¡ãœãã}
@ ïŒ{ã¡ãœãã}
çµãã
def ïŒ{ method } =ïŒvïŒ
@ ïŒ{ã¡ãœãã} = v
çµãã
}
çµãã
çµãã
çµãã
ç§ãã¡ã¯äœãããŸãããïŒ
Module
ã¯ã©ã¹ã®ã³ã³ããã¹ãã«ã¡ãœããå®çŸ©ãé
眮ãã
eval
ã
class_eval
眮ãæã
class_eval
ã
ãªããããããã®ã§ããïŒ çç±ããããŸãïŒ
*ã©ã®ãªããžã§ã¯ããå©çšå¯èœã«ãªãããç解ããã«ã¡ãœãããèšè¿°ããã®ã¯è¯ããããŸããã ã¯ã©ã¹ïŒ
Class
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ïŒããã³ã¢ãžã¥ãŒã«ïŒ
Module
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ïŒã®ã³ã³ããã¹ãã§äœ¿çšã§ãã
attr_accessor
ã¡ãœãããèšè¿°ããå¿
èŠããããŸãã
Class
ã¯ã©ã¹ã¯
Module
ã¯ã©ã¹ãç¶æ¿ããããããã®ã¡ãœããã
Module
ã€ã³ã¹ã¿ã³ã¹ã®ã¡ãœãããšããŠå®çŸ©ããã ãã§ååã§ãããã®å Žåãã¢ãžã¥ãŒã«ãšã¯ã©ã¹ã®äž¡æ¹ã§äœ¿çšã§ããŸãã
*
class_eval
ã¡ãœããã«ã¯
eval
ãšã®éãããããç¹ã«åŸè
ã¯ãåŒã
def ... end
ããå®è¡ãããšã
attr_accessor
ã¡ãœããå
ã«ããŒã«ã«ã«ååšãã
attr_accessor
ã¡ãœããã®å®è¡äžã«ã®ã¿äœ¿çšå¯èœãªã¡ãœããã®å®çŸ©ãäœæããŸãïŒããã¯ææžåãããŠããªãæ©èœã
def
inside
def
ãã§ãïŒã
class_eval
ã¡ãœããã¯ãæ£ããã³ã³ããã¹ãã§æå®ãããã³ãŒããå®è¡ãããããã
def
ããç®çã®çµæãçæãå§ããŸãã
class_eval
ã¡ãœãã
class_eval
ã¢ã¯ãã£ãã§ããããã®åŒæ°ãæååã§ã¯ãªããããã¯ã§ããå Žåã«æ£ç¢ºã«ã¡ã¿ããã°ã©ãã³ã°ã§äœ¿çšãããŸãã
ããã§ã³ãŒããæ©èœããŸããã ãããã圌ã¯ééã£ãŠããŸãã ã
class Module
ããšã
class_eval
ãã®ãªããã®ãå«ããä»ã®
ééã£ããœãªã¥ãŒã·ã§ã³ããããŸãã ãããã®1ã€ã次ã«ç€ºããŸãã
def attr_accessor ïŒ*ã¡ãœããïŒ
methods.each do | æ¹æ³ |
è©äŸ¡ ïŒ
{
ã¯ã©ã¹ ïŒ{ self }
def ïŒ{ã¡ãœãã}
@ ïŒ{ã¡ãœãã}
çµãã
def ïŒ{ method } =ïŒvïŒ
@ ïŒ{ã¡ãœãã} = v
çµãã
çµãã
}
çµãã
çµãã
æåŸã®ãªãã·ã§ã³ã¯ããã®ã³ã³ããã¹ãã§åŒ
self
ãçãããã©ããã«å¿ããŠãã¯ã©ã¹ã®ã³ã³ããã¹ãã§ã¯ãªããããåŒã³åºããŠäœãæªãçµæãåŸãããšãã§ãããšããç¹ã§æªãã§ãã äŸïŒ
s = " ã¯ã©ã¹ "
s.instance_eval { attr_accessor ïŒhahaha }
Array .hahaha = 3 ïŒäºæ³å€ã«ãArrayã«ã¯hahahaå±æ§ããããŸã
Array .hahahaã眮ããŸãïŒ
æãéèŠãªããšïŒ
説æãããŠãã
eval
ã䜿çšãã
attr_assessor
å®çŸ©ã¯ãã»ãã¥ãªãã£ã®æ¬ åŠã®ããã«æªãã§ã-æµã®æªæããããããã°ã©ããŒã®æãããããä¿è·ãããŠããŸããïŒ
method
å€æ°ã®å€ãã¡ãœããåã®æå¹ãªæååã§ãªãå Žåãããšãã°ãæååã
llalala(); puts `cat /etc/passwd`; puts
"ãçµæã¯äºæž¬ã§ããŸããã ããã°ã©ã ã®å®è¡äžã«ãšã©ãŒïŒäŸå€ïŒã衚瀺ãããªãå ŽåããããŸãã ããã±ããããã§ã«é£ãã§ããããšãã ãé©ããæ¥ãã§ãããïŒcïŒã ããããçµãããèŠã€ããããªããšãã«é
ãçŸããééãã»ã©æªããã®ã¯ãããŸããã
æåŸã«ã
attr_accessor
ã®å®çŸ©ã®æ£ããããŒãžã§ã³ãæžããŸãã 圌ã¯ãééã£ããã®ãšã¯ç°ãªãããŠããŒã¯ã§ãïŒ
ã¯ã©ã¹ ã¢ãžã¥ãŒã«
def attr_accessor ïŒ*ã¡ãœããïŒ
methods.each do | æ¹æ³ |
method.is_aïŒïŒ Symbol ïŒã§ãªãéãã TypeError .newïŒ " ã¡ãœããåã¯ã·ã³ãã«ã§ã¯ãããŸãã " ïŒãçºçãããŸãã
define_methodïŒã¡ãœããïŒ do
instance_variable_getïŒ " @ ïŒ{ method } " ïŒ
çµãã
define_methodïŒ " ïŒ{ method } = " ïŒ do | v |
instance_variable_setïŒ " @ ïŒ{ method } " ãvïŒ
çµãã
çµãã
çµãã
çµãã
ããã©ã«ãå€ã®attr_accessor
å€ãã®å Žåãããã©ã«ãå€ã§å±æ§ãèšè¿°ããŸãã ã
||=
ããšããã€ãã£ãªã ã䜿çšããŠãããè¡ããŸããããã¯ããåæåãããŠããªãå Žåã¯ãå³ã«ãããã®ã«ãã£ãŠå·Šã«ãããã®ãåæåããããšå€§ãŸãã«å€æããŸãã
class Song def length @length ||= 0 end def title @title ||= "no title" end end Song.new.length
ãã®ãããªæ±ºå®ã®åŸãæ°ããæ²ã®
length
å±æ§ã®å€ã¯0ã«ãªããŸãã
ãã€ã¯ã«ãããšãç§ã¯æ¬²ãã...ãã®ã³ãŒããç§ãæãããã«åäœãããŠãã ãã!!!ïŒ
ã¯ã©ã¹ã®æ
attr_accessorïŒlength ,: default => 0
attr_accessorïŒtitle ,: default => "no title"
çµãã
次ã®è¡ãã
class_eval
ã䜿çšããŠããã¬ãŒãã³ã°ç®çå°çšã®ééã£ãã³ãŒãã
class_eval
ããŸãã
ã¯ã©ã¹ ã¢ãžã¥ãŒã«
def attr_accessor ïŒ*ã¡ãœããïŒ
options = methods.last.is_aïŒïŒ ããã·ã¥ ïŒïŒ methods.popïŒ{}
methods.each do | æ¹æ³ |
class_eval ïŒ
{
def ïŒ{ã¡ãœãã}
\ïŒãã®ããã«æžããªãã§ãã ããïŒ
@ ïŒ{ã¡ãœãã} ïŒ{ " || = ïŒ{ options [ ïŒdefault ] } " if options [ ïŒdefault ] }
çµãã
def ïŒ{ method } =ïŒvïŒ
@ ïŒ{ã¡ãœãã} = v
çµãã
}
çµãã
çµãã
çµããå¥è·¡ããããããããŸãã!!!
ã¯ã©ã¹ã®æ
attr_accessorïŒlength ,: default => 42
çµãã
Song.new.lengthïŒåºå42ã眮ããŸã!!!
ééã£ãã³ãŒããæã
æ©èœããŸãã ããããããã¯ãã¡ããããããæžããããã°ã©ããŒã«ãã£ãŠè§£éãããªãçç±ã§ã¯ãããŸããã
ãããšã
ã¯ã©ã¹ã® æ
attr_accessor ïŒlength ãïŒ default => 42
attr_accessor ïŒtitle ãïŒ default => " no title "
çµãã
puts Song .new.length ïŒoutputs 42 !!!
Song .new.title ïŒoooooops !!!
ç§ãã¡ã¯ç¥ç§çã«ãªããŸãïŒ
artem @ laptopïŒã/ã¡ã¿ã¬ã¯ãã£ãŒ$ rubyââ bad_attr_accessor.rb
42
ïŒè©äŸ¡ïŒïŒ5ïŒãã¿ã€ãã«ãïŒã¹ã¿ãã¯ã¬ãã«ãæ·±ãããïŒSystemStackErrorïŒ
fromïŒè©äŸ¡ïŒïŒ5ïŒãã¿ã€ãã«ã
bad_attr_accessor.rbããïŒ27
ã¢ã«ãã @ã©ãããããïŒã/ã¡ã¿è¬çŸ©$
ãªããããªã«è¿·æãªã®ã§ããïŒ å®éã«ã¯ãåºæ¬çãªåé¡ããããšããããšã§ããæååã«ããã€ãã®ãªããžã§ã¯ããæ¿å
¥ããããšã¯ãåã«äžå¯èœã§ãã
ããã©ã«ãå€ã®
attr_accessor
ã®åé¡ã¯ã
attr_accessor
ããã«æ£ãã解決ãããŸãã
ã¯ã©ã¹ ã¢ãžã¥ãŒã«
def attr_accessor ïŒ*ã¡ãœããïŒ
options = methods.last.is_aïŒïŒ ããã·ã¥ ïŒïŒ methods.popïŒ{}
methods.each do | æ¹æ³ |
method.is_aïŒïŒ Symbol ïŒã§ãªãéãã TypeError .newïŒ " ã¡ãœããåã¯ã·ã³ãã«ã§ã¯ãããŸãã " ïŒãçºçãããŸãã
define_methodïŒã¡ãœããïŒ do
instance_variable_getïŒ " @ ïŒ{ method } " ïŒ||
instance_variable_setïŒ " @ ïŒ{ method } " ããªãã·ã§ã³[ ïŒããã©ã«ã ]ïŒ
çµãã
define_methodïŒ " ïŒ{ method } = " ïŒ do | v |
instance_variable_setïŒ " @ ïŒ{ method } " ãvïŒ
çµãã
çµãã
çµãã
çµãã
ãã®ãããæ€èšããäŸã§ã¯ãã¡ã¿ããã°ã©ãã³ã°ã¯ã¡ãœãããå®çŸ©ããã¡ãœãããèšè¿°ããããã«èŠããŸãã
åå¿è
ã®ã¡ã¿ããã°ã©ããŒããããã®æ€çŽ¢ãã°ãŒã°ã«ã§æ€çŽ¢ããã®ã¯çã«ããªã£ãŠããŸãïŒ
1. rubyââ doc attr_accessor
2. rubyââ doc Kernel
eval3. rubyââ docã¢ãžã¥ãŒã«
class_eval4. rubyââ docãªããžã§ã¯ã
instance_eval5. rubyââ doc Object
is_aïŒæåã®ãªã³ã¯ã¯æ£ããã§ãã
eval
ãããã³äžçŽç©ããã£ãã·ã³ã°ãRPCãDSLã¿ã¹ã¯ãæ°ããæœè±¡åã¬ãã«ã«å°ãããšãã§ãã修食åã¡ãœãããé
延ïŒé
延ïŒã³ã³ãã¥ãŒãã£ã³ã°ã®æŠå¿µãç¶ç¶ãããã¿ãŒã³ãªã©ã«ã€ããŠã¡ã¿ããã°ã©ãã³ã°ãè¡ãæ¹æ³ã«ã€ããŠã¯ãããã°ã®æ¬¡å·ãåç
§ããŠãã ããã