Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a yaml array with ruby

Tags:

arrays

ruby

I applied YAML.load_file to my example file:

---
languages:
  - name: "English"
    iso_639: "en"
    native_name: "English"
    region:
      - ''
      - UK
      - US
  - name: "Klingon"
    iso_639: "tlh"
    native_name: "tlhIngan Hol"
    region:
      - notearth

I want to iterate though these languages and the region arrays. This doesn't work:

records.each do |record|
  record.region.each do |region|
    self.create!
  end
end

record.region gives me an unknown method error for region. How can I iterate though the languages and and their regions? Or, how can I access the region array?

like image 889
Denny Mueller Avatar asked May 01 '26 07:05

Denny Mueller


2 Answers

There are two errors in your code:

  1. The object you get after loading the YAML file is not an array, it's a hash, say the file is called foo.yml:

    YAML.load_file('foo.yml')
    # => {"languages"=>[{"name"=>"English", "iso_639"=>"en", ...
    

    Thus you have to modify your code like the following to make it work:

    records['languages'].each do |record|
      # ...
    
  2. region is not a method of the hash record, it is a key, you have to access the related value using record['region'].

The correct code you have to use is:

records['languages'].each do |record|
  record['region'].each do |region|
    # My guess is you are going to use `region` inside this block
    self.create!
  end
end
like image 158
toro2k Avatar answered May 02 '26 20:05

toro2k


Yaml is loaded into a hash, hence it will be in form:

languages: [
{
  name: "English"
  iso_639: "en"
  native_name: "English"
  region: ['', 'UK', 'US']
}
{
  name: "Klingon"
  iso_639: "tlh"
  native_name: "tlhIngan Hol"
  region: ['notearth']
}]

So you need to iterate like:

results = YAML.load_file(file)
results['languages'].flat_map{|l| l['region']}.each do |region|
  self.create!
end
like image 30
BroiSatse Avatar answered May 02 '26 20:05

BroiSatse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!