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

  • SEARCH
  • Home
  • 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 7909699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:33:20+00:00 2026-06-03T12:33:20+00:00

I’m working on a Ruby binding for a REST API and am struggling with

  • 0

I’m working on a Ruby binding for a REST API and am struggling with what would be considered idiomatic Ruby for a piece of it.

To illustrate with a simplified example, the resources represented by the API form a tree structure:

           root
            /\
           /  \
          /    \
      group   group    
        /       /\
       /       /  \
      /       /    \
project  project  project

To create a new instance of a child, knowledge of its parent is required. For example, to create a new group it must have knowledge of the root so that it can retrieve the URI for creating a group from the root.

I’m reasonably happy with read access:

api = Api.new(...)
api.groups # An array of all groups
api.groups.select(...).projects # An array of all a group's projects

It’s creating new resources that I’m unsure about.

I’ve considered having a create method on an array subclass:

group = api.groups.create("group-name")
project = group.projects.create("project-name")

Instantiating the type directly, taking the parent as a constructor argument:

group = Group.new(api, "project-name")
project = Project.new(group, "project-name")

Create methods on the parent:

group = api.create_group("group-name")
project = group.create_project("project-name")

Of the three approaches above, I prefer the first as it’s concise and allows creation to be chained. The third option is similar, but it doesn’t feel right – in terms of the REST API, the create is happening at the wrong level as it’s the groups resource the allows creation of a group. That said, I’m not an experienced Ruby developer and, first and foremost, I want the Ruby API to be idiomatic for Ruby developers even if that means it doesn’t exactly match the model of the REST API.

  • 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-06-03T12:33:22+00:00Added an answer on June 3, 2026 at 12:33 pm

    It’s not clear to me how the code samples and description provided ties in to REST, but I’ll offer my two cents on what I do understand:

    I think it would be perfectly valid to use a combination of all three. As for what would be idiomatic, I’d take your cues from the Rails association methods.

    api.groups.create follows Rails’ has_many syntax. api.create_group and group.create_project follow the has_one syntax. And I’m assuming that, beneath the syntactic sugar of create/create_association, you are using composition to tie together the references to all the classes anyway, so Group.new(api, "group-name")/Project.new(group, "project-name") seem like perfectly valid options as well. There’s no rule saying you can’t use all three approaches to good effect (and Rails does).

    I’d suggest avoiding subclassing Ruby’s base classes (you mentioned subclassing Array) unless you have a really good reason to do so. Just use composition to delegate to them when necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
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.