⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02 - substituting variables into an existing string.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
template = 'Oceania has always been at war with %s.'template % 'Eurasia'# => "Oceania has always been at war with Eurasia."template % 'Eastasia'# => "Oceania has always been at war with Eastasia."'To 2 decimal places: %.2f' % Math::PI      # => "To 2 decimal places: 3.14"'Zero-padded: %.5d' % Math::PI              # => "Zero-padded: 00003"#---require 'erb'template = ERB.new %q{Chunky <%= food %>!}food = "bacon"template.result(binding)                       # => "Chunky bacon!"food = "peanut butter"template.result(binding)                       # => "Chunky peanut butter!"#---puts template.result# Chunky peanut butter!#---template = %q{<% if problems.empty? %>  Looks like your code is clean!<% else %>  I found the following possible problems with your code:  <% problems.each do |problem, line| %>    * <%= problem %> on line <%= line %>  <% end %><% end %>}.gsub(/^\s+/, '')template = ERB.new(template, nil, '<>')problems = [["Use of is_a? instead of duck typing", 23],	    ["eval() is usually dangerous", 44]]template.run(binding)# I found the following possible problems with your code:# * Use of is_a? instead of duck typing on line 23# * eval() is usually dangerous on line 44problems = []template.run(binding)# Looks like your code is clean!#---class String  def substitute(binding=TOPLEVEL_BINDING)    eval(%{"#{self}"}, binding)  endendtemplate = %q{Chunky #{food}!}                     # => "Chunky \#{food}!"food = 'bacon'template.substitute(binding)                       # => "Chunky bacon!"food = 'peanut butter'template.substitute(binding)                       # => "Chunky peanut butter!"#---food = '#{system("dir")}'puts template.substitute(binding)# Chunky #{system("dir")}!#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -