Главная
translate
Войти
Регистрация
Форум
Блоги
Пользователи
Ресурсы
Список джемов
Поиск
Loading...
Login form
Ruby On Rails in UA
/
Code Review, Вопросы по теории программирования
/
Сборник рецептов для String
/
Замена переменных в строках ERB
назад
новый
ответить
2008-06-10 12:59:30
Ответить
Ruslan Voloshin
Адрес:
odessa
Сообщений: 1201
Регистр: 2007-03-13
его блог
40 сообщ.
Замена переменных в строках ERB
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 44 problems = [] template.run(binding) # Looks like your code is clean! #--- class String def substitute(binding=TOPLEVEL_BINDING) eval(%{"#{self}"}, binding) end end template = %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")}! #---
ERB
,
erb.result
,
substitute
назад
новый
ответить
Реклама: Сгорел у тещи? Бери качественный
матрац
только в нашем магазине www.consul.su! Ремонтируете филиал? Покупайте качественную мебель
нарбутас
на сайте riys. Ru
Ключевые слова:
to_i [1]
create_tree [1]
$@ [1]
propedit [1]
rails 2.0 [7]
сервер [2]
notification [1]
each [2]
GMail [1]
css [2]
root [1]
rails and tiny_mce [1]
mysql [13]
консультант [1]
REST Rails2.0 [1]
Ruby2CExtension [1]
Html Validator [1]
median [1]
smtp [3]
db encoding [1]
Гости: 94 Онлайн:
Артемий Силивончик
,
О проекте
по всем вопросам обращайтесь на
support
Rubyclub.com.ua
Copyright © 2007 - 2008