Skip to content

Instantly share code, notes, and snippets.

@zdavatz
Created March 15, 2011 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zdavatz/870425 to your computer and use it in GitHub Desktop.
Save zdavatz/870425 to your computer and use it in GitHub Desktop.
index.rb
def proc_resolve_search_term # :nodoc:
if(@proc_resolve_search_term.nil?)
src = <<-EOS
Proc.new { |origin|
values = {}
EOS
@resolve_search_term.each { |name, info|
src << <<-EOS
begin
values.store('#{name}', origin.#{info['resolve']})
rescue NameError
end
EOS
}
src << <<-EOS
values
}
EOS
@proc_resolve_search_term = eval(src)
end
@proc_resolve_search_term
end
@mhatakeyama
Copy link

EOS is Ruby heredocument.

Sample for a nested Hash:
hash = {'name' => {'resolve' => 'value'}}

hash.each do |key, value|
p value['resolve']
end

@zdavatz
Copy link
Author

zdavatz commented Mar 15, 2011

Strings can continue across multiple input lines, in which case they will contain newline characters. It is also possible to use here documents to express long string literals. Whenever Ruby parses the sequence <<identifier or <<quoted string, it replaces it with a string literal built from successive logical input lines. It stops building the string when it finds a line that starts with the identifier or the quoted string. You can put a minus sign immediately after the << characters, in which case the terminator can be indented from the left margin. If a quoted string was used to specify the terminator, its quoting rules will be applied to the here document; otherwise, double-quoting rules apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment