Ruby:将对象添加到数组末尾
我有两个对象@tracks(可枚举)和@artist,我想创建一个包含所有曲目和其中艺术家的可枚举对象。这样我就可以将它们传递给一个可以执行的方法(每个曲目和艺术家都有更改事件):
change_events = object.map(&:change_events).flatten
我的想法是:
objects = @artist.tracks
objects << @artist
但这给了我第二行的错误(这是有道理的,但我不知道如何解决):
Track(#17816) expected, got Artist(#17572)
任何关于我如何做到这一点的想法将不胜感激!
I have two objects @tracks (an enumerable) and @artist, and I'd like to create an enumerable with all the tracks and the artist in them. This is so that I can pass them to a method which will do (each track, and the artist have change events):
change_events = object.map(&:change_events).flatten
My idea was:
objects = @artist.tracks
objects << @artist
but that gives me this error for the second line (which makes sense, but I don't know how to fix):
Track(#17816) expected, got Artist(#17572)
Any ideas on how I could do this would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个(错误)表明它(
@artist.tracks
的返回值)不是您正在处理的数组,而是一些特定于rails的数据类型。您可以尝试但是在数组中使用非齐次值通常不好,如果它们都响应您需要的方法,那么您应该会很好。
This (the error) signals that it (the return value of
@artist.tracks
) is not an array you are dealing with, but some rails-specific data type. You could tryBut using inhomogeneous values in an array is often not good, if they all respond to the method you need you should be good though.