码迷,mamicode.com
首页 > 其他好文 > 详细

ruby 学习 -- string --1

时间:2015-11-26 19:03:00      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

# define
french_string = "il \xc3\xa9tait une fois"
long_string = <<EOF
Here is a long string
With many paragraphs
EOF

puts long_string.empty?
puts long_string.include? "many"

puts french_string + long_string

# concatenate
hash = { key1: "val1", key2: "val2" }
string = ""
str2 = ""
hash.each{|k,v| string << k.to_s << " is " << v << "\n" }
hash.each{|k,v| str2 << "#{k}" << " is " << "#{v}" << "\n"}
puts string
puts str2

# join
data = [1, 2, 3]
s = ‘‘
data.each { |x| s << x <<  and a }
puts s # => "1 and a 2 and a 3 and a "
puts data.join( and a )

# number
number = 5
puts "The number is #{number}." # => "The number is 5."
puts "The number is #{5}." # => "The number is 5."
puts "The number after #{number} is #{number.next}."
# => "The number after 5 is 6."
puts "The number prior to #{number} is #{number-1}."
# => "The number prior to 5 is 4."
puts "We‘re ##{number}!" # => "We‘re #5!"
puts "I‘ve set x to #{x = 5; x += 1}."

# Escaping
puts "\#{foo}"
puts #{foo}
# puts "#{foo}" # error because no variable of foo defined.
template = Oceania has always been at war with %s.
puts template % Eurasia # => "Oceania has always been at war with Eurasia."

puts To 2 decimal places: %.4f % Math::PI
puts Zero-padded: %.3d % Math::PI

JSP, ASP type

require erb
template = ERB.new %q{Chunky <%= food %>!}
food = "bacon"
puts template.result(binding) # => "Chunky bacon!"
food = "peanut butter"
puts template.result(binding) # => "Chunky peanut butter!"
puts template.result

 

ruby 学习 -- string --1

标签:

原文地址:http://www.cnblogs.com/snow-backup/p/4998468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!