我有问题四舍五入.我有一个浮点数,我想要舍入到十进制的十分之一.但是,我只能使用.round哪个基本上把它变成一个int,意思2.34.round # => 2. 是有一种简单的效果方式来做类似的事情2.3465 # => 2.35
我理解这个概念,some_instance.send但我想弄清楚为什么你可以这两种方式调用它.Ruby Koans暗示除了提供许多不同的方法来做同样的事情之外还有一些原因.以下是两个用法示例:
class Foo
def bar?
true
end
end
foo = Foo.new
foo.send(:bar?)
foo.__send__(:bar?)
Run Code Online (Sandbox Code Playgroud)
有人对此有任何想法吗?
我的应用程序中有字段hr和min整数.对于hr字段,如果用户输入"1",我希望Rails在将其保存到数据库之前自动将其填充为"01".对于该min字段,如果用户输入"0",则应输入"00".
我怎样才能做到这一点?
我正在尝试使用以下代码创建一个目录:
Dir.mkdir("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")
unless File.exists?("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
没有这样的文件或目录 - /用户/路易吉/桌面/ Survey_Final /归档/调查/测试(错误:: ENOENT)
为什么Dir.mkdir上面的语句没有创建此目录?
列出ruby版本
console:~$ rvm list
rvm rubies
ruby-2.0.0-p481 [ i686 ]
# => - current
# =* - current && default
# * - default
Run Code Online (Sandbox Code Playgroud)
尝试使用特定版本的ruby
console:~$ rvm use 2.0.0
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
Run Code Online (Sandbox Code Playgroud) 我刚刚将我的Mac更新为OSX 10.11 El Capitan并且没有留下一些宝石.
当我试图运行不同宝石的安装时出现问题...例如:
> sudo gem install compass
Run Code Online (Sandbox Code Playgroud)
一旦我执行,我得到错误:
> ERROR: While executing gem ... (Errno::EPERM) Operation not permitted
Run Code Online (Sandbox Code Playgroud)
其他宝石也会发生这种情况......
我已经运行Homebrew并尝试再次安装Ruby,但没有任何工作.
我有一个数组,其每个元素都是一个带有三个键/值对的哈希:
:phone => "2130001111", :zip => "12345", :city => "sometown"
Run Code Online (Sandbox Code Playgroud)
我想对数据进行排序,zip因此phone同一区域中的所有s都在一起.Ruby有一个简单的方法吗?可以will_paginate在数组中分页数据吗?
我如何计算这两个日期之间的天数?
start_date = Date.parse "2012-03-02 14:46:21 +0100"
end_date = Date.parse "2012-04-02 14:46:21 +0200"
Run Code Online (Sandbox Code Playgroud)