1

I want to make a method named default_classroom. so that it takes an array of keys and a default value and returns a hash with all keys set to the default value. (Like, all students go to the same classroom. Here, students are the keys and class as a default value)

def default_classroom(students, default_class)
end
puts default_classroom([:jony, :marry], 8)

# should return => {jony: 8, marry: 8}

i did this:

def default_classroom(students, default_class)
    Hash[ students, default_class]
end

and also this:

def default_classroom(students, default_class)
    Hash[*students.flatten(default_class)]
end

but still not working. Will you please advice me, How can i complete that?

4
  • Rabby, I appreciate the greenie, but in future consider waiting longer before selecting an answer. A rush to judgement may discourage other, possibly better answers, and is a bummer for those still preparing answers when the checkmark appears. Many SO members wait for several hours or even days. Commented Aug 17, 2014 at 17:56
  • Cary Swoveland, Thanks for your advice. But i was so excited. Commented Aug 17, 2014 at 18:10
  • You shouldn't have the Rails tag here, as this is a pure-Ruby question. Having a superfluous tag may cause some to waste time, others (who filter out Rails questions) to not see the question. Commented Aug 17, 2014 at 19:20
  • Cary Swoveland, i edited the tags section. Commented Aug 17, 2014 at 19:31

1 Answer 1

3

There are many way to do this. Here are three.

#1

def default_classroom(students, default_class)
  Hash[ students.product([default_class]) ]
end

students = %w[Billy-Bob, Trixie, Huck]
  #=> ["Billy-Bob", "Trixie", "Huck"]
default_class = "Roofing 101"

default_classroom(students, default_class)
  #=> {"Billy-Bob"=>"Roofing 101", "Trixie"=>"Roofing 101",
  #    "Huck"=>"Roofing 101"}

For Ruby versions >= 2.0, you could instead write:

students.product([default_class]).to_h

#2

def default_classroom(students, default_class)
  students.each_with_object({}) { |s,h| h[s] = default_class }
end

#3

Depending on your application, you may need only specify the hash's default value (and not add a key-value pair for each student). After reviewing the documentation for Hash#new, you might think this would be done as follows:

h = Hash.new(default_class)
  #=> {}

Then:

h["Billy-Bob"] == "Real Analysis"

evaulates to:

"Roofing 101" == "Real Analysis"
  #=> false

but the hash remains empty:

h #=> {}

Instead, initialize the hash with a block, like this:

h = Hash.new { |h,k| h[k]=default_class }
  #=> {}

Then:

h["Billy-Bob"] == "Real Analysis"

again evaulates to:

"Roofing 101" == "Real Analysis"
  #=> false

but now:

h #=> {"Billy-Bob"=>"Roofing 101"}
Sign up to request clarification or add additional context in comments.

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.