ÏÂÃæÊÇÊä³öÒ»¸öСÓÚ100µÄFibnancciÊýÁУº
class test1
def Fib(max)
i1,i2 = 1,1
while i1 <= max
yield i1
i1,i2=i2,i1+i2
end
end
end
Fib(100){|f| print f," "}
ÓÉÓÚÓе㿴²»¶®£¬ÏÖÔÚÎÒ¸ÄÒ»ÏÂ
class test2
def test2.Fib
i1,i2 = 1,1
i1,
i2=i2, ££È¥µô¾Í±¨´í
i1+i2
end
end
ÎÒÏëÎʵÄÊÇ£º
1.ÔÚtest2ÀïÃæFibµÄ·µ»ØÖµÊÇÔõôËãµÄ£¿ÊDz»ÊÇ[i1,i1+i2]?Ϊʲôȥµôi2=i2»á¾Í±¨´í£¿
2.test1µÄwhileÑ»·ÊÇÔõôִÐеģ¿ÎÒµÄÏë·¨ÊÇ
µÚÒ»´ÎÑ»·,i1=1
yield 1
[1,2]
µÚ¶þ´ÎÑ»·,i1=1
yield 1
[1,3]
µÚÈý´ÎÑ»·,i1=1......²»¶Ô¾¢ÁË...
yield 1
[1,4]
..........
²Åѧruby£¬Ï°¹ßÁËc#ºÍjavaʽµÄÓï·¨£¬¾õµÃrubyÓï·¨ÕæËûÄïµÄ¹îÒì¡£¡£¡£
i1,i2=i2,i1+i2
ÀàËÆ£ºi1=i2;
i2=i1+i2;
rubyµÄº¯Êý£¬Èç¹ûûÓÐÃ÷È··µ»ØÖµ£¬¾Í·µ»Ø×îºóÒ»¸ö±í´ïʽµÄÖµ£¬
test2ÀïÃæFibÀïÊÇ
i1,
i2=i2, ££È¥µô¾Í±¨´í
i1+i2
ÎÒÕâ±ßÖ´ÐÐÆðÀ´ÊÇ12£¬¼´ÏÈ´òÓ¡i1£¬ºó´òÓ¡i2
test1µÄwhileÑ»·
µÚÒ»´Î
yield 1
[1, 2]
µÚ¶þ´Î
yield 1
Ïà¹ØÎÊ´ð£º
Ruby ´úÂëÈçÏ£º
class Array
def uniq_by
seen = {}
select{ |x|
v = yield(x)
!seen[v] && (seen[v]=true)
}
end
end
Çója ......
ÎÒÊÇÓÃnetbeans È¥寫bnf
a program will ONLY accept strings based on the following grammar:
<compute> --> <value> = < ......
Êý×Ö´ÓÓ񱧮ðÿÈýλÓÃÒ»¸ö¶ººÅ¸ô¿ª
1232445 ⇒ 1,232,445
---- ChangeStr.rbÄÚÈÝ ------------------------------------------
def change_str(num)
str = num.to_s
nil while str. ......
ÐÂÄê¸Õ¿ªÊ¼,ÒѾÓÐһЩÉúÃ͵ÄÅóÓÑÒª¿ªÊ¼ÐµÄÖ°³¡ÉúÑÄÁË,
²»ÖªµÀ»á²»»áÓöµ½ÕâÑùµÄÎÊÌâ: ÏëÕÒÒ»·ÝRuby on railsµÄ¹¤×÷,ÎÒÓ¦¸ÃÖªµÀÄÄЩ֪ʶ ?
»¹ºÃ,ÎÒ²¢²»ÊÇÏëÖ¸µãÄãʲô,Ö»ÊÇÏë°´ÎÒµÄ˼ά·½Ê½Õ¹ ......
ÎÒÏÖÔÚ²»µÃ²»ÃæÁÙÒ»¸öÑ¡Ôñ£º A£¬Ìø²Û¼ÌÐø×öJ2EE. B,תÐÍ×örailsµÄ¿ª·¢
ÔÚй«Ë¾¹¤×÷ÒѾ°ëÄ꣬Õâ±ßµÄ·ÕΧ±È½ÏÇáËÉÓä¿ì£¬µ«ÊÇÎÒ¸ºÔðµÄÒ»¸öJavaÏîÄ¿ÒѾÉÏÏߣ¬°´ÕÕpmµÄÒªÇó£¬ÈÃÎÒתÐÍ×örails¿ª·¢¡£¾ÍÎÒѧϰrailsµÄ¹ý³ ......