標籤:
# definefrench_string = "il \xc3\xa9tait une fois"long_string = <<EOFHere is a long stringWith many paragraphsEOFputs long_string.empty?puts long_string.include? "many"puts french_string + long_string# concatenatehash = { 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 stringputs str2# joindata = [‘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 ‘)# numbernumber = 5puts "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}."# Escapingputs "\#{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::PIputs ‘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