English 中文(简体)
a. 单项物品
原标题:Array to individual pieces

I m 试图利用以下手段从一群人那里收集电子邮件:

def representative_email(group)
  group.representatives.collect{ |m|
    rep.User.find(m.user_id)
    rep.email
  }
end

但是,这产生了如下成果:[“email@email.com”,“email2@email.com”

• “

email@email.com, email2@email.com

事实上,最好用半殖民地取代 com。

最佳回答
def representative_email(group)
  group.representatives.collect{ |m|
    rep.User.find(m.user_id)
    rep.email
  }.join( ;  )
end

The result of your function (i.e. the result of Array#collect) is an Array; the brackets and the quotes are artifacts introduced by the way the Array got converted into a String. If you control the conversion process, it generally goes better - in that you know exactly what you receive.

在此情况下,你可以使用<代码>Array#join,把<代码>Array的表述放在一起,将<代码>String作为一 gl。

问题回答

值得注意的是,通过<代码>p或spect。 双重引证不属于示意图。 它们是代表文学体的方法。 方括号和文字表示它们属于阵列。 当你通过<代码>p或看到这些编号时,这些编号即行。 如果你想看他们如何实际看像,则使用<代码>puts

你所看到的是你所创作的“强化”阿雷拉版本。 如果你想以不同的方式编排格式,那么当你称:

emails = representative_email(group).join( ; )

您在使用<代码>find时也应非常谨慎。 如果能够找到你重新寻找的东西,那么可以而且将会 throw弃一种积极的规律:RecordNotFound例外,因此,你必须做好救援准备。 我不敢肯定你在座以“<代码”为呼声。 用户:经过限定的似乎已经过时。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签