How to iterate over block of text

Kumari Sweta

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.

7stud

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Kotlin - how to iterate over a map or a list inside a builder block

分類Dev

How to iterate over and filter an array?

分類Dev

how to iterate over tuple items

分類Dev

How to iterate over the lines in a string?

分類Dev

How to iterate over dates in a dataframe?

分類Dev

How to read the first element of all Rows in a text file and then iterate over it in Powershell scripting?

分類Dev

How can we iterate over an HashMap in JSTL?

分類Dev

How to define and iterate over map in Jenkinsfile

分類Dev

How to iterate over keys of a generic object in TypeScript?

分類Dev

In Elm, how can I iterate over a map?

分類Dev

How to iterate over a dictionary and operate with its elements?

分類Dev

How elegantly iterate over list or dictionary

分類Dev

[Swift[ How to iterate over all the properties of an object

分類Dev

"How to unevenly iterate over two lists"

分類Dev

How to iterate over multiple lists simultaneously

分類Dev

How to iterate over and access individual values in a dictionary

分類Dev

How to iterate over MethodCall object to get the parameters

分類Dev

How to iterate over properties of Powershell instance

分類Dev

How to iterate over multiple function arguments?

分類Dev

How to iterate over an xml and save it into a dataframe in Python?

分類Dev

How to iterate over a list of floats in python

分類Dev

How to send an array of maps and iterate over it using gin-templating

分類Dev

How do I iterate over multiple, discontinuous ranges in a bash for loop

分類Dev

How to iterate over a collection of structs as an iterator of trait object references?

分類Dev

How to iterate over Sudoku sub-grid using c++?

分類Dev

How Camel split/iterate over the list and send to queue individually?

分類Dev

How to query and iterate over array of structures in Athena (Presto)?

分類Dev

how to iterate over a dictionary from value in an array and store in a new dictionary

分類Dev

how to iterate over multiple dataframes and add values to new dataframe in python

Related 関連記事

  1. 1

    Kotlin - how to iterate over a map or a list inside a builder block

  2. 2

    How to iterate over and filter an array?

  3. 3

    how to iterate over tuple items

  4. 4

    How to iterate over the lines in a string?

  5. 5

    How to iterate over dates in a dataframe?

  6. 6

    How to read the first element of all Rows in a text file and then iterate over it in Powershell scripting?

  7. 7

    How can we iterate over an HashMap in JSTL?

  8. 8

    How to define and iterate over map in Jenkinsfile

  9. 9

    How to iterate over keys of a generic object in TypeScript?

  10. 10

    In Elm, how can I iterate over a map?

  11. 11

    How to iterate over a dictionary and operate with its elements?

  12. 12

    How elegantly iterate over list or dictionary

  13. 13

    [Swift[ How to iterate over all the properties of an object

  14. 14

    "How to unevenly iterate over two lists"

  15. 15

    How to iterate over multiple lists simultaneously

  16. 16

    How to iterate over and access individual values in a dictionary

  17. 17

    How to iterate over MethodCall object to get the parameters

  18. 18

    How to iterate over properties of Powershell instance

  19. 19

    How to iterate over multiple function arguments?

  20. 20

    How to iterate over an xml and save it into a dataframe in Python?

  21. 21

    How to iterate over a list of floats in python

  22. 22

    How to send an array of maps and iterate over it using gin-templating

  23. 23

    How do I iterate over multiple, discontinuous ranges in a bash for loop

  24. 24

    How to iterate over a collection of structs as an iterator of trait object references?

  25. 25

    How to iterate over Sudoku sub-grid using c++?

  26. 26

    How Camel split/iterate over the list and send to queue individually?

  27. 27

    How to query and iterate over array of structures in Athena (Presto)?

  28. 28

    how to iterate over a dictionary from value in an array and store in a new dictionary

  29. 29

    how to iterate over multiple dataframes and add values to new dataframe in python

ホットタグ

アーカイブ