Vijikumar M
Vijikumar M

Reputation: 3764

Extracing words from a string?

I have a string "---\n- bb\n- j2me\n". I want to extract the words and save it into an array. Like ['bb','j2me']. I tried the below but its not working.

 "---\n- bb\n- j2me\n".split("\n")

If you have any idea please share.

Upvotes: 0

Views: 55

Answers (1)

Stefan
Stefan

Reputation: 114138

That looks like YAML:

puts "---\n- bb\n- j2me\n"
# ---
# - bb
# - j2me

You can parse it with:

require 'yaml'

YAML.load("---\n- bb\n- j2me\n")
#=> ["bb", "j2me"]

Upvotes: 4

Related Questions