4

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

2
  • 1
    You mean like: days[3] = 'thurdsay'? Commented Dec 14, 2014 at 0:42
  • 2
    @pguardiario, I've noticed that's an increasingly popular spelling of the day of the week following 'wednesday'. Commented Dec 14, 2014 at 1:06

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4
days[3] = "thursday"

Unless you want something more dynamic.

Comments

3
days[days.index("jueves")] = "thursday" 

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.