# If Statement not working as intended?

**URL:** <https://www.ruby-forum.com/t/if-statement-not-working-as-intended/135729>\
**Category:** Rails\
**Created:** [April 24, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/if-statement-not-working-as-intended/135729 "2008-04-24T15:01:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 24, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/if-statement-not-working-as-intended/135729/1 "2008-04-24T15:01:21Z")

</div>

i am having a problem with the if statement here.  
i know which team won the toss and what they elected to do.

i have check the values/equations in the console and it works, but  
when the data is saved it always save  
only bat\_1 and and field\_2

one note: when i swapped toss\_won and toss\_lost with test numbers  
everything runs fine???

```
@game = Game.new(params[:game])
# select which teams batted and fielded first and second

#make a copy of @team which is an array [["Rangers", 27], ["Red

```

Sox", 29]]  
check\_teams = @teams

```
say toss_won = 29 as an example
toss_won = params[:toss_won]

#delete the team that won the toss, this removes the array set of

```

the team that lost the toss  
check\_teams.delete\_if { |a| [\*a].include?(toss\_won) }

```
#this gets the number from the array check_teams which is now like

```

[[“Rangers”, 27]]  
toss\_lost = check\_teams[0][1]

#check to see what the team the won the toss did, team 29 say  
elected to ‘Bat’ then  
# put team numbers to bat first, bat second…  
if params[:elected] == “Bat”  
@game.bat\_1 = toss\_won  
@game.bat\_2 = toss\_lost  
@game.field\_1 = toss\_lost  
@game.field\_2 = toss\_won  
else  
@game.bat\_1 = toss\_lost  
@game.bat\_2 = toss\_won  
@game.field\_1 = toss\_won  
@game.field\_2 = toss\_lost  
end

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 24, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/if-statement-not-working-as-intended/135729/2 "2008-04-24T19:25:35Z")

</div>

One possible source of confusion is the following:  
#make a copy of @team which is an array [[“Rangers”, 27], [“Red  
Sox”, 29]]  
check\_teams = @teams

The comment is _not_ accurate. Essentially what you’ve done is create  
a new pointer to the same object, not create a copy of it. As a  
result, everything that you do to check\_team is also done to @teams.  
I don’t know if that’s effecting the results you’re seeing but it’s  
worth being aware of. If you actually want a completely separate copy  
of the exact same structure, you’d need check\_teams = @teams.dup

Also, a note on your model. What is the purpose of having two sets of  
fields that are exactly the inverse of one another? That is, bat\_1 is  
always field\_2 and vice versa. That could become an expensive  
redundancy.

You might want to do this as well:

check\_teams.delete\_if { |a| [\*a].include?(toss\_won) } .flatten  
=\> [“Rangers”, 27]

Similarly,  
toss\_lost = check\_teams.delete\_if { |a| [\*a].include?  
(toss\_won) } .flatten,last  
=\> 27

@game.bat\_1 = params[:elected]==“Bat” ? toss\_won : toss\_lost  
@game.bat\_2 = params[:elected]==“Bat” ? toss\_lost : toss\_won  
@game.field\_1, @game.field\_2 = @game.bat\_2, @game.bat\_1

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 25, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/if-statement-not-working-as-intended/135729/3 "2008-04-25T16:16:39Z")

</div>

andy thanks for taking the time. i did solve this and i will make my  
notes below because there is still some confusion.  
i am glad that i was able to figure out the problem.

On Apr 24, 10:24 am, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> One possible source of confusion is the following:  
> #make a copy of @team which is an array [[“Rangers”, 27], [“Red  
> Sox”, 29]]  
> check\_teams = @teams
> 
> The comment is _not_ accurate. Essentially what you’ve done is create  
> a new pointer to the same object, not create a copy of it.

yes, i realize that, that was put in while i was testing the code,  
it’s not necessary.

> Also, a note on your model. What is the purpose of having two sets of  
> fields that are exactly the inverse of one another? That is, bat\_1 is  
> always field\_2 and vice versa. That could become an expensive  
> redundancy.

thanks for pointing this out. i agree.

> You might want to do this as well:

the previous logic was working, the problem was with

toss\_won = params[:toss\_won]  
for some reason this didn’t work, but when i changed it to.

toss\_won = @game.toss\_won it worked  
@game was simply @game = Game.new(params[:game])

> check\_teams.delete\_if { |a| [\*a].include?(toss\_won) } .flatten  
> =\> [“Rangers”, 27]

i really like the code below, thanks.
