请原谅newbiew的总问题,但为什么@game_score总是为零呢?
#bowling.rb
class Bowling
@game_score = 0
def hit(pins)
@game_score = @game_score + pins
end
def score
@game_score
end
end
Run Code Online (Sandbox Code Playgroud) bitbucket.org和bytebucket.org之间有什么关系?后者是前者的拥有者,还是某种骗局?
以下几种变量初始化方法之间是否存在差异?
@var ||= []
@var = [] if @var.nil?
@var = @var || []
Run Code Online (Sandbox Code Playgroud)
请分享初始化变量的方式,并说明利弊.
我有以下目录树.
- app.rb
- folder/
- one/
- one.rb
- two/
- two.rb
Run Code Online (Sandbox Code Playgroud)
我希望能够在文件夹/目录中加载Ruby文件,甚至是子目录中的文件.我该怎么办?
给出以下带有一个参数的方法:
def foo(arg); p arg; end
Run Code Online (Sandbox Code Playgroud)
我可以用空数组调用它:
foo([])
# prints []
Run Code Online (Sandbox Code Playgroud)
我也可以将它保存为一个Method对象,并使用一个空数组调用它,结果相同:
method(:foo).call([])
# prints []
Run Code Online (Sandbox Code Playgroud)
但是,如果我将Method对象转换为a Proc并使用空数组调用它,我得到一个ArgumentError:
method(:foo).to_proc.call([])
# ArgumentError: wrong number of arguments (0 for 1)
# from (irb):4:in `foo'
# from (irb):4:in `to_proc'
# from (irb):10:in `call'
Run Code Online (Sandbox Code Playgroud)
我预计它的行为与前两种情况相同.相反,它似乎表现得像我写的那样foo(*[]).但是,如果我有一个称之为非 -empty阵列,它确实表现我预期的方式:
method(:foo).to_proc.call([1])
# prints [1]
Run Code Online (Sandbox Code Playgroud)
所以它解构了参数,但前提是参数恰好是一个空数组.只有我打电话Method#to_proc.
我对如何Method或Proc工作的理解是否有差距,或者这是一个错误?
我正在运行Ruby 1.8.7-p299.我在1.8.6-p399和1.8.7-head中观察到相同的行为.但是,我没有在1.9.1-p378中看到它:[]当使用空数组调用时,所有三个表单都会打印出来.
我有这样的哈希:
[
{
:lname => "Brown",
:email => "james@intuit.com",
:fname => "James"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
},
{
:lname => "Smith",
:email => "brad@intuit.com",
:fname => "Brad"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
},
{
:lname => "Smith",
:email => "brad@intuit.com",
:fname => "Brad"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
}
]
Run Code Online (Sandbox Code Playgroud)
我想学习如何删除记录是否重复.意思是,看看有几个"brad@intuit.com"如何删除重复的记录,意味着删除所有其他有电子邮件"brad@intuit.com"的人....将电子邮件作为密钥而不是其他字段?
我正在尝试编写一个程序,根据从文件中读取的配置动态定义ruby类.我知道我可以使用Class.new来做到这一点.这是一个示例程序:
x = [1,2,3]
Test = Class.new do
@@mylist = x
def foo
puts @@mylist
end
end
Test.new.foo
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出(与ruby 1.9.3p0一起运行):
c:/utils/test.rb:4: warning: class variable access from toplevel c:/utils/test.rb:7: warning: class variable access from toplevel 1 2 3
有谁知道是什么导致这些警告以及我如何摆脱它们?
我已经尝试更换线路了
@@mylist = xRun Code Online (Sandbox Code Playgroud)
有了这个
class_variable_set(:@@mylist, x)Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我得到了这个错误:
c:/utils/test.rb:7: warning: class variable access from toplevel
c:/utils/test.rb:7:in `foo': uninitialized class variable @@mylist in Object (NameError)
from c:/utils/test.rb:11:in `'
提前致谢!
我今天在浏览Rails代码时偶然发现了这个片段:
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
Run Code Online (Sandbox Code Playgroud)
什么是星号 - 双结肠(如果你愿意的话,还是splat-le-colon)*::Date呢?
据推测,它与特定命名空间的Date类的范围有关......但是,作者必须包含它而不仅仅是使用标准Date类.
我一直试图弄清楚一些代码.我有一个表格,我正在尝试使用Wicked和Cocoon宝石.一切正常,包括link_to_add_association功能.我正在为Cocoon推荐的相关表单字段渲染部分,并且除了link_to_remove_association函数之外,一切似乎都在找工作.它返回以下错误:
new_record?nil的未定义方法:NilClass
这是我的部分错误:
<div class="nested-fields">
<div>
<%= f.input :address1 %>
</div>
<div>
<%= f.input :address2 %>
</div>
<div>
<%= f.input :city %>
</div>
<div>
<%= f.input :state %>
</div>
<div>
<%= f.input :postal %>
</div>
<div>
<%= link_to_remove_association "remove task", f %>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
以下是调用partial的视图:
<%= simple_form_for @vendor, url: wizard_path do |f| %>
<div id="locations">
<%= f.simple_fields_for :locations do |location| %>
<%= render 'location_fields', :f => location %>
<% end %>
<div class="links"> …Run Code Online (Sandbox Code Playgroud) ruby ×9
bitbucket ×1
cocoon-gem ×1
dynamic ×1
filesystems ×1
memoization ×1
object ×1
oop ×1
proc-object ×1
require ×1
ruby-1.8 ×1
scope ×1
splat ×1
syntax ×1
variables ×1
warnings ×1
wicked-gem ×1