Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.collect(&:id)的语法问题 #89

Open
liangchaob opened this issue Oct 25, 2016 · 0 comments
Open

.collect(&:id)的语法问题 #89

liangchaob opened this issue Oct 25, 2016 · 0 comments

Comments

@liangchaob
Copy link

liangchaob commented Oct 25, 2016

在研究 Rails 102 的时候,尝试.collect(&:id)的语法结构貌似不认这个语法结构:

2.3.1 :047 > topics = [{id:1, title:"topic1"}, {id:2, title:"topic2"}]
 => [{:id=>1, :title=>"topic1"}, {:id=>2, :title=>"topic2"}]
2.3.1 :048 > topics.collect(&:id)
NoMethodError: undefined method `id' for {:id=>1, :title=>"topic1"}:Hash
    from (irb):48:in `collect'
    from (irb):48
    from /Users/alan/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'

试过 stackoverflow 中的不含hash的花括号结构是有效的:

2.3.1 :038 >   a = [ "a", "b", "c", "d" ]
 => ["a", "b", "c", "d"]
2.3.1 :039 > a.collect {|x| x + "!" }
 => ["a!", "b!", "c!", "d!"]

是不是ruby语法结构变化又不兼容了呢,array里面套hash后collect方式是怎样的呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant