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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:39:14+00:00 2026-06-09T02:39:14+00:00

I am trying to learn ruby and have a doubt regarding passing arrays of

  • 0

I am trying to learn ruby and have a doubt regarding passing arrays of objects as function parameters and printing it in the function.

I have an array that contains an array of objects as follows

describe Name
par1 = "John"
par2 = "Miley"
par3 = "Maria"
@obj_arr = [Name.new(par1),Name.new(par2),Name.new(par3)]
Name.func1(@obj_arr)

I want to print the name “John”, “Miley” and “Maria” in the function and I wrote the function func1 is as follows :

def self.func1(parameter)
parameter.each do |p|
puts p
end
end

This did not print the names. Am I going wrong in accessing the obj_arr in the function?

  • 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-09T02:39:16+00:00Added an answer on June 9, 2026 at 2:39 am

    I think your problem might be the to_s method of the object. You should override it to print what you want. BTW, the syntax in your question is a bit off. I think the definition of the function should be def self.func1 and that your missing an end.
    This is the code I tested:

    irb(main):001:0> class Name
    irb(main):002:1> def self.func1(parameter)
    irb(main):003:2> parameter.each do |p|
    irb(main):004:3* puts p
    irb(main):005:3> end
    irb(main):006:2> end
    irb(main):007:1> end
    => nil
    irb(main):008:0> class Name
    irb(main):009:1> def initialize(name)
    irb(main):010:2> @name = name
    irb(main):011:2> end
    irb(main):012:1> end
    => nil
    irb(main):013:0> Name.func1([Name.new('a'), Name.new('b')])
    #<Name:0x2163dc8>
    #<Name:0x2163d98>
    => [#<Name:0x2163dc8 @name="a">, #<Name:0x2163d98 @name="b">]
    irb(main):014:0> class Name
    irb(main):015:1> def to_s
    irb(main):016:2> @name
    irb(main):017:2> end
    irb(main):018:1> end
    => nil
    irb(main):019:0> Name.func1([Name.new('a'), Name.new('b')])
    a
    b
    => [a, b]
    irb(main):020:0>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm trying to learn ruby blocks. But I have a trouble to get
I am trying to learn Ruby, and arrays are giving me some trouble. I
I've recently installed Ruby on Rails 3.2 and have been trying to learn it.
I have recently begun the process of trying to learn Ruby/Rails and have been
I am trying to learn Ruby on Rails, I have followed the instructions from
I'm trying to learn Ruby as well as Ruby on Rails right now. I'm
I am trying to learn Ruby on Rails using this Rails Tutorial. Earlier, I
I am trying to learn Ruby on Rails by playing around with stuff and
I am trying to learn Ruby on Rails and trying to write some of
I am trying to learn ruby and learn how to handle a while request

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.