What are the pros and cons of rspec mocking versus other mocking frameworks?

I have seen a lot of outdated podcasts that mention mocha as a gem I would want to install because it does mocking better then rspec. I have a feeling that the rspec developers have caught on to this and have improved their mocking since then. However, in the default spec_helper.rb file I see some commented out code stubs for three mocking frameworks mocha flexmock rr Upvote to anyone w

rspec嘲笑与其他嘲讽框架有什么优缺点?

我见过很多过时的播客,提到摩卡是我想要安装的宝石,因为它确实更好地嘲讽rspec。 我有一种感觉,那就是rspec开发者已经接触到这一点,并从那以后改进了他们的嘲笑。 但是,在默认的spec_helper.rb文件中,我看到一些注释掉三个spec_helper.rb框架的代码存根 mocha flexmock rr Upvote给那些能给我一个像样的答案的人,只要这些框架中的至少一个与rspec自己的嘲讽框架有所不同。 我会接受一个答案,如果你可以给我

DateTime serialization and deserialization

I'd like to serialize a Ruby DateTime object to json. Unfortunately, my approach is not symetrical: require 'date' date = DateTime.now DateTime.parse(date.to_s) == date => false I could use some arbitrary strftime/parse string combination, but I believe there must be a better approach. The accepted answer is not a good solution, unfortunately. As always, marshal/unmarshal is a tool

DateTime序列化和反序列化

我想将一个Ruby DateTime对象序列化为json。 不幸的是,我的方法并不是对称的: require 'date' date = DateTime.now DateTime.parse(date.to_s) == date => false 我可以使用一些任意的strftime / parse字符串组合,但我相信必须有更好的方法。 不幸的是,接受的答案并不是一个好的解决方案。 像往常一样,编组/解组是一种工具,你应该只用作最后的手段,但在这种情况下,它可能会破坏你的应用程序。 OP特别提到将

Multiple Ruby versions on one webserver?

The Ideal Using rvm, it would be awesome to be able to have multiple Rubies on one webserver, and through some sort of server configuration, be able to assign Ruby versions to different Rails/Sinatra/etc apps on a per-project basis. I am aware, from rvm's documentation, that Passenger only works with one Ruby at a time. :( The Compromise Failing that, it would be nice to at least be

一台网络服务器上有多个Ruby版本?

理想 使用rvm,能够在一台网络服务器上拥有多个Rubies,并通过某种服务器配置,能够按照每个项目将Ruby版本分配到不同的Rails / Sinatra / etc应用程序是非常棒的。 我知道,从rvm的文档中,Passenger一次只能用一个Ruby。 :( 妥协 否则,至少能够编写一种方法来将项目分配给Ruby 1.8或Ruby 1.9解释器会很好。 我读过使用Nginx作为反向代理允许在同一个盒子上运行Apache和Nginx。 那么是否有可能让Apache + Passenger

Inconsistent implicit hash creation in Ruby?

Ok, so I was comparing some stuff in my own DSL to Ruby. One construct they both support is this x=["key" => "value"] Knowing the difference between arrays and hashes, I would think this to be illegal, but the result in Ruby is [{"key" => "value"}] Why is this? And with this kinda syntax why can't you do x=("key" => "value") Why is an array a special case for implicitly crea

在Ruby中不一致的隐式哈希创建?

好的,我将我自己的DSL中的一些东西与Ruby比较。 他们都支持的一个构造就是这样 x=["key" => "value"] 知道数组和哈希之间的区别,我认为这是非法的,但Ruby中的结果是 [{"key" => "value"}] 为什么是这样? 用这种有点语法,为什么你不能这样做 x=("key" => "value") 为什么数组是隐式创建哈希的特例? 另一个特例是函数调用,请考虑: def f(x) puts "OK: #{x.inspect}" end f("foo" => "bar") =>

How do I know if a BigDecimal failed to parse?

I'm importing data from a csv, I need to cast some values to BigDecimal, and raise an error if they can't be parsed.. From testing, BigDecimal("invalid number") returns a BigDecimal of 0. This would be ok, but kind of messy, except a valid value is 0... Float("invalid number") acts differently and throws an exception... My current solution is: class String def

如何知道BigDecimal是否无法解析?

我从csv导入数据,我需要将一些值转换为BigDecimal,并且如果它们不能被解析就会报错。 从测试,BigDecimal(“无效数字”)返回一个BigDecimal为0.这将是好的,但有点混乱,除了一个有效的值是0 ... 浮动(“无效数字”)的行为不同,并引发异常... 我目前的解决方案是: class String def to_bd begin Float(self) rescue raise "Unable to parse: #{self}" end BigDecimal(self) end end

Can I test that a Sinatra post method successfully saves to a YAML store?

I can't find a basic explanation anywhere about how I can test, with Rack::Test, that a Ruby/Sinatra post method successfully saves data to a YAML store/file. (This explains testing get , which I can do(!), but not post ; other mentions of testing post methods with rack/test seem irrelevant.) For self-study, I'm building a "to do" app in Ruby/Sinatra and I'm trying to use T

我可以测试Sinatra post方法是否成功保存到YAML商店?

我无法在Rack :: Test的任何地方找到关于如何测试的基本解释,即Ruby / Sinatra post方法成功地将数据保存到YAML存储/文件。 (这解释了测试get ,我可以做(!),但不能post ;其他提到的使用机架/测试测试post方法的提法似乎不相关。)对于自学,我正在构建一个“待办事宜” Sinatra和我正在尝试使用TDD的一切和单元测试,就像一个好孩子。 我的要求是:当用户发布新任务时,它会保存在YML商店中。 我正在考虑通过查看是否在

Open Excel files on a remote computer

I want to programmatically open Excel files on a user's computer, read what is in the first cell, then save this to a mysql db on my webapp with the following info: cell content (text) file_updated_date (date) Is this possible? Will the user have to install a desktop application for me to open files on their computer, or can I get permission to run a script and return information from a w

在远程计算机上打开Excel文件

我想以编程方式打开用户计算机上的Excel文件,阅读第一个单元格中的内容,然后将其保存到我的Web应用程序中的一个MySQL数据库中,并使用以下信息: cell content (text) file_updated_date (date) 这可能吗? 用户是否必须安装桌面应用程序才能在他们的计算机上打开文件,或者我是否可以获得运行脚本并从网站返回信息的权限? 有哪些语言或技术可用于打开Excel文件,读取内容并发送到http端点? 这是一个Ruby程序吗? R

Inheriting class methods from modules / mixins in Ruby

It is known that in Ruby, class methods get inherited: class P def self.mm; puts 'abc' end end class Q < P; end Q.mm # works However, it comes as a surprise to me that it does not work with mixins: module M def self.mm; puts 'mixin' end end class N; include M end M.mm # works N.mm # does not work! I know that #extend method can do this: module X; def mm; puts 'extender' end end Y = Cl

在Ruby中继承modules / mixins中的类方法

众所周知,在Ruby中,类方法被继承: class P def self.mm; puts 'abc' end end class Q < P; end Q.mm # works 但是,对于我来说,它并不适用于mixin: module M def self.mm; puts 'mixin' end end class N; include M end M.mm # works N.mm # does not work! 我知道#extend方法可以做到这一点: module X; def mm; puts 'extender' end end Y = Class.new.extend X X.mm # works 但是我正在编写一个包含实例方

Why would we put a module inside a class in Ruby?

In Ruby, I see that it can be useful to put classes inside modules for the sake of namespacing. I also see that it's possible to put modules inside classes. But I don't see why you'd do that. Modules are generally mixed into classes, right? So, what would be the purpose of defining a module inside a class? We could use it when writing ape-like code like this: class DrugDealer

为什么我们要在Ruby的类中放一个模块?

在Ruby中,为了命名空间的缘故,我发现将模块放在模块内可能很有用。 我也看到可以将模块放在类中。 但我不明白你为什么这么做。 模块通常被混合到类中,对吗? 那么,在类中定义模块的目的是什么? 我们可以在编写像这样的类猿代码时使用它: class DrugDealer module Drug def happy?; true; end end def approach(victim) victim.extend Drug end end o = Object.new DrugDealer.new.approach(o) o.

Ruby function to remove all white spaces?

What is the Ruby function to remove all white space? Kind of like php's trim() ? 如果你只想删除前后的空格(比如PHP的修剪),你可以使用.strip ,但是如果你想删除所有的空格,你可以使用.gsub(/s+/, "") 。 s = "I have white space".delete(' ') 并模仿PHP的trim()函数: s = " I have leading and trailing white space ".strip Related answer: " clean up my edges ".strip returns "cl

Ruby函数删除所有空格?

什么是Ruby功能来删除所有的空格? 有点像PHP的trim() ? 如果你只想删除前后的空格(比如PHP的修剪),你可以使用.strip ,但是如果你想删除所有的空格,你可以使用.gsub(/s+/, "") 。 s = "I have white space".delete(' ') 并模仿PHP的trim()函数: s = " I have leading and trailing white space ".strip 相关解答: " clean up my edges ".strip 回报 "clean up my edges"