Jump to content

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"

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.