Jump to content

[SOLVED] Quick ruby help


aeonsky

Recommended Posts

Hey everyone.

 

just having trouble understanding this...

 

array = ["Hello", "there", "sweetie", 1, 2, 3]
array[1, 2] = "here"
puts array

 

Outputs...

 

Hello
here
1
2
3

 

I have no idea why, where does "sweetie" go?

 

I would assume the output to be this...

 

Hello
here
here
1
2
3

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/131000-solved-quick-ruby-help/
Share on other sites

OK, never mind guys, I got it. For anyone who is new to Ruby too, here is what happens.

 

array[1, 2]

 

Selects two elements and then changes it to the string you specified. Since there is only one string, it produces only one element.

 

So if you want to produce this...

 

Hello
here
here
1
2
3

 

You need to do this...

 

array[1, 2] = "here", "here"

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.