I have a hash like this:
@password_constraints = {
length: 'Must be at least 6 character long.',
contain: [
'one number',
'one lowercase letter',
'one uppercase letter.'
]
}
And I want to write a method that returns an human-readable string built from this hash like this one:
Must be at least 6 character long. Must contain: one number, one lowercase letter and an uppercase letter.
Currently, I have a very verbose method with an each
that iterates over the @password_constraints[:contain]
array and has several conditions to check if I have to put a ,
, or an and
or nothing.
But I want almost the same behavior as join(', ')
but the last delimiter must be and
.
I'm looking for some sort of solution like this one with the special join
:
def password_constraints
@password_constraints[:length] << ' Must contain.' << @password_constraints[:contain].join(', ')
end
Is there a way to make this more compact or Ruby-like?