我有三个Ruby数组:
[1, 2, 3, 4]
[2, 3, 4, 5]
[3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
如何获取位置0,位置1等所有三个数字的平均值,并将它们存储在一个名为"平均"的新数组中?
我正在阅读Blogger教程的"标签"部分,我对一部分感到困惑:def to_s函数(在tag.rb中); 为什么需要它以及如何包含它.
我已经为相关文件中包含了相关文件的一些相关部分.
楷模
article.rb
class Article < ActiveRecord::Base
attr_accessible :tag_list
has_many :taggings
has_many :tags, through: :taggings
def tag_list
return self.tags.collect do |tag|
tag.name
end.join(", ")
end
def tag_list=(tags_string)
self.taggings.destroy_all
tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
tag_names.each do |tag_name|
tag = Tag.find_or_create_by_name(tag_name)
tagging = self.taggings.new
tagging.tag_id = tag.id
end
end
end
Run Code Online (Sandbox Code Playgroud)
tag.rb
class Tag < ActiveRecord::Base
has_many :taggings
has_many :articles, through: :taggings
def to_s
name
end
end
Run Code Online (Sandbox Code Playgroud)
tagging.rb
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :article
end
Run Code Online (Sandbox Code Playgroud)
CONTROLLERS
tags_controller.rb …
混合exec命令和print命令时使用分号不起作用.最好的方法是什么?
print "Initializing tests...\n"
print 'Testing 00_hello\n'
exec 'cd 00_hello; rspec hello_spec.rb; cd ..'
print 'Testing 01_temperature\n'
exec 'cd 01_temperature; rspec temperature_spec.rb; cd ..'
Run Code Online (Sandbox Code Playgroud) 此网站呈现如下:
测试
测试
$ 1.00包装
测试
测试
$ 1.00包装
你好!
这是一个描述.
$ 125135.13
test.gif
产品#1
产品#1描述.
$ 100.00
N/A
[#,photo:"",created_at:"2013-03-16 06:17:30",updated_at:"2013-03-16 06:17:30">,#,photo:"",created_at:"2013 -03-16 06:17:48",updated_at:"2013-03-16 06:17:48">,#,照片:"test.gif",created_at:"2013-03-16 06:24:13 ",updated_at:"2013-03-16 06:24:13">,#,照片:"N/a",created_at:"2013-03-16 13:16:35",updated_at:"2013-03- 16 13:16:35">]
index.html.erb
<h1>Product Listings</h1>
<%= @products.each do |p| %>
<li><%= p.name %></li>
<li><%= p.description %></li>
<li><%= sprintf("$%0.02f", p.price) %></li>
<li><%= p.photo %></li>
<% end %>
Run Code Online (Sandbox Code Playgroud)
关于如何制作Rails的任何想法只显示模型的迭代而不是底部的哈希?