小编tor*_*o2k的帖子

命令rbenv install缺失

在Ubuntu 10.04中,我刚刚安装了rbenv.安装命令不存在.

rbenv 0.4.0-49-g8b04303
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List …
Run Code Online (Sandbox Code Playgroud)

ruby rbenv

121
推荐指数
7
解决办法
5万
查看次数

在MiniTest的assert_raises/must_raise中检查异常消息的预期语法是什么?

什么是在MINITEST的检查异常消息预期的语法assert_raises/ must_raise

我正在尝试做出如下所示的断言,其中"Foo"是预期的错误消息:

proc { bar.do_it }.must_raise RuntimeError.new("Foo")
Run Code Online (Sandbox Code Playgroud)

ruby tdd minitest assertion

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

如何假冒Time.now?

Time.now在单元测试中设置测试时间敏感方法的最佳方法是什么?

ruby time unit-testing

70
推荐指数
6
解决办法
2万
查看次数

如何使用open-uri处理503错误?

如果网站返回"503 service unavailable"错误,则open-uri会抛出异常.例如:

require 'open-uri'
open('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8')
# OpenURI::HTTPError: 503 Service Unavailable
# ...
Run Code Online (Sandbox Code Playgroud)

但是,如果您随后在Web浏览器中访问它,它实际上会显示一个带有CAPTCHA而不是错误的页面.

我如何确保open-uri不仅仅将此作为异常,而是实际处理响应并向我提供页面内容?

ruby error-handling open-uri

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

获取字符串中的最后一个字

我想获取字符串中的最后一个字符MY WAY - 1)获取最后一个索引2)获取最后一个索引处的字符,作为STRING.之后我会将字符串与另一个字符串进行比较,但我不会在此处包含该部分代码.我尝试了下面的代码,我得到了一个奇怪的数字.我使用的是ruby 1.8.7.

为什么会这样,我该怎么办?

line = "abc;"
last_index = line.length-1
puts "last index = #{last_index}"
last_char = line[last_index]
puts last_char
Run Code Online (Sandbox Code Playgroud)

输出 -

last index = 3
59
Run Code Online (Sandbox Code Playgroud)

Ruby文档告诉我,数组切片以这种方式工作 -

a = "hello there"
a[1] #=> "e"
Run Code Online (Sandbox Code Playgroud)

但是,在我的代码中它没有.

ruby ruby-1.8.7

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

如何检查两个其他值之间是否包含值?

我试图表达这样的条件:

if 33.75 < degree <= 56.25
  # some code
end
Run Code Online (Sandbox Code Playgroud)

但是Ruby给出了这个错误:

undefined method `<=' for true:TrueClass
Run Code Online (Sandbox Code Playgroud)

我猜测一种方法是这样的:

if 33.75 < degree and degree <= 56.25
  # code
end
Run Code Online (Sandbox Code Playgroud)

但是没有另一种更简单的方法吗?

ruby conditional-statements

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

有没有办法知道方法需要多少参数?

使用irb,我们可以通过以下方式列出特定对象的方法:

"Name".methods
Run Code Online (Sandbox Code Playgroud)

但是如果我想知道特定方法需要多少参数,我该如何实现呢?我的意思是有什么办法(通过在irb上点击一些命令),我们可以得到特定方法的参数数量(而不是指代文档)?

.methods 仅返回方法名称,而不返回方法的参数列表.

ruby

18
推荐指数
2
解决办法
5430
查看次数

Ruby on Rails,一种形式的两个模型

我有两个非常相似的模型预处理和诊断,属于模型患者:

class Pretreatment < ActiveRecord::Base
  belongs_to :patient
  attr_accessible :content
end

class Diagnosis < ActiveRecord::Base
  belongs_to :patient
  attr_accessible :content
end

class Patient < ActiveRecord::Base
  attr_accessible :age, :name, :city, :street, :number
  has_many :anamneses
  has_many :befunds
end
Run Code Online (Sandbox Code Playgroud)

Patient显示页面上,我显示了两个表单,一个用于Preatreatment另一个用于Diagnosis:

<%= form_for([@patient, @patient.preatreatments.build]) do |f| %>
  <div class="field">
    <%= f.label :conten %><br />
    <%= f.text_field :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<%= form_for([@patient, @patient.diagnosiss.build]) do |f| %>
  <div class="field">
    <%= f.label :content %><br …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-3

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

如果行为不同,Ruby if if vs end?

为什么这段代码不起作用?

b if b = true
Run Code Online (Sandbox Code Playgroud)

错误: undefined local variable or method `b'

但这样做:

if b = true
    b
end
Run Code Online (Sandbox Code Playgroud)

它们不应该是一样的吗?

ruby

16
推荐指数
1
解决办法
2166
查看次数

Cron与notify-send

我需要显示来自cron作业的通知.我的crontab是这样的:

$ crontab -l
# m h  dom mon dow   command
  * *   *   *   *    Display=:0.0 /usr/bin/notify-send Hey "How are you"
Run Code Online (Sandbox Code Playgroud)

我检查过/var/log/syslog,命令实际上每分钟执行一次,但它没有弹出通知.谁能帮助我理解为什么?

linux cron notify

16
推荐指数
5
解决办法
9612
查看次数