摘要:
类似于C,一般用'\'作为转义字符如'escape using "\\"' 将显示 escape using "\" 'That\'s right' 显示 That's right#{expr}序列把任何Ruby代码的值放入字符串中"Seconds/day: #{24*60*60}" 得到 Seconds/day: 86400"#{'Ho! '*3'}Merry Christmas!" 得到 "Ho! Ho! Ho! 阅读全文
摘要:
irb(main):020:0> f=File.open("/home/dongjichao/c/3_1.c")=> #<File:/home/dongjichao/c/3_1.c>irb(main):021:0> f.each do |line|irb(main):022:1* puts lineirb(main):023:1> end#include <stdlib.h>int main(void){ printf("ddddd\n"); return EXIT_SUCCESS;}=> #< 阅读全文
摘要:
irb(main):012:0> def n_times(thing)irb(main):013:1> return lambda {|n| thing * n}irb(main):014:1> end=> nilirb(main):015:0> p1=n_times(23)=> #<Proc:0x 阅读全文
摘要:
ruby中整数支持times,upto,downto,step等几种迭代irb(main):010:0> 3.times {print "X " }X X X => 3irb(main):011:0> 1.upto(5) {|i| print i," "}1 2 3 4 5 => 1irb(main):012:0> 99.downto(95) {|i| print i," "}99 98 97 96 95 => 99irb(main):013:0> 50.step(80,5) {|i| pri 阅读全文
摘要:
在ruby中方法与block的合用:~/ruby$ irbirb(main):001:0> def take_block(p1)irb(main):002:1> if block_given?irb(main):003:2> yield(p1)irb(main):004:2> elseirb(main):005:2* p1irb(main):006:2> endirb(main):007:1> end=> nilirb(main):008:0> take_block("no block")=> "no bloc 阅读全文