Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:20:43+00:00 2026-05-23T18:20:43+00:00

I’m interested in creating an object from a hash that’s generated by a Yaml

  • 0

I’m interested in creating an object from a hash that’s generated by a Yaml file.

The Yaml file might look something like this:

foos:
  first:
    code: f
    name: The First Foo
    icon: path/to/file.png
  second:
    code: s
    name: The Second Foo
    icon: path/to/file.png
bars:
  dive:
    code: d
    name: Dive Bar
    location: 100 Main St.
  college:
    code: c
    name: College Bar
    location: 100 University Ave.

So, the yaml file basically defines a set of attributes which belong to categories.
The categories have a name (foos, bars), and each attribute has at least a code and a name.

So, what I’d like to be able to do is create an “Attributes” model that turns the category names into methods which can call the items within that category as simple objects.

I’d like to be able to do something like:

Attributes = Attributes.new(...yaml_file...)
Attributes.foos #returns an array of foos
Attributes.foo(:f) #returns the foo with a code (f)
Attributes.foo(:s).name #returns "The Second Foo"

I’m kind of lost as to how to approach this.

I know how to setup my Attribute model to load the hash into an instance variable, but what I don’t know how to do is to use the keys from that hash to create methods named for the keys, and to pass on the individual items from each category to create a hash of objects so I can chain these in dot syntax.

I’m aware that with the hash I could already do

attributes[:foos][:first][:icon] 

But I’d like to use this model-from-hash as a starting point where I can add other useful methods to the attributes model later. Plus I’m fairly new to ruby and I’d really like to learn how to do something like this just for the sake of doing it.

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:20:43+00:00Added an answer on May 23, 2026 at 6:20 pm

    Assuming that your model is processed and stored in a hash of hashes, then you can use method_missing to implement your scheme. An extremely rough sketch of this is:

    class Attributes {
      def init(*args) {
        @hash = # ....
      }
      def method_missing(symbol, *args)
        result = @hash[symbol]
        if (result && args.length) {
          return result[args[0]]
        }
        return result
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.