• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Ruby(2008-02-10)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

#############################  Array And Hash  #######################
#array with three elements
a = [1, 'ca', 3.14]

#access the first element
puts(a[0])
#set the third element(nil means nothing)
a[2] = nil
#dump out the array
puts a

#the other way of creating arrays of words
b = %w{cat dog ca}
puts b[1]

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

puts inst_section['oboe']

#create empty hash
#zero is default value, you can specify another default value.
histogram = Hash.new(0)
puts histogram['key1']

########################################  Control Structures   ##########################################
count = 0
# if statements
if count > 10
 puts "Try again"
elsif count == 3
 puts "You lose"
else
 puts "Enter a number"
end

#while statements
while count >= 0 and count < 100
 puts count
 
 count = count + 1
end

#gets method returns the next line from the standard input stream or nil when end of file is reached.
while line = gets
 puts line.downcase
end

#statement modifiers
puts "Danger, Will Robinson" if radiation > 3000

square = 2
square = square * square while square < 1000
 
######################################  Regular Expressions  ##########################################
/Perl|Pathon/
/P(erl|athon)/
/ab+c/
/ab*c/

#a time such as 12:34:56
/\d\d:\d\d:\d\d/
#Perl, zero or more other chars, then Python
/Perl.*Python/
#Perl, a space, and Python
/Perl Python/
#Perl, zero or more spaces, and Python
/Perl *Python/
#Perl, one or more spaces, and Python
/Perl +Python/
#Perl, whitespace characters, then Python
/Perl\s+Python/
#Ruby, a space, and either Perl or Python
/Ruby (Perl|Python)/

#match operator
if line =~ /Perl|Python/
 puts "Scripting language mentioned: #{line}"
end

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ruby on rails开发从头来(windows)(十三)-订单(Order)发布时间:2022-07-13
下一篇:
ruby执行字符串代码发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap