Passing object initialization arguments inside another method?

I am coming to Ruby from other languages, notably Perl — which, though not dead, is not viable for serious new work anymore. I have come across something in Ruby that I want to know how to do, but my searches on the 'net have not borne fruit.

I understand how to pass arguments when I am initializing a new object; this works perfectly:

dice_set = DiceSet.new({ :die_sides => sides, :num_dice => num })

…and this works perfectly:

@dice_array.push( Die.new )

…but I get errors when I try to do the same thing when #new is called as the argument for the Array#push method:

@dice_array.push( Die.new({ :die_sides => @die_sides }))

I must have the :die_sides argument for initialization of an object of the Die class, since I offer the user dice with any number from 2 through 20 sides. How can I pass the argument inside the push() method, in order to end up with an array of Die objects with an arbitrary number of sides?

Thank you in advance,

-Brian

What is the error you get?

There should is nothing special on how to pass the Die object as the argument. The only difference I see between the example you show as working and the other one not working is in the way you initialize the Die objects:

Die.new works but Die.new(some_hash) doesn’t . So maybe your class Die can’t be initialized with a hash?

Another possible error is that you don’t have access to the @die_sides attribute in that scope: where are you invoking that push method?

It could help if you share with use the definitions of the Die and DiceSet classess also.

He didn’t give us the source code of Die or the error messages, so he’s obviously another troll trolling with a realistic seeming post. such posts are very easy to tell to the trained eye. They’re always lacking in a very obviously essential piece of information, no matter how filled with realistic information they are. I’d like to be proven wrong about this.

Whoa, hostile much? I originally provided everything I thought would be necessary to receive valid answers to my polite, carefully-worded question. When @jgomo3 requested further information in order to help, I began preparing to send my code; however, I have a life outside of Ruby that unexpectedly took up the rest of my day. It is now the middle of the night for me, when I normally would not post anything; I happened to be awake in the night just now, and I saw an email notification for your ridiculous post.

I will respond fully during my day today. Your post has me questioning whether this is a legitimate forum for discussion of Ruby, or if it is merely a haven for internet trolls. You accused me of trolling when that was exactly what you, yourself, were doing.

You could benefit from an anger management course.

-Brian

Problem solved. TIL that I need to learn more about variable scoping in Ruby; I was reusing variable names and hash key symbols because I thought they were out of scope when I used them in separate classes, but after I renamed every variable and hash key symbol to be unique in its containing class, the script works as desired:

╰─> ruby init.rb
--------------------------
Welcome to the Dice Roller
--------------------------

How many sides should the dice have? (2-20) 14
How many dice do you wish to throw? (2-10) 6
[ 9 ] - [ 2 ] - [ 10 ] - [ 10 ] - [ 13 ] - [ 5 ]

Type 'r' to roll again, 'q' to quit
> r

[ 9 ] - [ 1 ] - [ 7 ] - [ 8 ] - [ 14 ] - [ 3 ]

Type 'r' to roll again, 'q' to quit
> q

Thank you for your patient questioning, @jgomo3 … it got me thinking in the direction that eventually worked. You were a great sounding board. :slight_smile:

-Brian

You still did not provide the Die source code or the original error messages to explain how your problem exactly occurred, thus confirming my point about you trolling with a realistic seeming post. Nice try!

Seriously Dude, anger management is a good thing. Take a deep breath. Hold it. Now exhale slowly.

Another trolling trick is to switch the focus of the conversation to something else by going on the offensive and attacking the person who is rightfully calling out their trolling so that they could continue their trolling unchecked. Nice try again!

It’s obvious at this point that I’m living in your head, rent-free. I didn’t even have try hard to move in, I only had to make two good-faith posts — and reply to your nonsense. Have you always been so simpleminded?