我需要验证我的类的任何实例都接收到某种方法,但我不关心是否有许多实例接收它(它们应该是).
我试过这样的:
expect_any_instance_of(MyClass).to receive(:my_method).at_least(:once)
Run Code Online (Sandbox Code Playgroud)
但显然,它只允许单个实例多次接收方法,但不允许不同的实例.
有没有办法实现这一目标?
我尝试比较我的Ruby应用程序中的标志.
我有这个代码:
if self.flag &~ flag == self.flag
return false
Run Code Online (Sandbox Code Playgroud)
但它不会运行.我把问题缩小到了这个范围:
irb(main):020:0> my_user.flag
=> 1
irb(main):021:0> flag
=> 128
irb(main):022:0> my_user.flag.class
=> Fixnum
irb(main):023:0> flag.class
=> Fixnum
irb(main):024:0> my_user.flag &~ flag
TypeError: wrong argument type Fixnum (expected Proc)
Run Code Online (Sandbox Code Playgroud)
这真是令人不安,因为它的工作原理如下:
irb(main):025:0> 1 &~ 128
=> 1
Run Code Online (Sandbox Code Playgroud) 我希望能够从调用方法返回而仍在被调用方法内部。
范例:
def calling_method
# stuff
called_method
# more stuff
end
def called_method
# stuff
return_from_caller if foo # << I would like to return from calling_method
# more stuff
end
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以做到这一点?
我目前使用的“肮脏”方式是这样的:
def calling_method
# stuff
called_method and return
# more stuff
end
def called_method
# stuff
return false if foo
# more stuff
end
Run Code Online (Sandbox Code Playgroud)
但这并不完全令人满意,因为我必须and return在调用方法中执行一个。
在我的模型中,我有users(User)和stories(Story),关系:user has_many stories.
我在shell中发现了一些奇怪的东西:
(dev) user.stories.any?
=> true
(dev) user.stories
Story Load (1.6ms) SELECT "stories".* FROM "stories" WHERE "stories"."user_id" = 703 ORDER BY created_at ASC [["user_id", 703]]
=> []
(dev) user.stories.any?
=> false
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?这是由于我的代码,还是Rails中的某种错误以及它查询数据库的方式?
我的反应应用程序中有两个文件:
/* MyApp/components/my-component.jsx */
export class MyComponent extends React.Component {
// ...
};
console.log(MyComponent); // (1)
Run Code Online (Sandbox Code Playgroud)
和
/* MyApp/my-app.jsx */
import MyComponent from './components/my-component';
console.log(MyComponent); // (2)
Run Code Online (Sandbox Code Playgroud)
console.log数字(1)给了我这个:function MyComponent(props, context) {...。但是console.log数字(2)给了我undefined。
我究竟做错了什么?这看起来很简单,但行不通。
我想计算数组中的真实对象.因为我可以通过一个块来计算,我找到的最惯用的方式是:
[1, nil, 'foo', false, true].count { |i| i }
#=> 3
Run Code Online (Sandbox Code Playgroud)
但是我想知道是否有更好的方法,特别是使用语法count(&:something),因为在这里传递一个完整的块看起来对我来说太过分了.
AFAIK,truthy?Ruby中没有方法,所以我找不到如何实现这一点.
我目前在我的routes.rb:
namespace :api
namespace :v1
namespace :me
# ...
resources :offers do
resources :state, only: %i(index)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这给了我这条路线:
GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/state#index
Run Code Online (Sandbox Code Playgroud)
但我想要的路线是这样的:
GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/offers/state#index
Run Code Online (Sandbox Code Playgroud)
简单地说,我希望能够将我的state_controller.rb放在一个offers文件夹中,而无需更改访问它的路径。我怎样才能做到这一点?
我想拆分这样的字符串:
my_string = "I want to split this (these should stay together) correctly"
Run Code Online (Sandbox Code Playgroud)
并得到以下结果:
["I", "want", "to", "split", "this", "(these should stay together)", "correctly"]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
my_string.split(/(?=[^\(]){1,} (?=[^\)]){1,}/)
Run Code Online (Sandbox Code Playgroud)
但是圆括号内的元素是分开的.我怎样才能做到这一点?
ruby ×5
fixnum ×1
import ×1
javascript ×1
regex ×1
return ×1
routes ×1
rspec ×1
rspec-mocks ×1
sql ×1