0

How to create an Array of objects like the below C++ example?

What's the Ruby way of doing Print print[1000];?

The below code is C++. I'm just wondering if Ruby has an option to instantiate 1000 unique objects of a class:

class Print
{

public:
    Print()
    {
        static int count = 0;
        cout<<"h"<<++count<<endl;
    }
};

int main()
{    
    Print print[1000];
    getchar();
    return 0;
}
11
  • 1
    Is that not C++? Interesting if its not Commented Nov 25, 2013 at 4:23
  • @Hanky웃Panky I came through the program as a solution for printing 1000 chars without loop or recursion. Commented Nov 25, 2013 at 4:27
  • I suppose count is a global variable and the output looks like: "h0 h1 h2 h3 "... ? Commented Nov 25, 2013 at 4:28
  • @ThomasW yes thats how its supposed to be Commented Nov 25, 2013 at 4:29
  • @Shiva that is C++, not Ruby. You are looking at the wrong code if its Ruby you want to work with Commented Nov 25, 2013 at 4:29

5 Answers 5

3

To get 1000 copies of a string instantiated, we can do this:

collection_of_strings = Array.new(1000, String.new('h'))
print collection_of_strings

This is the same object stored in the array. And then that array printed.

This form of Array.new with the block given will create an individually instantiated object, however many times you gave for the argument:

>> collection = Array.new(10) {String.new}   
=> ["", "", "", "", "", "", "", "", "", ""] 

And the proof by checking object_id of each object in the array.

>> collection.each {|e| puts e.object_id}    
85621980                                     
85621970                                     
85621960                                     
85621950                                     
85621940                                     
85621930                                     
85621920                                     
85621910                                     
85621900                                     
85621890                                     
=> ["", "", "", "", "", "", "", "", "", ""]  
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works but seems the initialize is called only once
Hmm... You are correct. This form works with individually unique objects... (editted)
Thanks Array.new(10) {String.new} is what I am looking for
3

Given that

"I suppose count is a global variable and the output looks like: "h0 h1 h2 h3 "

The following should work (see Array#new block for details [1]). It doesn't require an external variable, the block is passed the array's index:

Array.new(1000) {|count| "h#{count}"}

to put them all in one string, try:

Array.new(1000) {|count| "h#{count}"}.join(' ')

[1] http://ruby-doc.org/core-2.0.0/Array.html#method-c-new

Comments

0

Alternate to ThomasW's answer:

output = []
1000.times do |i| { output << "h#{i}" }

Comments

0

Yes there is! In translating your C++ to Ruby, this is what I come up with:

#create dummy class
class Kraken
  # equivalent to your C++ static variable which I think
  # should be declared outside the constructor
  @@count = 0

  # increment the count class variable on every 
  def initialize
    @@count = @@count + 1
  end
end

# Release the Kraken!
kraken = Array.new(1000) { Kraken.new }

2 Comments

Thanks @Yasky But the sole purpose of the C++ program is to print without loop or recusrion.So in you soln Array.new(1000){Kraken.new} should do fine
@shiva: Array.new(1000){Kraken.new} contains an implicit loop (specifically, in MRI, for (i=0; i<len; i++) { rb_ary_store(ary, i, rb_yield(LONG2NUM(i))); ARY_SET_LEN(ary, i + 1); }). There is no way to do this in Ruby (or any other language) without a loop of some sort, just like the C++ code will invoke the constructor 1000 times when compiled to machine code.
0

To get that output with Ruby you can just do: puts (0..999).to_a.

Or, if you want the 'h's you can do: (0..999).each {|n| puts "h#{n}"}

7 Comments

But the solution should not have loop or recusrion
@shiva: Why no loops or recursion? Are you asking people to do your homework for you?
No i think you are misunderstanding my question, I just wondering how to port the exact solution in ruby
@muistooshort actually he can ask people to do this homework for him if the question is clear and precise :P
@Nobita: But it should be stated that it is homework and they should have a basic understanding of what they're doing (doubly so if it is homework).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.