given hash of family members, keys title , array of names values, use ruby's built-in select
method gather immediate family members' names new array.
# given family = { uncles: ["bob", "joe", "steve"], sisters: ["jane", "jill", "beth"], brothers: ["frank","rob","david"], aunts: ["mary","sally","susan"] }
the solution is:
immediate_family = family.select |k, v| k == :sisters || k == :brothers end arr = immediate_family.values.flatten p arr
why ||
operator used instead of &&
operator in select method? when run &&
, select method returns empty array.
the ||
, &&
operators mean or
, and
respectively in programming languages.
the expression:
family.select |k, v| k == :sisters || k == :brothers end
would translate "select elements of familiy
hash key :sisters
or :brothers
". @ursus points out, in case doesn't make sense k
equal :brother
, :sister
@ same time.
Comments
Post a Comment