现在的位置: 首页 > 综合 > 正文

Ruby(2008-02-16)

2013年01月10日 ⁄ 综合 ⁄ 共 793字 ⁄ 字号 评论关闭

############################  Block   #######################
def call_block
 puts "start of method"
 yield
 yield
 puts "end of method"
end
call_block {puts "in the block"}

def call_block
 yield("hello", 99)
end
call_block{|str, num| puts str}

#################################  iterators    ################
animals = %w(ant bee cat dog elk)
animals.each{|animal| puts animal}

['cat', 'dog', 'horse'].each{|name| print name, " "}
5.times{print "*"}
3.upto(6){|i| print i}
('a'..'e').each{|char| print char}

##########################   Reading and 'Riting   #############
#puts  writes its arguments, adding a newline after each. print  also writes
#its arguments, but with no newline.
#Another output method we use a lot is printf, which prints its arguments under the
#control of a format string (just like printf in C or Perl).

printf("Number: %5.2f,\nString: %s\n", 1.23, "hello")

line = gets
printf line

抱歉!评论已关闭.