I am writing a method inside MimeTypes
class that iterates over contents and creates a Hash
of extension as key and mime type as value. Here is my rspec snippet.
let(:mime_content) do
<<-FILE_CONTENT
# Mime type extension
image/png png
image/jpeg jpeg jpg jpe
FILE_CONTENT
end
let(:mime_types) { WebServer::MimeTypes.new(mime_content) }
I am not sure how to iterate over block of text. I am not sure what <<-FILE_CONTENT
does. I am assuming it is creating a block of text and assigning it to mime_content
. Please help.
You can use let() like this:
let(:var_name) { "some string" }
That is equivalent to:
let(:var_name) do
"some string"
end
Ruby also has a Perl feature called Heredoc, which allows you to do stuff like this:
text = <<END_OF_STRING
hello world
goodbye mars
help
get lost
END_OF_STRING
text will then contain the string:
"hello world\ngoodbye mars\nhelp\nget lost\n"
Heredoc sytnax allows you to do some tricky things, like:
puts(<<END_OF_TEXT)
hello world
goodbye mars
END_OF_TEXT
No one really knows all the tricky places you can insert <<SOME_UNIQUE_ID
into code, and so Heredocs can be quite confusing.
In your example, apparently the Heredoc syntax is being used to create a multi line string inside a block. Normally, the matching ending terminator has to be on a line by itself AND not indented. The preceding dash, <<-FILE_CONTENT
, means the matching terminator WILL be indented.
You can also put single quotes around the identifier, to create a single quoted string:
<<'FILE_CONTENT'
Personally, I would write the example like this:
text =<<'FILE_CONTENT'
blah
blah
blah
FILE_CONTENT
let(:mime_content) { text }
I am not sure how to iterate over block of text.
How do you want to iterate over the block of text? By character? By word? By sentence? By line? By paragraph? See the String class for the various methods.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加