I am using Ruby on Rails 3.1.0 and I am trying to use YARD 0.7.4. I would like to document constant values, so in my class I state this:
# [Fixnum] Test constant documentation.
TEST_CONSTANT = 1
Run Code Online (Sandbox Code Playgroud)
but the HTML output after I run the yardoc (or yard doc) command is the following:

In the above image is shown that the HTML documentation is not good rendered/generated, at least not as I would expect. I read the YAML documentation …
我正在使用Ruby on Rails 3.2.2,我想检查a Integer是否大于,0而且更一般地说,如果a Integer大于另一个Integer.
有一些Ruby或Ruby on Rails 方法可以"轻松"/"有效"地实现这一目标吗?
注意:我想在我的视图文件中使用/声明该方法,我认为,如果该方法不"存在",最好在我的模型或控制器文件中声明"专用"方法并使用该方法我的看法.
我正在使用jQuery,我有以下代码:
var result = [];
if ( some_condition ) {
result = [...]
} else {
$.ajax({
url: some_url,
data: some_data,
dataType: 'json',
success: function(data) {
items = data
}
});
result = items
}
// Playing with the 'result' variable...
Run Code Online (Sandbox Code Playgroud)
上面的代码生成错误" items is not defined" some_condition是false什么时候(我认为这是因为变量范围不正确).
我想将result变量设置为AJAX响应数据,但我不知道如何解决问题.
注意:我正在尝试这样做,因为我想在语句之外使用result变量(即,在上面的代码中的语句之后).if ... elseif ... else
我正在使用Ruby on Rails 3.2.9,我想验证名称(a String)的第一个字符不是数字(Integer).我想使用以下代码:
class User < ActiveRecord::Base
validates_each :name do |record, attr, value|
record.errors.add(attr, 'cannot begin with a number') if ... # the first char is a number
end
end
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我正在使用Ruby on Rails 3.2.2,我想通过引发"自定义"错误消息来正确地挽救以下流程:
def rescue_method
# sample_string.class
# => String
# sample_string.inspect
# => "ARubyConstantThatDoesNotExist"
begin
build_constant(sample_string)
rescue
raise("My custom error message: #{build_constant(sample_string)} doesn't exist.")
end
end
def build_constant(sample_string)
"AModule::#{sample_string}".constantize
end
Run Code Online (Sandbox Code Playgroud)
注意:我觉得"强制" constantize在引发的"自定义"消息中也使用该方法以便干掉代码...
当rescue_method执行时,似乎raise("My custom error message")代码永远不会执行,我收到以下错误:
uninitialized constant AModule::ARubyConstantThatDoesNotExist
Run Code Online (Sandbox Code Playgroud)
如何正确显示引发的"自定义"消息(因为在后续引发的"自定义"消息中引发了进一步的错误异常)?你有什么建议?
我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3.给定一个字符串我想删除该字符串末尾的所有 /字符.也就是说,例如:
From "abc/" to "abc"
From "abc//" to "abc"
From "abc///" to "abc"
...
From "a/b/c///" to "a/b/c"
Run Code Online (Sandbox Code Playgroud)
我该怎么做(可能是通过使用正则表达式)?
我试过了:
string = string[0,string.length-1] if string.end_with?('/')
Run Code Online (Sandbox Code Playgroud)
但它只适用于一个角色.
我正在使用Ruby on Rails 3.2.2,我想以最简单的方式构建一个长字符串.我想过使用这个times方法,但是使用下面的代码它不会返回我正在寻找的:
10000.times{ "Foo bar" }
# => 10000
Run Code Online (Sandbox Code Playgroud)
我想它会回来"Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar ...".
我该怎么做?
注意:我想在我的rspec文件中使用上面的代码进行测试.
我正在使用Ruby on Rails 3.2.2,我试图缩短以下代码:
custom_values.each do |custom_value|
raise("Error!") unless AVAILABLE_CUSTOM_VALUES.include?(custom_value)
end
Run Code Online (Sandbox Code Playgroud)
有办法做到这一点?如果是这样,如何缩短(或可能,改进)代码?
我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3.我有以下case声明:
case
when private?
case
when not_active? then [:a, :b, :c, :d]
when active? then raise "private cannot be active"
else raise "not recognized"
end
when shared?
case
when not_active? then [:a, :b, :c]
when active? then raise "shared cannot be active"
else raise "not recognized"
end
when public?
case
when not_active? then [:a, :b]
when active? then [:a]
else raise "not recognized"
end
else raise "not recognized"
end
Run Code Online (Sandbox Code Playgroud)
如何重构上面的代码?
可能重复:
Ruby拒绝正确划分
我正在使用Ruby on Rails 3.2.2,当我尝试计算时,10 * (50 / 100)我回来了0但当我让10 * 50 / 100我回来时5.
由于技术原因,我不能使用后者,因为我的代码如下:
perc = 50 /100
# Some code that uses the 'perc' variable
... = method_name(perc)
return 10 * perc
Run Code Online (Sandbox Code Playgroud)
我应该做些什么来解决问题并且5(没有 5.0或其他东西;也就是说,返回是一个Integer)?
ruby ×9
regex ×2
string ×2
ajax ×1
comparison ×1
exception ×1
function ×1
integer ×1
javascript ×1
jquery ×1
numbers ×1
refactoring ×1
rescue ×1
scope ×1
validation ×1
yard ×1