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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:05:38+00:00 2026-06-02T04:05:38+00:00

If you have a subclass X of Array, then doing X#to_a returns an array

  • 0

If you have a subclass X of Array, then doing X#to_a returns an array object, while doing X#to_ary returns an x object.

While I understand that to_a means “I can be changed into an array”, while to_ary means “I behave like an array”, I don’t understand why the former implements a change of class while the latter doesn’t.

Also, isn’t returning a subclass of Array sufficient for to_a, under the Liskov Substitution Principle?

  • 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-02T04:05:41+00:00Added an answer on June 2, 2026 at 4:05 am

    Is “because that’s the way it’s defined to be” sufficient?

    to_a

    Returns self. If called on a subclass of Array, converts the receiver to an Array object.

    to_ary

    Returns self.

    Probably not, so here we go into the rabbit hole.

    Beyond the fact that the documentation definitively states that this is the way it is, the reasoning is perhaps only truly answerable by Matz, et al.

    Digging around though it would seem that to_ary is used when implicit type conversions occur. Its use for implicit conversions seems to be echoed in this feature request as well. In other words, if an object responds to to_ary, then it should be treated as an Array, and it is used in this way internally. Thus to_a would be for when you (explicitly) want an Array and not some subclass.

    Yes, returning a subclass would still satisfy LSP (assuming the subclass does not decide to radically change the behavior of Array such that it wouldn’t be), but the principle only states that a subclass may be substituted for its base class, not that it needs to be. I’m not really sure that matter here anyway, though, since you’re calling to_a your explicitly asking for a different object (to go along with the reasoning about implicit conversions above) and thus you’re saying you don’t want a substitute object type.

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

Sidebar

Related Questions

I have a subclass of UITableViewController. I have code that can add/remove a UISearchBar
I have a custom NSOperation object that is instantiated from a UITableViewController subclass (MusicTableVC).
I have a managed object subclass named Tag that has a to-many relationship to
I've created a subclass of Array and written some messages for it that have
I have a subclass of UIImageView that I need to know when is touched.
I have a subclass of JLabel that forms a component of my GUI. I
I have a subclass that calls a method from a superclass. The method in
I have a subclass of UIScrollView that I'm using for images slideshow, with infinite
I have multiple arrays of instances of ActiveRecord subclass Item that I need to
I have several objects in an array. These objects are from a UIImageView subclass.

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.