ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

#

##闭包的常见用法  
[ 'cat', 'dog', 'horse' ].each {|name| print name, " " }
5.times { print "*" }
3.upto(6) {|i| print i } ##3..6又一写法
('a'..'e').each {|char| print char }

##输出函数的格式字符串
printf("Number: %5.2f,\nString: %s\n", 1.23, "hello")
posted @ 2006-09-12 15:59 ivaneeo 阅读(279) | 评论 (0)编辑 收藏

#############################################
##闭包的一个用法
animals = %w( ant bee cat dog elk ) # create an array
animals.each {|animal| puts animal } # iterate over the contents


#############################################
  def call_block
puts "Start of method"
yield ##执行传入的参数.这里为"In the block"
yield
puts "End of method"
end

call_block { puts "In the block" }

##############################################
posted @ 2006-09-12 15:52 ivaneeo 阅读(255) | 评论 (0)编辑 收藏

##块的用法
{ puts "Hello" } # this is a block

do ###
club.enroll(person) # and so is this
person.socialize #
end ###


posted @ 2006-09-12 15:49 ivaneeo 阅读(238) | 评论 (0)编辑 收藏

line = "abc"
if line =~ /Perl|Python/ ##匹配正则表达式
puts "Scripting language mentioned: #{line}"
end


line.sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby'
line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby'

posted @ 2006-09-12 15:29 ivaneeo 阅读(244) | 评论 (0)编辑 收藏

  histogram = Hash.new(0)	##给hash表设置默然值为0
histogram['key1']
histogram['key1'] = histogram['key1'] + 1
histogram['key1']

######################################
##gets 从命令行出入的文件名得到的文件的每一行文本。
while line = gets
puts line.downcase ##downcase全部转换成小写
end
posted @ 2006-09-12 15:22 ivaneeo 阅读(303) | 评论 (0)编辑 收藏

##数组
a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
a[0]
a[3]
# this is the same:
a = %w{ ant bee cat dog elk }
a[0]
a[3]

##hash表
inst_section = {
'cello' => 'string',
'clarinet' => 'woodwind',
'drum' => 'percussion',
'oboe' => 'woodwind',
'trumpet' => 'brass',
'violin' => 'string'
}

inst_section['oboe']
inst_section['cello']
inst_section['bassoon']
posted @ 2006-09-12 15:10 ivaneeo 阅读(244) | 评论 (0)编辑 收藏

 a = [ 1, 'cat', 3.14 ]   # array with three elements
# access the first element
a[0]
# set the third element
a[2] = nil
# dump out the array
a
posted @ 2006-09-12 15:07 ivaneeo 阅读(215) | 评论 (0)编辑 收藏

  def say_goodnight(name)
result = "Good night, #{name}" ##把变量name的值替换进字符串,ruby使用#{var}.
return result
end
puts say_goodnight('Pa')
##########################################################

def say_goodnight(name)
result = "Good night, #{name.capitalize}" ##s.capitalize 第一个子母变为大写
##s.capitalize!
##Returns a copy of s with the first character converted to uppercase and the remainder to lowercase.
##"fooBar".capitalize # => "Foobar"
return result
end
puts say_goodnight('uncle')
##########################################################

$greeting = "Hello" # $greeting is a global variable
@name = "Prudence" # @name is an instance variable
puts "#$greeting, #@name"

##########################################################
def say_goodnight(name) ##不用使用return语句也可以返回
"Good night, #{name}"
end
puts say_goodnight('Ma')

posted @ 2006-09-12 15:05 ivaneeo 阅读(276) | 评论 (0)编辑 收藏

 number = -123	#数字都有Math类的方法
 number = number.abs
posted @ 2006-09-12 14:53 ivaneeo 阅读(220) | 评论 (0)编辑 收藏

  song = 1
sam = ""
def sam.play(a) #定义方法
"duh dum, da dum de dum ..."
end
  "gin joint".length	#所有都是以Object为root
"Rick".index("c")
-1942.abs
sam.play(song)
posted @ 2006-09-12 14:50 ivaneeo 阅读(225) | 评论 (0)编辑 收藏

仅列出标题
共67页: First 上一页 22 23 24 25 26 27 28 29 30 下一页 Last