Ruby 101£º·½·¨¶ÔÏó
Ruby 101£º·½·¨¶ÔÏó
Written by Allen Lee
´Ó·½·¨µ÷ÓÃ˵Æð
ÔÚÉÏһƪÎÄÕÂÀÎÒÃÇ¿´µ½µ÷ÓöÔÏóµÄ·½·¨ÊµÖÊÉÏÊÇÏò¶ÔÏó·¢ËÍÏûÏ¢£¬ÏÂÃ棬ÎÒÃÇÔÙÀ´¿´Ò»¸öÓÐȤµÄÓ¦Óá£ÔÚRubyÀ×Öµä¿ÉÒÔͨ¹ý {key => value} À´´´½¨£¬Èç¹ûÄãʹÓõİ汾ÊÇ1.9»òÒÔÉÏ£¬µ±keyµÄÀàÐÍÊÇSymbolʱ£¬´´½¨×ÖµäµÄÓï·¨¿ÉÒÔ½øÒ»²½¼ò»¯Îª {key: value} £¨×¢Ò⣬ðºÅÒª½ôÌùÔÚkeyºóÃ棩£¬ÕâʹµÃÎÒÃÇ¿ÉÒÔ´´½¨ÕâÑùµÄ¶ÔÏó£º
´úÂë 1
ÓÐûÓоõµÃÕâ¸öд·¨ºÜÃæÊ죿ÓÐЩͬѧ¿ÉÄÜÒѾ¿´³öÀ´ÁË£¬Õâ¸öд·¨ºÜÏñJSON£¬ÊÂʵÉÏ£¬Õâ¸ö´úÂëÕýÊÇ·ÂÕÕWikipediaÉϵÄJSONʾÀý´úÂëд³öÀ´µÄ£¬È»¶ø£¬ÓÉÓÚËü±¾ÉíÊÇÒ»¸ö×Öµä¶ÔÏó£¬ÔÚ·ÃÎÊÀïÃæµÄÄÚÈÝʱÐèҪʹÓÃ×ÖµäµÄÓï·¨£º
´úÂë 2
ÏÖÔÚ£¬Çë˼¿¼Ò»Ï£¬ÓÐûÓа취ʹËü½ÓÊÜÕâÑùµÄ×ö·¨ÄØ£º
´úÂë 3
ÓÐЩͬѧ¿ÉÄÜÒѾ·´Ó¦¹ýÀ´ÁË——´ò¿ªHashÀ࣬ÖØдmethod_missing·½·¨£º
´úÂë 4
µ±ÎÒÃǵ÷ÓÃfirst_nameºÍphone_numbersµÈ²»´æÔڵķ½·¨Ê±£¬¾Í»á´¥·¢method_missing·½·¨£¬ËüÊ×Ïȼì²é×ÖµäÊÇ·ñ°üº¬Õâ¸ö·½·¨Ãû£¬ÈôÊÇ£¬·µ»Ø¶ÔÓ¦µÄÖµ£¬·ñÔò£¬×ª½»super´¦Àí¡£ÏÂÃ棬ÎÒÃÇÖ´ÐÐһϴúÂ룺
ͼ 1
ºÜºÃ£¬»ù±¾ÉÏ´ïµ½ÎÒÃǵÄÔ¤ÆÚÁË£¬µ«ÊÇ£¬ÕâÖÖ×ö·¨ÖÁÉÙ´æÔÚÁ½¸ö±×²¡£¬µÚÒ»£¬ËüÇ¿ÖÆËùÓÐ×ÖµäʹÓÃÕâÖÖÖض¨ÏòÂß¼£¬µÚ¶þ£¬Èç¹û×ÖµäµÄÏÖÓз½·¨ºÍËüµÄ¼ü³öÏÖÖØÃû£¬Öض¨ÏòÂß¼½«±»ºöÂÔ£¬ÒòΪÏÖÓз½·¨½«±»ÓÅÏȵ÷Ó㬲»»á´¥·¢method_missing·½·¨£¬ÄÇô£¬Ôõô½â¾öÕâÁ½¸öÎÊÌ⣿àÅ……ºÜ±§Ç¸£¬ÕâЩÎÊÌâ²»ÊǽñÌìµÄÖ÷½Ç£¬»¹ÊÇ´ýµ½ËüÃǵÄÖ÷Ï·ÉÏÑÝ֮ʱÔÙÐзֽâ°É……
ÏÖÔÚ£¬Çë˼¿¼Ò»¸öÎÊÌ⣬µ±ÎÒÃǵ÷ÓÃÒ»¸ö·½·¨Ê±£¬ÎÒÃÇÖ»Ðè"Ö±ºôÆäÃû"£¬Ïñ´úÂë3µÄfirst_nameºÍcityµÈ·½·¨ÄÇÑù£¨µ±È»£¬¼ÓÉÏ"()"Ò²ÊÇ¿ÉÒԵģ¬¼´person1.first_name()£©£¬ÕâÒâζ×Å£¬µ±ÎÒÃÇÒýÓÃÒ»¸ö·½·¨µÄÃû×Öʱ£¬ÎÒÃÇʵ¼ÊÉÏÔÚÒýÓÃËüµÄ·µ»ØÖµ£¬ÄÇô£¬Èç¹ûÎÒÏë°ÑÒ»¸ö·½·¨±¾Éí¶ø²»ÊÇËüµÄ·µ»ØÖµ×÷Ϊ²ÎÊý´«¸øÁíÒ»¸ö·½·¨ÄØ£¿¿¼ÂÇÕâÑùÒ»¸öÇé¾°£¬ÎÒÓÐÒ»¸ö¹ºÎï³µ£¬ÄÚÓÐͼÊéÈô¸É£º
´úÂë 5
ͼÊéͨ¹ýcalc_preferential_price·½·¨¼ÆËãÓŻݼ۸ñ£º
´úÂë 6
ÏÖÔÚ£¬ÎÒÏë´´½¨Ò»¸öcheck_out·½·¨£¬ÓÃÀ´¼ÆËã»õ¿î£º
´úÂë 7
ÄÇô£¬ÎÒ¸ÃÈçºÎ°Ñcalc_preferential_price·½·¨´«¸øcheck_out·½·¨£¿
Ö±½Ó°Ñ·½·¨µÄÃû×Ö´«¸øËü¿Ï¶¨²»ÐУ¬ÒòΪ"Ö±ºôÆäÃû"Òâζ×ŵ÷ÓÃËü£¬µÃµ½µÄÊÇËüµÄ·µ»ØÖµ
Ïà¹ØÎĵµ£º
·ÖΪÈçϼ¸½Ú:
·»ù±¾ÊµÏÖ
·ÔÚµü´úÖÐÒýÓÃÔÀ´µÄ¶ÔÏ󣬻òÕßÖ±½Ó¸Ä±äÊý×éµÄÖµ¶ø²»ÊÇ·µ»ØÒ»¸öÐÂÊý×é
·Ïòµü´ú´«ÈëÎÞÏÞ¶àµÄ²ÎÊý
·»ù±¾ÊµÏÖ
½ñÌìͻȻ·¢ÏÖjsµÄÊý×é´¦ÀíÆðÀ´ÕæÊÇÂé·³,´úÂëһЩ¾ÍÊÇÒ»´ó¶Ñ,Ïà±ÈÆðrubyµÄµü´úÆ÷À´ÕæÊÇÑ·É«²»ÉÙ,Ö÷ÒªÊÇҪдµÄ´úÂëÌ«¶àÁË,Ò²ÐíÊÇjsÓÐÌØÊâµÄ´¦ÀíÊý×éµÄ·½Ê ......
def delVss(path)
if File.directory?(path)
for f in d = Dir.open(path)
fpath = File.join(path, f)
if(f!="."&&f!="..")
......
Ò» °²×°ruby
$sudo apt-get install ruby irb rdoc
¶þ °²×°gem
1.µ½ÕâÀïÏÂÔØ ,×îºÃÊÇ×îа汾£¬ÎÒµÄ1.3.5
½âѹ£¬Çл»µ½µ±Ç°Ä¿Â¼£¬Ö´ÐÐ$sudo ruby setup.rb
»òÕßÕâÑù£º
$ tar xzvf rubygems-1.3.5.tgz £¨½âѹ£©
$ cd rubygems-1.3.5 £¨Çл»µ½´ËĿ¼£©
$ sudo ruby setup ......
SleekEngine
Ëæ׿ÆËã»úÓ²¼þµÄ¿ìËÙ·¢Õ¹£¬10ÄêÇ°ÒòΪËÙ¶ÈÎÊÌâ¶ø±»Ú¸²¡µÄJavaÏÖÔÚ³ÉÁ˼ÆËã»úÁ÷ÑÔÓïÑÔÖеÄÀϴ󣬵«ÊÇËüÈ´ÈÔ±»ÈÏΪ¸´ÔÓ±¿ÖØ£¬½ü¼¸ÄêÀ´¶¯Ì¬Óï
ÑÔÈ´ÓкóÀ´¸ÏÉϵÄÇ÷ÊÆ£¬ÕÆÎÕÒ»ÃŶ¯Ì¬ÓïÑÔ³ÉÁ˳ÌÐòÔ±ÐèÒª¿¼ÂǵÄÊÂÒË¡£ ½üÆÚ½Ó´¥Á˼¸ÖÖÁ÷ÐеĶ¯Ì¬ÓïÑÔPython, Ruby,
Groovy£¬ËüÃÇÏà±È¾²Ì¬ÓïÑÔJava/C++À´Ëµ£¬´óÎ ......
ÓÐЩͬѧÌáÎÊ£ºÎÒдÊý¾Ýµ½ÎļþÖлá°ÑÀϵÄÊý¾ÝÌæ»»µô¡£
ÔõôÔÚÔ´ÎļþµÄ»ù´¡ÉÏÌí¼ÓÄÚÈÝ£¬¶ø²»ÊÇÖØд
¡¾ruby code¡¿
def txt_write(value,memo)
File.open("c:\\my_text.txt", 'a') do |f|
......