标签: ruby

RSpec:每次指定对具有不同参数的方法的多次调用

在rspec(1.2.9)中,指定对象每次都会接收到具有不同参数的方法的多次调用的正确方法是什么?

我问因为这个令人困惑的结果:

describe Object do

  it "passes, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1) # => Mock "foo" expected :bar with (1) once, but received it twice
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(2) # => Mock "foo" received :bar out of order
    foo.bar(1)
  end

  it "fails, as expected, but with an unexpected message" do
    foo = mock('foo') …
Run Code Online (Sandbox Code Playgroud)

ruby testing rspec

37
推荐指数
1
解决办法
2万
查看次数

数组到哈希:单词计数

我有一些单词,我想得到一个哈希,其中键是单词,值是字数.

有没有更美丽的方式,然后我的:

result = Hash.new(0)
words.each { |word| result[word] += 1 }
return result
Run Code Online (Sandbox Code Playgroud)

ruby

37
推荐指数
3
解决办法
2万
查看次数

Ruby局部变量未定义

我有以下Ruby代码:

local_var = "Hello"

def hello
  puts local_var
end

hello
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

local_variables.rb:4:in 'hello': undefined local variable or method 'local_var' 
for main:Object (NameError) from local_variables.rb:7:in '<main>'
Run Code Online (Sandbox Code Playgroud)

我一直认为局部变量不能从块外部,函数,闭包等访问.

但是现在我在文件中定义了局部变量,并尝试从INSIDE函数获取对同一文件的访问权限 .

我的理解有什么问题?

ruby local-variables

37
推荐指数
1
解决办法
4万
查看次数

CSV.read x行中的非法引用

我正在使用带有大量数据的ruby CSV.read.库有时会遇到格式不正确的行,例如:

"Illegal quoting in line 53657."
Run Code Online (Sandbox Code Playgroud)

忽略该行并跳过它,然后遍历每个csv并修复格式将更容易.我怎样才能做到这一点?

ruby csv formatting

37
推荐指数
4
解决办法
2万
查看次数

Integer(value)和value.to_i之间的差异

给定一个像这样的字符串对象:

twohundred = "200"
Run Code Online (Sandbox Code Playgroud)

做的有什么区别:

Integer(twohundred)  #=> 200
Run Code Online (Sandbox Code Playgroud)

和:

twohundred.to_i      #=> 200
Run Code Online (Sandbox Code Playgroud)

有什么区别吗?是否建议在另一个中使用其中一个?

ruby

37
推荐指数
2
解决办法
1万
查看次数

Ruby:将unix时间戳转换为日期

irb> dt = Time.now.utc
=> 2012-04-26 10:47:01 UTC

irb> dti = dt.to_i
=> 1335437221
Run Code Online (Sandbox Code Playgroud)

现在,如何转换dti回日期/时间?

ruby datetime unix-timestamp

37
推荐指数
1
解决办法
4万
查看次数

如何在Ruby中的RestClient gem中设置超时?

我通过它来调用服务器来使用RestClient gem.问题是如何从客户端设置超时.

RestClient.get "http://127.0.0.1:7819/tokenize/word/stackoverflow"
Run Code Online (Sandbox Code Playgroud)

我想把它设置为10秒.

提前致谢!!

ruby timeout rest-client

37
推荐指数
1
解决办法
2万
查看次数

gets.chomp()与STDIN.gets.chomp()之间有什么区别?

它们是相同的,还是两个命令之间存在细微差别?

ruby

37
推荐指数
4
解决办法
2万
查看次数

如何使用Ruby Rest-Client处理异常

我最近从Ruby的Net:HTTP类切换到rest-client 1.6.7.

我发现形成请求要容易得多,但与Net:HTTP请求不同,当rest-client获得200以外的任何东西时,请求就会消失.我已经尝试在RestClient.get之后直接放置一个断点,它永远不会被击中 - 所以我做错了.

def get_member_using_card
  resource = "#{@settings_app_uri}api/v1/card/#{self.member_card_num}?token=#{@settings.api_key}"
  response = RestClient.get resource
  if response.code == 200 
    card = JSON.parse(response.body)
    self.customer_id = card['card']['customer_id']
  else
    return 0
  end
end
Run Code Online (Sandbox Code Playgroud)

这导致了这个堆栈跟踪:

RestClient::ResourceNotFound - 404 Resource Not Found:
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/abstr
act_response.rb:48:in `return!'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:230:in `process_result'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:178:in `block in transmit'
        /Users/tim/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:627:in `start'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:172:in `transmit'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:64:in `execute'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient/reque
st.rb:33:in `execute'
        /Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/rest-client-1.6.7/lib/restclient.rb:68
:in `get'
Run Code Online (Sandbox Code Playgroud)

有人能告诉我如何正确评估响应代码并防止这种异常发生......?

ruby rest-client

37
推荐指数
3
解决办法
4万
查看次数

如何打开(读写)或创建允许截断的文件?

我想要:

  • 以读写模式打开文件(如果存在);
  • 如果它不存在就创建它;
  • 能够随时随地截断它.

编辑:使用truncate我的意思是写入一个位置并丢弃文件的剩余部分(如果存在)

所有这些原子(通过单个open()调用或模拟单个open()调用)

似乎没有单一的开放模态适用:

  • r:显然不起作用;
  • r +:如果文件不存在则失败;
  • w:重新创建文件(如果存在);
  • w +:如果存在则重新创建文件;
  • a:看不懂;
  • a +:不能截断.

我试过的一些组合(rw,rw +,r + w等)似乎也不起作用.可能吗?

来自Ruby的一些文档(也适用于python):

r
Read-only mode. The file pointer is placed at the beginning of the file.
This is the default mode.

r+
Read-write mode. The file pointer will be at the beginning of the file.

w
Write-only mode. Overwrites the file if the file exists. If the file
does not exist, creates a new …
Run Code Online (Sandbox Code Playgroud)

ruby python truncate

36
推荐指数
2
解决办法
4万
查看次数