ã¡ã¿ããã°ã©ãã³ã°Ruby
- ä½è : Paolo Perrotta,è§å¾å ¸
- åºç社/ã¡ã¼ã«ã¼: KADOKAWA/ã¢ã¹ãã¼ã»ã¡ãã£ã¢ã¯ã¼ã¯ã¹
- çºå£²æ¥: 2010/08/28
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 18人 ã¯ãªãã¯: 533å
- ãã®ååãå«ãããã° (125件) ãè¦ã
ãRubyããã°ã©ãã®ä¸ã§é常ã«è©ä¾¡ãé«ããã¡ã¿ããã°ã©ãã³ã°Rubyããåã ããæ°ã«ãªã£ã¦ãããã®ã®ãæ®å¿µãªãã絶çããããä¸å¤ãå®ãã¦5000åç¨åº¦ããã¨ãããã¨ã§ãããããããã¦ããã¨ããââå¶ç¶ã«ã大å¦å³æ¸é¤¨ã§çºè¦! ãã¡ã®å¤§å¦ãæç³»ã®åç§å¤§å¦ãªãã§ããâ¦â¦ã
ããã è¦ã¤ããã¨ãã£ã¦ããæ¬ãã®ãã®ã¯ç¦è²¸åºããã®ããæªããã¨ã«è¦ã¤ããã¨ãã®ãããã«ã¯1æéå¼±ããæéããªãã£ããããã¨ããã®ãã¨ããããç®ãéããã¨ã«ãã¤ã¾ããèªãã ãã¨ãã£ã¦ãããã¨ãã°ãæ²è¼ã³ã¼ããåçµãããã¨ãã詳細ã«ãã¼ããä½ã£ããã¨ãã§ã¯ãªãããã¼ã¸ãããã£ã¦æç« ãç解ãããã ãã ã ããããæããã¦ãèªãã ãã¨ãããã®ããçåã¯æ®ãã¨ããã§ãããã¨ããããèªæ¸ã¡ã¢ã¯æ®ãã¦ããã®ã§ãæ°ã«ãªã£ãã¨ããã»é¢ç½ãã£ãã¨ãããããã°ã«æ¸ãã¦ããã¾ãã
define_method
ãdefine_method
ã¯def
ãå©ç¨ãããã¡ã½ãããå®ç¾©ãããã¨ãã§ããã¡ã½ããããã®ãã次ã®ãããªãã¨ãã§ãã¾ãã
str = "hello world" def hoge1 p str end define_method(:hoge2) do p str end hoge1 #=> strãå®ç¾©ããã¦ããªãããerror hoge2 #=> "hello world"
method_missing
ãæ¬æ¥ãã®ã¯ã©ã¹ãã¢ã¸ã¥ã¼ã«ã«ãªãã¡ã½ãããå¼ã³åºããå ´åãNoMethodErrorãçºçãã¾ããããã®method_missing
ãã¤ã³ã¯ã«ã¼ããã¦ããå ´åããã¡ãã代ãã£ã¦å¼ã³åºããã¾ãã
class Hoge def method_missing(method) p "#{method}ãªãã¡ã½ããã¯ãã®ã¯ã©ã¹ã«åå¨ãã¾ãã!" end end hoge = Hoge.new hoge.hello #=> "helloãªãã¡ã½ããã¯ãã®ã¯ã©ã¹ã«åå¨ãã¾ãã!"
ã
ããã®method_missing
ã®ããã«ããä½ããã®å¦çãè¡ãããã¨ãããã®å¤åãæ¤åºããããå²ãè¾¼ãã§å¥ã®å¦çãè¡ã£ããããããã¨ãããã¯ã¨ããããããå®ç¾ããã¡ã½ãããããã¯ã¡ã½ããã¨ãããããæ¬æ¸ã§ã¯ã»ãã«ãRubyã®ããã¯ã¡ã½ãããç´¹ä»ããã¦ãã¾ãã(ex.ã¯ã©ã¹ã«ã¢ã¸ã¥ã¼ã«ãã¤ã³ã¯ã«ã¼ãããããã¨ãæ¤åºããã¡ã½ãã)ããã¨ãããmethod_missing
ã®ã¤ã³ãã¯ããå¼·ãã£ãã
send
ãsend
ã¯å¼æ°ã®ã¡ã½ãããå®è¡ããã¡ã½ããã§ããæ義ã¨ãã¦SymbolãStringãã¡ã½ããã¨ãã¦å®è¡ã§ãããã¨ã«ããã¾ãããã¡ã½ããåã«æ£è¦è¡¨ç¾ãé©ç¨ãã¦å®è¡ããªãã¦ãã¨ãã§ããããã§ãã(ä¾ã¯æãã¤ãã¾ããã)ã
def hoge p "hello world" end sym = :hoge send(:hoge) #=> "hello world"
ãã¾ãsend
ã¯ã¬ã·ã¼ããã¨ããã¨ãã§ãããã®å ´åå¼æ°ã¯ãã®ã¬ã·ã¼ãã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã»ã¯ã©ã¹ã¡ã½ããã¨ãã¦å®è¡ãããã¨ãå¯è½ã§ãã
class Hoge def hello p "hello world" end end hoge = Hoge.new hoge.send(:hello) #=> "hello world"
eval
ãeval
ã¯å¼æ°ãRubyã®ã³ã¼ãã¨ãã¦å®è¡ããã¡ã½ããã§ãã
str = "p hello world" eval(str) #=> "hello world"
ãã¾ãeval
ã«ã¯ã親æããå¤ããã¬ã·ã¼ãã«ã¯ã©ã¹ãã¨ã£ã¦å¼æ°ãã¯ã©ã¹å
ã®ä¸é¨ã¨ã¿ãªãclass_eval
ããã¤ã³ã¹ã¿ã³ã¹ãã¬ã·ã¼ãã«ã¨ãinstance_eval
ãªã©ãããã¾ãã
class Hoge def initialize(str) @str = str end end Hoge.class_eval{ def say p @str end } hoge = Hoge.new("hello") hoge.say #=> "hello" hoge.instance_eval{p @str} #=> "hello"
alias
ãaias
ã¯ã¡ã½ããã«å¥åãã¤ããã¡ã½ããã§ããããããç´°ãã使ãæ¹ãããããã§ããããããå
¨ãã¡ã¢ãã¦ããªãâ¦â¦ãåçã
class Hoge def hello p "hello" end alias :say :hello end hoge = Hoge.new hoge.say #=> "hello"
$SAFE
ã$SAFE
ã¯ã»ãã¥ãªãã£ã¬ãã«ãè¨å®ããã°ãã¼ãã«å¤æ°ã§ããåæå¤ã¯0ã§ãæ大å¤ã¯4ãæ°åã大ãããªãã»ã©ãã»ãã¥ãªãã£ã¬ãã«ãä¸ããã4ã«ããªãã¨ã»ãã¥ãªãã£ä¸å±éºãªãã¨ã¯å
¨ãã§ããªããªãã¾ããããã ãå
·ä½ä¾ãæãã¤ããªãã£ãã®ã¯å
ç·ã
ã¾ã¨ã
ããªãã ãã¨ãã¨ãã®ãªãèªæ¸ã¡ã¢ã«ãªã£ã¦ãã¾ãã¾ããããããã¦ã¦èªãã ããã«ã¡ã¢ãä¸å®å ¨ã§ããã®ä½ããããæéããªãã£ãã¨ã¯ãããããã¯åçã
ãããã§ã¯ç®ç«ã£ããã®ãç´¹ä»ããã ãã§ãããæ¬ã§ã¯ããããé»éè¡ãã¡ã½ãããã©ã®ããã«çµã¿åãããã®ãããããã¯ã©ã®ããã«å®è·µã¸çµã³ä»ãã¦ããã®ãããããããã解説ããã¦ãã¾ããã¨ãããæå¾ã®ã»ãã«ãããActiveRecordã®è§£èª¬ãã¯å§å·»ã§ããã
ãæã«å ¥ãæ©ä¼ãããã°ãæå ã«ããã¦ããããæ¬ã§ãã絶çã ãã©ãä»åº¦æéãããã¨ãã¯å³æ¸é¤¨ã«ããã£ã¦ãåçµããªããä¸èº«ãè¡èã«å¤ãã¦ããããã¨æãã¾ãã