-1
A
回答
2
我想有很多可以做这种宝石的,但是如果你想要滚你自己的,你可以像这样做,在一个相当普遍的方式:
你输入由列标签:
col_labels = { date: "Date", from: "From", subject: "Subject" }
和用于行中的数据:
arr = [{date: "2014-12-01", from: "Ferdous", subject: "Homework this week"},
{date: "2014-12-01", from: "Dajana", subject: "Keep on coding! :)"},
{date: "2014-12-02", from: "Ariane", subject: "Re: Homework this week"}]
其中col_labels
和arr
的元件具有相同的密钥。
从现在起,代码是一般的。首先构造一个散列@columns
(为了方便起见,我已经创建了一个实例变量)。
@columns = col_labels.each_with_object({}) { |(col,label),h|
h[col] = { label: label,
width: [arr.map { |g| g[col].size }.max, label.size].max } }
# => {:date=> {:label=>"Date", :width=>10},
# :from=> {:label=>"From", :width=>7},
# :subject=> {:label=>"Subject", :width=>22}}
def write_header
puts "| #{ @columns.map { |_,g| g[:label].ljust(g[:width]) }.join(' | ') } |"
end
def write_divider
puts "+-#{ @columns.map { |_,g| "-"*g[:width] }.join("-+-") }-+"
end
def write_line(h)
str = h.keys.map { |k| h[k].ljust(@columns[k][:width]) }.join(" | ")
puts "| #{str} |"
end
write_divider
write_header
write_divider
arr.each { |h| write_line(h) }
write_divider
+------------+---------+------------------------+
| Date | From | Subject |
+------------+---------+------------------------+
| 2014-12-01 | Ferdous | Homework this week |
| 2014-12-01 | Dajana | Keep on coding! :) |
| 2014-12-02 | Ariane | Re: Homework this week |
+------------+---------+------------------------+
如果你想扭转显示器,使之成为大一点,像你这样的,首先执行:
$_!.reverse
[email protected] += 4
+0
巨人的答案..你让Postgresql DB像输出...恭喜...... + 1 – 2015-02-24 07:02:55
相关问题
- 1. 在Powershell中打印数组
- 2. 在sendmail中打印数组
- 3. 在Firefox中用Ruby打印
- 4. 打印列表数组
- 5. 在Ruby中比较数组并打印常见元素
- 6. 如何在ruby中打印多维数组?
- 7. 从数组列表中打印问题
- 8. 打印数组?
- 9. 打印数组
- 10. 动态索引多维数组打印在foreach循环中并打印在表
- 11. 在数组中向后打印数字
- 12. Ruby:如何从并行数组中打印字符串
- 13. 打印类别打印数组?
- 14. C通用打印数组函数 - 打印字符串数组
- 15. 如何在gdb中打印Java数组?
- 16. 在PHP数组中打印项目
- 17. 如何在数组中打印对象?
- 18. 在jquery中创建和打印数组
- 19. 在Javascript中打印多维数组
- 20. 在java中使用long []打印数组
- 21. 在int数组中打印出位
- 22. 在python中打印numpy数组
- 23. 如何在GDB中打印Fortran数组?
- 24. 在Java中打印二维数组
- 25. 如何在javascript中打印php数组
- 26. 如何在java中打印数组?
- 27. 在循环中打印数组元素
- 28. 在Java中打印数组的索引
- 29. 在objective-c中打印数组?
- 30. 在C中打印二维数组
...什么呢你的数组看起来像?到目前为止尝试过的东西? – ptierno 2015-02-23 22:21:26
为了让每个准备答案的读者不必构建包含输入数据的同一对象,请编辑以提供答案,以便他们可以复制和粘贴。例如,'arr = [{date:“2014-12-01”,来自:“Ferdous”,主题:“本周作业”},{date:“2014-12-01”,来自:“Dajana”,主题:“Keep on coding!:)”},{date:2014-12-02,来自:“麦当娜”,主题:“Re:本周作业”}]'。 – 2015-02-23 22:26:49
你需要提供一些关于你的环境的更多信息(它是一个rails项目吗?这是一个'ActiveRecord'表吗?等等)和一些关于你的代码的东西(例如相关类的一个例子)以及你所做的尝试至今。 – xlembouras 2015-02-23 22:30:04