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 6213831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:46:03+00:00 2026-05-24T06:46:03+00:00

Overview I am writing a Ruby program that uses data from mysql queries to

  • 0

Overview

I am writing a Ruby program that uses data from mysql queries to create chart URLs. A new requirement has recently surfaced where we could need to created graphs with grouped bars in the future. So instead of having one set of data, I could have any number of sets. Right now the constructor for my BarChart object only takes a single array of data, and I am looking Ruby-like ways of allowing more than one array of data.

Current Constructor

    #constructor
    #title          The title of the graph
    #data           The data that will go in the bar chart
    #labels         The labels that match the data
    #x_axis_label   The label for the x axis
    #y_axis_label   The label for the y axis
    def initialize(title, data, labels, x_axis_label, y_axis_label)
        @title, @data1, @labels, @x_axis_label, @y_axis_label = 
            title, data, labels, x_axis_label, y_axis_label

        super(@title, @@type, @@size)
        @url = to_url()
    end

My attempt

My initial thought was to use var args.

    #constructor
    #title          The title of the graph
    #data           The data that will go in the bar chart
    #labels         The labels that match the data
    #x_axis_label   The label for the x axis
    #y_axis_label   The label for the y axis
    def initialize(title, *data, labels, x_axis_label, y_axis_label)
        .....
    end

Is this a decent idea? or is there a better way to go about it?

Thanks

  • 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-24T06:46:04+00:00Added an answer on May 24, 2026 at 6:46 am

    Personally, when you have this many arguments, I would use an options hash.

    def initialize(options = {})
      options = { default options here, if applicable }.merge(options)
      ...
    end
    

    So that you can construct your class like this:

    MyClass.new(:title => "Awesome Graph", :data => [[1,2,3], [4,5,6]], ...)
    

    I find this approach makes your method calls much more readable, so you don’t have a constructor call which has a long sequence of number and string arguments for which the meaning may be difficult to determine. This also gives you a natural way to add an arbitrary amount of optional parameters with default values.

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

Sidebar

Related Questions

I'm writing a query that will fetch results from a logging table, resulting in
Overview: I have an array of 20 byte strings that needs to be stored
Overview I am using CompositeWPF to create an app using C#. This really should
I have a overview page containing a list with some links from which multiple
I want to get an overview of files that are updated in TFS (that
Is there any way to see an overview of what kind of queries are
I'd like to get a quick overview of available solutions (libraries, ...) that allow
Overview: I'm trying to design an application that will encrypt files to safely send
Overview of the problem: I've been playing with writing custom http server apps for
I'm writing an ASP.NET web service using C# that has a DoLookup() function. For

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.