다음 코드 from Ruby Quiz website을 이해하려고합니다.madlib.gsub! ( ( s (*) s * ) ) /, "<% = q_to_a ('\ 1') %>"에서 \ 1의 의미
'\\1'
부분은 madlib.gsub!(/\(\(\s*(.+?)\s*\)\)/, "<%= q_to_a('\\1') %>")
의 하단에서 세 번째 줄이 아닙니다.
'\\1'
은 탈출합니까? 1
의 출처는 어디입니까?
미리 감사드립니다.
이것은 전체 코드입니다.
#!/usr/local/bin/ruby -w
#---
# Excerpted from "Best of Ruby Quiz"
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information.
#---
# use Ruby's standard template engine
require "erb"
# storage for keyed question reuse
$answers = Hash.new
# asks a madlib question and returns an answer
def q_to_a(question)
question.gsub!(/\s+/, " ") # normalize spacing
if $answers.include? question # keyed question
$answers[question]
else # new question
key = if question.sub!(/^\s*(.+?)\s*:\s*/, "") then $1 else nil end
print "Give me #{question}: "
answer = $stdin.gets.chomp
$answers[key] = answer unless key.nil?
answer
end
end
# usage
unless ARGV.size == 1 and test(?e, ARGV[0])
puts "Usage: #{File.basename($PROGRAM_NAME)} MADLIB_FILE"
exit
end
# load Madlib, with title
madlib = "\n#{File.basename(ARGV.first, '.madlib').tr('_', ' ')}\n\n" +
File.read(ARGV.first)
# convert ((...)) to <%= q_to_a('...') %>
madlib.gsub!(/\(\(\s*(.+?)\s*\)\)/, "<%= q_to_a('\\1') %>")
# run template
ERB.new(madlib).run
이'(. +?)'부분의 'Regexp'. '\ 1','\ 2' 등은': gsub'가 사용되지 않을 때'$ 1','$ 2' 등과 같이 결과 괄호의 값을 얻습니다. –
답변이 도움이 되었습니까? –