I would like to know if there is a better way (that is, less coding) to have the same output
as the following code:
# arg = :a, :b, :c
output = args.to_a.flatten.compact.map(&:to_sym)
# => [:a, :b, :c]
I use the above code to parse arguments passed by a method in this way
`method_name(开发者_Go百科:a, :b, :c)`.
I don't see any need for to_a
, flatten
or compact
.
You can just do .map(&:to_sym)
if you want to convert strings to symbols. If you're only accepting symbols, just args
contains what you want.
def method_name(*args)
p args.map(&:to_sym)
end
method_name(:a, "b", :c)
Output
[:a, :b, :c]
精彩评论