Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I replace one single element in an array in ruby?

Tags:

ruby

how can I replace one single element in an array in ruby? so that an array

days=["monday", "tuesday", "wednesday", "jueves"]  #so "jueves" gets replaced by the string "thursday"

thanks

like image 442
Avres03 Avatar asked Dec 25 '22 01:12

Avres03


1 Answers

You an also do...

days.map!{|day| day == "jueves" ? "thursday" : day}

m-p's answer will replace the first occurrence, this will replace all occurrences.

like image 82
SteveTurczyn Avatar answered Jan 14 '23 03:01

SteveTurczyn