我需要一些TDD概念的帮助.说我有以下代码
def execute(command)
case command
when "c"
create_new_character
when "i"
display_inventory
end
end
def create_new_character
# do stuff to create new character
end
def display_inventory
# do stuff to display inventory
end
Run Code Online (Sandbox Code Playgroud)
现在我不确定要为我的单元测试编写什么.如果我为该execute方法编写单元测试并不能完全覆盖我的测试create_new_character和display_inventory?或者我在那时测试错误的东西?我对该execute方法的测试是否只测试执行是否传递给正确的方法并停在那里?然后,我应该写更多的单元测试是专门测试create_new_character和display_inventory?
我试图了解是什么attr_accessor让我访问.据我所知,它提供了getter和setter方法.所以attr_accessor :color它会为我创造类似下面的东西
def color
@color
end
def color=(value)
@color = value
end
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么在下面的代码中,为什么我不能color=在我的初始化器中使用?(它最终是空白的).为什么我需要使用@color=或self.color=代替?不color=应该是一种调用上面刚为我创建的setter方法的方法吗?
class Bird
attr_accessor :color
def initialize(c="green")
color = c # this doesn't work
# either one of the following DOES work
# @color = c
# self.color = c
end
end
puts Bird.new.color # prints nothing unless using @color or self.color
Run Code Online (Sandbox Code Playgroud) 说我有这样的字符串
"some3random5string8"
Run Code Online (Sandbox Code Playgroud)
我想在每个整数后插入空格,所以它看起来像这样
"some3 random5 string8"
Run Code Online (Sandbox Code Playgroud)
我特别想要使用gsub但我无法弄清楚如何访问与我的正则表达式匹配的字符.
例如:
temp = "some3random5string8"
temp.gsub(/\d/, ' ') # instead of replacing with a ' ' I want to replace with
# matching number and space
Run Code Online (Sandbox Code Playgroud)
我希望有一种方法可以引用正则表达式匹配.像$1这样的东西,我可以做一些事情temp.gsub(/\d/, "#{$1 }")(注意,这不起作用)
这可能吗?
我正在开发一个具有以下黄瓜步骤的项目:
Given /^no registered users$/ do
User.delete_all
end
Run Code Online (Sandbox Code Playgroud)
作为一个新的RoR用户,即使我在我们的开发数据库上进行测试,这看起来有点危险,因为我们的User表有实际数据.什么是代码行?
谢谢!
我正在使用jquery工具叠加,它很棒.但是,滚动行为有点奇怪.如果您打开叠加层并将鼠标放在叠加层上,如果您位于叠加层的顶部/底部,则仍可以在其后面滚动页面.
有没有办法(最好是一个内置的jquery工具)来阻止页面覆盖滚动?
说我有以下内容:
class Foo
def inspect
false
end
def ==(other)
false == other
end
end
foo = Foo.new # => false
foo # => false
foo == false # => true
foo == true # => false
Run Code Online (Sandbox Code Playgroud)
然而
if foo then 'hi' end
# => 'hi'
Run Code Online (Sandbox Code Playgroud)
我所期待的是
if false then 'hi' end
# => nil
Run Code Online (Sandbox Code Playgroud)
我期待foo评估,false但事实并非如此.如何if评估foo,为什么不评估false?
我有一个用名为日期数组的data键命名的哈希值。checks如果该键存在,我想附加到现有数组。如果该键不存在,我需要添加一个包含当前日期的数组。在 ruby 中是否有一种优雅的方法来做到这一点?
这就是我现在正在做的事情,这似乎比应该做的更难:
if data.has_key?('checks')
data['checks'] << DateTime.now
else
data['checks'] = Array.wrap(DateTime.now)
end
Run Code Online (Sandbox Code Playgroud)