If Statement not working as intended?

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

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

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 [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs