模块阵列输出 [英] module array output

查看:38
本文介绍了模块阵列输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模块来为 FlagShihTzu gem 添加功能.

I'm writing a module to add functionality to the FlagShihTzu gem.

基本上它通过标志并输出分配给对象的键.它正在工作,但我也希望能够在视图中使用块来处理输出.

Basically it goes through the flags and outputs the keys for the ones assigned to the object. It's working, but I also want to be able to use a block in the view to do things with the output.

问题在于它同时输出模块中的数组和视图中块的输出.

The problem is that it's outputting both the array from the module and the output from the block in the view.

module AwesomeFlags

  def my_flags(column = nil)
    a = self.flag_mapping
    if column.nil?
      c = a.values.map {|var| var.keys}.flatten
    else
      b = a[column]
      c = Array.[](b.keys).flatten
    end
    c.map {|var| self.send(var) ? "#{var.to_s} " : nil}.compact!
  end

end

在视图中:

= book_offer.my_flags.each do |flag|
  = flag.titleize

我得到的是:

Regular Complimentary regular complimentary

推荐答案

您应该将其改为:

- book_offer.my_flags.each do |flag|
  = flag.titleize

= 表示包含方法调用的输出,其中 - 表示简单地执行它.each 循环返回列表中的项目.

The = means to include the output of the method call, where - means to simply execute it. The each loop is returning the items in the list.

这篇关于模块阵列输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆