Simple question

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very very simple to me but no matter what I try it I can’t get my desired out put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“ NO
move to the next to last element, is this element == to “-“ YES replace with “X"

I know how to make them ALL change at once but that isn’t my goal, it needs to be one at time. Example would be receiving user input so I would want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

The question is a bit vague. Are you just trying to iterate through the array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from behind once, but does that do what you want or are you looking for different output?

···


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very very simple to me but no matter what I try it I can’t get my desired out put. (therefore I guess it isn’t easy….)
I want to iterate through my_array to see if the last element is == “-“ if it is replace that element with “X”
Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“ NO
move to the next to last element, is this element == to “-“ YES replace with “X"
I know how to make them ALL change at once but that isn’t my goal, it needs to be one at time. Example would be receiving user input so I would want to change the elements one at a time. Kind of like a match data.
starting array
my_array = [“3”, “-“, “-“, “-“, “-“]
Hope this makes sense and thanks in advance for any help!
Troy

sorry to be vague...
I tried what you suggested. It didn’t work for me.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

second array

my_array = [“3”, “-“, “-“, “-“, “X“]

third array
my_array = [“3”, “-“, “-“, “X“, “X“]

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

what i’m trying to do is basically make a game. I want the user to pick ‘row’ 3 to put a game piece in so I need to see if the game piece already exist or not. if it doesn’t insert a game piece if it does check the next empty spot to see if the game piece exist there if it doesn’t insert a game piece if it does move onto the next and so on… make sense..

I really want to try and write the code myself but I have been struggling with this one piece for two days… make sense?

···

On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:

The question is a bit vague. Are you just trying to iterate through the array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from behind once, but does that do what you want or are you looking for different output?


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very very simple to me but no matter what I try it I can’t get my desired out put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“ NO
move to the next to last element, is this element == to “-“ YES replace with “X"

I know how to make them ALL change at once but that isn’t my goal, it needs to be one at time. Example would be receiving user input so I would want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

Hi Troy,

Could you add how you'd expect the array to look like and how you would
not? Perhaps throw in some different scenarios if that applies?

Regards,

Jorge Colon
Senior Software Architect/Owner
2UP Media

Zend Certified Engineer
www.bit.ly/jorgecolonzce

···

Sent from my mobile device

On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:

sorry to be vague...
I tried what you suggested. It didn’t work for me.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

second array

my_array = [“3”, “-“, “-“, “-“, “X“]

third array
my_array = [“3”, “-“, “-“, “X“, “X“]

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

what i’m trying to do is basically make a game. I want the user to pick
‘row’ 3 to put a game piece in so I need to see if the game piece already
exist or not. if it doesn’t insert a game piece if it does check the next
empty spot to see if the game piece exist there if it doesn’t insert a game
piece if it does move onto the next and so on… make sense..

I really want to try and write the code myself but I have been struggling
with this one piece for two days… make sense?

On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:

The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for
different output?


Sent from Mailbox <https://www.dropbox.com/mailbox&gt;

On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very
very simple to me but no matter what I try it I can’t get my desired out
put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if
it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“
NO
move to the next to last element, is this element == to “-“ YES replace
with “X"

I know how to make them ALL change at once but that isn’t my goal, it
needs to be one at time. Example would be receiving user input so I would
want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

Troy,

Do you just want a loop like this?

while num >= 0

if my_array[num] == '-'

my\_array\[num\] = &#39;X&#39;

break

end

num -= 1

end

And you would just call this every time a player makes a move?

···


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:29 PM, Jorge Colon <2upmedia@gmail.com> wrote:

Hi Troy,
Could you add how you'd expect the array to look like and how you would
not? Perhaps throw in some different scenarios if that applies?
Regards,
Jorge Colon
Senior Software Architect/Owner
2UP Media
Zend Certified Engineer
www.bit.ly/jorgecolonzce
Sent from my mobile device
On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:
sorry to be vague...
I tried what you suggested. It didn’t work for me.
starting array
my_array = [“3”, “-“, “-“, “-“, “-“]
second array
my_array = [“3”, “-“, “-“, “-“, “X“]
third array
my_array = [“3”, “-“, “-“, “X“, “X“]
fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]
what i’m trying to do is basically make a game. I want the user to pick
‘row’ 3 to put a game piece in so I need to see if the game piece already
exist or not. if it doesn’t insert a game piece if it does check the next
empty spot to see if the game piece exist there if it doesn’t insert a game
piece if it does move onto the next and so on… make sense..
I really want to try and write the code myself but I have been struggling
with this one piece for two days… make sense?
On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:
The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?
I was originally going to just say something like
my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse
It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for
different output?

Sent from Mailbox <https://www.dropbox.com/mailbox&gt;
On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very
very simple to me but no matter what I try it I can’t get my desired out
put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if
it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“
NO
move to the next to last element, is this element == to “-“ YES replace
with “X"

I know how to make them ALL change at once but that isn’t my goal, it
needs to be one at time. Example would be receiving user input so I would
want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

sorry add

num = my_array.length - 1

to the beginning of that script

···


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:29 PM, Jorge Colon <2upmedia@gmail.com> wrote:

Hi Troy,
Could you add how you'd expect the array to look like and how you would
not? Perhaps throw in some different scenarios if that applies?
Regards,
Jorge Colon
Senior Software Architect/Owner
2UP Media
Zend Certified Engineer
www.bit.ly/jorgecolonzce
Sent from my mobile device
On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:
sorry to be vague...
I tried what you suggested. It didn’t work for me.
starting array
my_array = [“3”, “-“, “-“, “-“, “-“]
second array
my_array = [“3”, “-“, “-“, “-“, “X“]
third array
my_array = [“3”, “-“, “-“, “X“, “X“]
fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]
what i’m trying to do is basically make a game. I want the user to pick
‘row’ 3 to put a game piece in so I need to see if the game piece already
exist or not. if it doesn’t insert a game piece if it does check the next
empty spot to see if the game piece exist there if it doesn’t insert a game
piece if it does move onto the next and so on… make sense..
I really want to try and write the code myself but I have been struggling
with this one piece for two days… make sense?
On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:
The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?
I was originally going to just say something like
my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse
It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for
different output?

Sent from Mailbox <https://www.dropbox.com/mailbox&gt;
On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very
very simple to me but no matter what I try it I can’t get my desired out
put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if
it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“
NO
move to the next to last element, is this element == to “-“ YES replace
with “X"

I know how to make them ALL change at once but that isn’t my goal, it
needs to be one at time. Example would be receiving user input so I would
want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

Array#rindex is your friend here. The example here

http://ruby-doc.org/core-2.1.5/Array.html#method-i-rindex

should be all you need to work out the answer.

John

Ok..if I under stand here is snippets of the full array (sorry I haven’t loaded this up to git or I could show all my code but it’s a bit of a mess right now)

this is my array

@board = Array.new(6, Array.new(7, "-“))
@header = Array("1".."7”)

board.unshift(header)

=> produces
1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -

=> select a column to drop a game piece
=> user input == 3

after the user input the new board would look like this

1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - x - - - -

is this better?

···

On Dec 19, 2014, at 9:29 PM, Jorge Colon <2upmedia@gmail.com> wrote:

Hi Troy,

Could you add how you'd expect the array to look like and how you would not? Perhaps throw in some different scenarios if that applies?

Regards,

Jorge Colon
Senior Software Architect/Owner
2UP Media

Zend Certified Engineer
www.bit.ly/jorgecolonzce

Sent from my mobile device

On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:

sorry to be vague...
I tried what you suggested. It didn’t work for me.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

second array

my_array = [“3”, “-“, “-“, “-“, “X“]

third array
my_array = [“3”, “-“, “-“, “X“, “X“]

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

what i’m trying to do is basically make a game. I want the user to pick ‘row’ 3 to put a game piece in so I need to see if the game piece already exist or not. if it doesn’t insert a game piece if it does check the next empty spot to see if the game piece exist there if it doesn’t insert a game piece if it does move onto the next and so on… make sense..

I really want to try and write the code myself but I have been struggling with this one piece for two days… make sense?

On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:

The question is a bit vague. Are you just trying to iterate through the array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from behind once, but does that do what you want or are you looking for different output?


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very very simple to me but no matter what I try it I can’t get my desired out put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“ NO
move to the next to last element, is this element == to “-“ YES replace with “X"

I know how to make them ALL change at once but that isn’t my goal, it needs to be one at time. Example would be receiving user input so I would want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

Thank you to everyone that reached out and helped. the While loop in fact did work now I just have to work it into my code.

John, I looked at rindex and didn’t think it would work. I will research this further tomorrow, obviously I am not clear on how it works.

I want to thank everyone!! I will post my game after it is complete in hopes I will receive feed back and ways to improve my code!

thanks
troy

···

On Dec 19, 2014, at 10:13 PM, John W Higgins <wishdev@gmail.com> wrote:

Array#rindex is your friend here. The example here

http://ruby-doc.org/core-2.1.5/Array.html#method-i-rindex

should be all you need to work out the answer.

John

Hi Troy,

The code you showed in the previous e-mail is different.

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

With this initialization you're getting cols and lines swapped
compared to the previous example.

@board = Array.new(6, Array.new(7, "-“))
@header = Array("1".."7”)

Keep in mind that with this initialization above you will transverse
the array like this.

@board[line][column]

For something like this:
1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - x - - - -

@board[0] # for example
# => ["1", "2", "3", "4", "5", "6", "7"]

@board[6]
# => [ "-", "-", "x", "-", "-", "-", "-" ]

@board[6][2] # line 6, col 2
# => "x"

@board[6][1] # line 6, col 1
# => "-"

# Not like the 'my_array' example
my_array = [“3”, “-“, “X“, “X“, “X“]

Where the Array has one column.

So, you're probably willing to loop in reverse from the last _line_
(6) to the second one (1) (because the first line (0) is the header).
While looping, testing the desired column (that column chosen by the
user), for example column 2 (the third one) to see if it has an "x".
If not, you should change it to an "x" and break the loop.
It yes, it has an "x", you should try to loop again, testing the line
before the current line (line-1) and go until line 1 (the second line;
the line after the header).

Is it something like "Conect Four" ? (http://en.wikipedia.org/wiki/Connect_Four\)

It's not hard and I think you're getting closer.
If you get stuck and need more help just drop another e-mail on this thread.

Abinoam Jr.

···

On Sat, Dec 20, 2014 at 12:36 AM, Troy Leach <troyleach29@gmail.com> wrote:

Ok..if I under stand here is snippets of the full array (sorry I haven’t
loaded this up to git or I could show all my code but it’s a bit of a mess
right now)

this is my array

@board = Array.new(6, Array.new(7, "-“))
@header = Array("1".."7”)

board.unshift(header)

=> produces
1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -

=> select a column to drop a game piece
=> user input == 3

after the user input the new board would look like this

1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - x - - - -

is this better?

On Dec 19, 2014, at 9:29 PM, Jorge Colon <2upmedia@gmail.com> wrote:

Hi Troy,

Could you add how you'd expect the array to look like and how you would not?
Perhaps throw in some different scenarios if that applies?

Regards,

Jorge Colon
Senior Software Architect/Owner
2UP Media

Zend Certified Engineer
www.bit.ly/jorgecolonzce

Sent from my mobile device

On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:

sorry to be vague...
I tried what you suggested. It didn’t work for me.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

second array

my_array = [“3”, “-“, “-“, “-“, “X“]

third array
my_array = [“3”, “-“, “-“, “X“, “X“]

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

what i’m trying to do is basically make a game. I want the user to pick
‘row’ 3 to put a game piece in so I need to see if the game piece already
exist or not. if it doesn’t insert a game piece if it does check the next
empty spot to see if the game piece exist there if it doesn’t insert a game
piece if it does move onto the next and so on… make sense..

I really want to try and write the code myself but I have been struggling
with this one piece for two days… make sense?

On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:

The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for different
output?


Sent from Mailbox

On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> wrote:

Hello all,
first time posting, or emailing as it may be.
I’m a newbie to Ruby and I am struggling with something that seems very
very simple to me but no matter what I try it I can’t get my desired out
put. (therefore I guess it isn’t easy….)

I want to iterate through my_array to see if the last element is == “-“ if
it is replace that element with “X”

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to “-“
NO
move to the next to last element, is this element == to “-“ YES replace
with “X"

I know how to make them ALL change at once but that isn’t my goal, it
needs to be one at time. Example would be receiving user input so I would
want to change the elements one at a time. Kind of like a match data.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

Hope this makes sense and thanks in advance for any help!

Troy

or you could use a stack stru w a stack ptr. advancing the stack
pointer for every push. if you go this way, you dont need a loop.

kind regards --botp

···

On Sat, Dec 20, 2014 at 1:02 PM, Troy Leach <troyleach29@gmail.com> wrote:

.. the While loop in fact did work now I just have to work it into my code.

It looks like you're doing a connect four program. I would probably create
a class Column that I can drop things in like a bucket with 7 buckets
(instances) and then have a Row class for the lines.. Your Column class
would have to inherit from Array and you'd be using #unshift and #pop.
There are several ways to do it, because it doesn't really need to be a
reverse escalator, but that's what I'd do.

···

On Sat, Dec 20, 2014 at 9:50 AM, Abinoam Jr. <abinoam@gmail.com> wrote:

Hi Troy,

The code you showed in the previous e-mail is different.

> fourth array
> my_array = [“3”, “-“, “X“, “X“, “X“]

With this initialization you're getting cols and lines swapped
compared to the previous example.

> @board = Array.new(6, Array.new(7, "-“))
> @header = Array("1".."7”)

Keep in mind that with this initialization above you will transverse
the array like this.

@board[line][column]

For something like this:
1 2 3 4 5 6 7
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - x - - - -

@board[0] # for example
# => ["1", "2", "3", "4", "5", "6", "7"]

@board[6]
# => [ "-", "-", "x", "-", "-", "-", "-" ]

@board[6][2] # line 6, col 2
# => "x"

@board[6][1] # line 6, col 1
# => "-"

# Not like the 'my_array' example
my_array = [“3”, “-“, “X“, “X“, “X“]

Where the Array has one column.

So, you're probably willing to loop in reverse from the last _line_
(6) to the second one (1) (because the first line (0) is the header).
While looping, testing the desired column (that column chosen by the
user), for example column 2 (the third one) to see if it has an "x".
If not, you should change it to an "x" and break the loop.
It yes, it has an "x", you should try to loop again, testing the line
before the current line (line-1) and go until line 1 (the second line;
the line after the header).

Is it something like "Conect Four" ? (
http://en.wikipedia.org/wiki/Connect_Four\)

It's not hard and I think you're getting closer.
If you get stuck and need more help just drop another e-mail on this
thread.

Abinoam Jr.

On Sat, Dec 20, 2014 at 12:36 AM, Troy Leach <troyleach29@gmail.com> > wrote:
> Ok..if I under stand here is snippets of the full array (sorry I haven’t
> loaded this up to git or I could show all my code but it’s a bit of a
mess
> right now)
>
> this is my array
>
> @board = Array.new(6, Array.new(7, "-“))
> @header = Array("1".."7”)
>
> board.unshift(header)
>
> => produces
> 1 2 3 4 5 6 7
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - - - - - -
>
> => select a column to drop a game piece
> => user input == 3
>
> after the user input the new board would look like this
>
> 1 2 3 4 5 6 7
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - - - - - -
> - - x - - - -
>
> is this better?
>
> On Dec 19, 2014, at 9:29 PM, Jorge Colon <2upmedia@gmail.com> wrote:
>
> Hi Troy,
>
> Could you add how you'd expect the array to look like and how you would
not?
> Perhaps throw in some different scenarios if that applies?
>
> Regards,
>
> Jorge Colon
> Senior Software Architect/Owner
> 2UP Media
>
> Zend Certified Engineer
> www.bit.ly/jorgecolonzce
>
> Sent from my mobile device
>
> On Dec 19, 2014, at 10:27 PM, Troy Leach <troyleach29@gmail.com> wrote:
>
> sorry to be vague...
> I tried what you suggested. It didn’t work for me.
>
> starting array
> my_array = [“3”, “-“, “-“, “-“, “-“]
>
> second array
>
> my_array = [“3”, “-“, “-“, “-“, “X“]
>
> third array
> my_array = [“3”, “-“, “-“, “X“, “X“]
>
> fourth array
> my_array = [“3”, “-“, “X“, “X“, “X“]
>
> what i’m trying to do is basically make a game. I want the user to pick
> ‘row’ 3 to put a game piece in so I need to see if the game piece
already
> exist or not. if it doesn’t insert a game piece if it does check the next
> empty spot to see if the game piece exist there if it doesn’t insert a
game
> piece if it does move onto the next and so on… make sense..
>
> I really want to try and write the code myself but I have been struggling
> with this one piece for two days… make sense?
>
>
>
> On Dec 19, 2014, at 9:17 PM, Raj Sahae <rajsahae@gmail.com> wrote:
>
> The question is a bit vague. Are you just trying to iterate through the
> array in reverse? Can you give an example input and output?
>
> I was originally going to just say something like
>
> my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse
>
> It’s not the most efficient way, it would be better to just iterate from
> behind once, but does that do what you want or are you looking for
different
> output?
>
> —
> Sent from Mailbox
>
>
> On Fri, Dec 19, 2014 at 7:07 PM, Troy Leach <troyleach29@gmail.com> > wrote:
>>
>> Hello all,
>> first time posting, or emailing as it may be.
>> I’m a newbie to Ruby and I am struggling with something that seems very
>> very simple to me but no matter what I try it I can’t get my desired out
>> put. (therefore I guess it isn’t easy….)
>>
>> I want to iterate through my_array to see if the last element is == “-“
if
>> it is replace that element with “X”
>>
>> Then I want to repeat this operation.
>> I want to iterate through my_array to see if the last element is == to
“-“
>> NO
>> move to the next to last element, is this element == to “-“ YES replace
>> with “X"
>>
>> I know how to make them ALL change at once but that isn’t my goal, it
>> needs to be one at time. Example would be receiving user input so I
would
>> want to change the elements one at a time. Kind of like a match data.
>>
>> starting array
>> my_array = [“3”, “-“, “-“, “-“, “-“]
>>
>> Hope this makes sense and thanks in advance for any help!
>>
>> Troy
>>
>>
>
>
>