开发者

Improve parsing '*arg' values [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜