Reputation: 808
Regarding the exercises in Michael Hartl's RoR Tutorial in lesson 4.3.3 (Hashes & Symbols):
"Define a hash with symbol keys corresponding to name, email, and a “password digest”, and values equal to your name, your email address, and a random string of 16 lower-case letters."
I am hoping to get some input and/or alternative & 'better' solutions to this (or at least some criticism regarding my solution).
def my_hash
a = ('a'..'z').to_a.shuffle[0..15].join
b = { name: "John", email: "[email protected]", password: a }
return b
end
puts my_hash
(Yes I realize this is a very simple exercise and apologize if it has been asked before.)
Upvotes: 1
Views: 544
Reputation: 121000
There are many 2 improvements could be made:
Array#sample
to get random letters (it has an advantage: the letter might in fact repeat in the password, while shuffle[0..15]
will return 16 distinct letters);return
.Here you go:
def my_hash
{
name: "John",
email: "[email protected]",
password: ('a'..'z').to_a.sample(16).join
}
end
puts my_hash
Bonus:
I accidentaly found the third glitch in the original code. It should probably be:
def my_hash
{
name: "Brandon",
email: "[email protected]",
password: ('a'..'z').to_a.sample(16).join
}
end
:)
Upvotes: 3