Rubyѧϰ±Ê¼ÇËÄ——Ä£¿é
#Ò»¡¢Ä£¿é¶¨Òå¼°ÒýÓã¬Ä£¿é¾ÍÊÇÒ»¶Î´úÂ룬ÀïÃæÓÐһЩ·½·¨·ÅÒ»Æð¡£
#¶¨ÒåÄ£¿éÓÃmodule...end ¡£Ä£¿éÓëÀà·Ç³£ÏàËÆ£¬µ«ÊÇ£º
#A) Ä£¿é²»¿ÉÒÔÓÐʵÀý¶ÔÏó£»
#B) Ä£¿é²»¿ÉÒÔÓÐ×ÓÀà¡£
include Math
puts sqrt(91);
module Me
def sqrt(a)
puts a*a;
return a*a;
end
PI=3.1415926
end
include Me
puts sqrt(10)
puts Math.sqrt(10);
puts Math::PI;
puts Me::PI;#±äÁ¿ÒýÓÃÓë·½·¨µ÷Óò»Í¬£¬ÓÃ˫ðºÅ£¬¶ø²»ÊǵãºÅ
#¶þ¡¢¿É±ä²ÎÊý
def params(*num)
"This is text sqrt. "
puts num.size
puts num[1]
puts num.inspect
end
params(4,2,3)
#Èý¡¢Ä£¿éµÄµÚÈý¸ö×÷Óã¬ÊµÏÖÀàËÆÓÚ¶àÖؼ̳еŦÄÜ£¬Ò²³ÆMix-in
#ÎÒÃÇÓÐÒ»¸öStudentÀ࣬ÓÐ×ÅPersonÀàµÄÊôÐԺͷ½·¨£¬»¹»á×öÊýѧÌâ——Çóƽ·½¸ù¡£ÒѾÓÐÁËMeÄ£¿é£¬Ö»ÒªMix-in ÔÚStudentÀàÀï¾Í¿ÉÒÔÁË¡£
module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end
class Person
def talk
puts "I'm talking."
end
end
class Student < Person
include Me
end
aStudent=Student.new
aStudent.talk # I'm talking.
puts aStudent.sqrt(20.7,3.3) # 4.54972526643248
#ͨ¹ý“ < ¸¸ÀàÃû ” £¬Ò»¸ö×ÓÀà¿ÉÒԵõ½¸¸ÀàµÄÊôÐԺͷ½·¨£»Í¨¹ý“ includeÄ£¿éÃû ” £¬Ò»¸ö×ÓÀà¿ÉÒԵõ½Ä³¸öÄ£¿éµÄ³£Á¿ºÍ·½·¨¡£À಻Äܱ» include ¡£
#ËÄ¡¢extend,»ìÐ͵ÄÁíÒ»ÖÖÇé¿ö
module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end
class Student
end
aStudent=Student.new
aStudent.extend(Me)
puts aStudent.sqrt(93.1, 25) # 9.64883412646315
#include ·½·¨ÎªÒ»¸öÀàµÄËùÓжÔÏó°üº¬Ä³¸öÄ£¿é£» extend ·½·¨ÎªÒ»¸öÀàµÄij¸ö¶ÔÏó°üº¬Ä³¸öÄ£¿é¡£
#Îå¡¢requireºÍload
#require£¬loadÓÃÓÚ°üº¬Îļþ£»include£¬extendÔòÓÃÓÚ°üº¬Ä£¿é¡£
#require¼ÓÔØÎļþÒ»´Î£¬load¼ÓÔØÎļþ¶à´Î¡£
#require¼ÓÔØÎļþʱ¿ÉÒÔ²»¼Óºó׺Ãû£¬load¼ÓÔØÎļþʱ±ØÐë¼Óºó׺Ãû¡£
#requireÒ»°ãÇé¿öÏÂÓÃÓÚ¼ÓÔØ¿âÎļþ£¬¶øloadÓÃ
Ïà¹ØÎĵµ£º
ÐÂÄê¸Õ¿ªÊ¼,ÒѾÓÐһЩÉúÃ͵ÄÅóÓÑÒª¿ªÊ¼ÐµÄÖ°³¡ÉúÑÄÁË,
²»ÖªµÀ»á²»»áÓöµ½ÕâÑùµÄÎÊÌâ: ÏëÕÒÒ»·ÝRuby on railsµÄ¹¤×÷,ÎÒÓ¦¸ÃÖªµÀÄÄЩ֪ʶ
?
»¹ºÃ,ÎÒ²¢²»ÊÇÏëÖ¸µãÄãʲô,Ö»ÊÇÏë°´ÎÒµÄ˼ά·½Ê½Õ¹¿ª,ÈÃÄãÄÜ"ÎÅ"µ½µã¶«Î÷¼´³É.
¹ÅÔÆ:Öª¼ºÖª±Ë£¬°ÙÕ½²»´ù£»²»Öª±Ë¶øÖª¼º£¬Ò»Ê¤Ò»¸º£» ......
PythonºÍRubyµÄ¶Ô±È£¬¾ÀÕýһЩÎó½â
ÏÂÃæÊÇÎÒÔÚ¿´Á½Æª¹ØÓÚPythonºÍRuby¶Ô±ÈµÄÎÄÕÂʱ£¬Ëù×÷µÄ¾ÀÕý£¬ÔÎĶ¼Êǹ㷺Á÷Ðеģ¬±È½ÏºÃÕÒ¡£
------------------------------------------------------
¡¶rubyºÍpythonµÄ±È½Ï¡·¸üÕýÒ»µãÊÂÇé
1¡¢Îĵµ¡¢¿ªÔ´ÏîÄ¿¡¢¿âÖ§³Ö£¬ÕâЩ¶«Î÷Ruby²»Òª¸úPython±È£¬²»ÊǼ¸¸öÊýÁ¿¼¶µÄÎÊÌ⣬ºÎ±ØÃ²Ë ......
½ñÌì°ÑRuby Shoes´ò°üµÄ³ÌÐò¸´ÖƵ½Ã»Óа²×°Ruby ShoesµÄ»úÆ÷ÉÏ£¬·¢ÏÖËü»¹Ï²»ÁËRuby ShoesµÄ»·¾³°ü¡£
ÕâÈÃÎҸоõºÜ¾ÚÉ¥£¬ºÃ²»ÈÝÒ×ÈëÃÅÁËÒ»ÃÅÓïÑÔ£¬È´ÓöÉÏÁËÎÞ·¨²¿ÊðµÄÎÊÌâ¡£
ÉõÖÁ£¬ÎÒºóÀ´ÏëÒª°ÑÔËÐл·¾³Ç¶Èëµ½Ruby ShoesÖУ¬¶¼Ã»Äܹ»×öµ½¡£
ÕâÑùµÄÎÊÌâÕæÊÇÈÃÎҸоõµ½¶ÔShoesµÄʧÍû¡£
²»¹ý»¹ºÃ£¬³öÁËRuby Shoes£¬»¹Óкܶ ......
ÈýÕßÖ®¼äÇø±ð²¢²»ÏñÄãÏëµÄÄÇôÄÑ£¬Ò²²»»áÏñÓÐЩÎÄÕÂдµÄÄÇô³¤¡£Í¦¼òµ¥µÄ¡£
Ïà֮ͬ´¦£ºÈýÕß¾ùÔÚkernelÖж¨ÒåµÄ£¬¾ùº¬Óаüº¬½øijÎïÖ®Òâ¡£
²»Í¬Ö®´¦£º
1¡¢requre,loadÓÃÓÚÎļþ£¬Èç.rbµÈµÈ½áβµÄÎļþ¡£
2¡¢includeÔòÓÃÓÚ°üº¬Ò»¸öÎļþ(.rbµÈ½áβµÄÎļþ)ÖеÄÄ£¿é¡£
3¡¢requreÒ»°ãÇé¿öÏÂÓÃÓÚ¼ÓÔØ¿âÎļþ£¬¶øloadÔòÓÃÓÚ¼ÓÔØÅ ......