ruby - Improve parsing '*arg' values -
i know if there better way (that is, less coding) have same output following code:
# arg = :a, :b, :c output = args.to_a.flatten.compact.map(&:to_sym) # => [:a, :b, :c] i use above code parse arguments passed method in way
`method_name(:a, :b, :c)`.
i don't see need to_a, flatten or compact.
you can .map(&:to_sym) if want convert strings symbols. if you're accepting symbols, args contains want.
def method_name(*args) p args.map(&:to_sym) end method_name(:a, "b", :c) output
[:a, :b, :c]
Comments
Post a Comment