# 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
时间: 2024-11-05 18:42:14