0

In Python you can do something like:

d = {"Austria": "Vienna", "Peru": "Lima"} d.pop("Austria") 

"Vienna" is returned, and the "Austria":"Vienna" pair is deleted from d.

Is there something analogous in Ruby? I think I know the answer but I haven't seen this asked yet on SO and want to confirm I'm correct.

8
  • 3
    I'm curious if you think you know the answer, why don't you try it? Commented Jul 24, 2014 at 17:38
  • I did. But I always like to get confirmation and make sure I'm not missing some details or side-effects. Commented Jul 24, 2014 at 17:41
  • Doc never lies.. You can read it as many times as you want. It is perfectly written in the doco. Commented Jul 24, 2014 at 17:42
  • 1
    It's important to be able to figure stuff out without asking. That's why we have documentation, and tools like IRB, which lets us try and experiment and SEE whether things work. Asking questions that show you haven't read, or tried, will only irritate those who you could end up working with; Trying is an important characteristic for a programmer. Commented Jul 24, 2014 at 18:27
  • @theTinMan, you are showing your age by your use of the term "programmer". Commented Jul 24, 2014 at 18:30

2 Answers 2

2

Hash#delete is similar to dict.pop in Python.

h = {"Austria" => "Vienna", "Peru" => "Lima"} h.delete("Austria") # => "Vienna" h # => {"Peru"=>"Lima"} 
Sign up to request clarification or add additional context in comments.

1 Comment

It was close between you and Nick (I saw those edits flying), but you edged him out because the final result after 15 minutes was more complete and clearer. Thanks to both!
2

Yup, delete.

[1] pry(main)> d = { "Austria" => "Vienna", "Peru" => "Lima" } => {"Austria"=>"Vienna", "Peru"=>"Lima"} [2] pry(main)> d.delete('Austria') => "Vienna" [3] pry(main)> d => {"Peru"=>"Lima"} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.