如果我想require在Ruby中使用相关文件并且希望它在1.8.x和> = 1.9.2中工作,那么最佳实践是什么?
我看到几个选项:
$LOAD_PATH << '.'忘记一切$LOAD_PATH << File.dirname(__FILE__)require './path/to/file'RUBY_VERSION<1.9.2,然后定义require_relative为require,require_relative随后在需要的地方使用require_relative已存在,如果已存在,请尝试按上一种情况继续操作require File.join(File.dirname(__FILE__), 'path/to/file')
Run Code Online (Sandbox Code Playgroud)它们似乎不能在Ruby 1.9中工作,因为,例如:$ cat caller.rb
require File.join(File.dirname(__FILE__), 'path/to/file')
$ cat path/to/file.rb
puts 'Some testing'
$ ruby caller
Some testing
$ pwd
/tmp
$ ruby /tmp/caller
Some testing
$ ruby tmp/caller
tmp/caller.rb:1:in 'require': no such file to load -- tmp/path/to/file (LoadError)
from tmp/caller.rb:1:in '<main>' …Run Code Online (Sandbox Code Playgroud)Ruby中有没有办法在方法中找到调用方法名?
例如:
class Test
def self.foo
Fooz.bar
end
end
class Fooz
def self.bar
# get Test.foo or foo
end
end
Run Code Online (Sandbox Code Playgroud) 我有几个gem文件,我通过它安装gem install xx.gem.我可以告诉Bundler使用它们吗?或者我是否必须指定源路径?
更新RMagick和Imagemagick是一个痛苦的期待.我用自制软件更新了Mac上的Imagemagick版本(MacOS El Capitan版本10.11.5),用于Ruby 2.3中的一个项目6.9.5-9
$ convert --version
Version: ImageMagick 6.9.5-9 Q16 x86_64 2016-09-09
Run Code Online (Sandbox Code Playgroud)
现在Ruby 1.8.7中的一个旧项目拒绝使用错误消息"这个RMagick的安装配置了ImageMagick 6.8.9但ImageMagick 6.9.5-9正在使用中".因此我卸载了"rmagick",但无法重新安装
$ gem install rmagick -v 2.16.0
Building native extensions. This could take a while...
ERROR: Error installing rmagick:
ERROR: Failed to build gem native extension.
checking for /usr/local/opt/gcc46/bin/gcc-4.6... yes
checking for Magick-config... yes
checking for outdated ImageMagick version (<= 6.4.9)... no
checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no
checking for Ruby version …Run Code Online (Sandbox Code Playgroud) 当我得到异常时,它通常来自调用堆栈中的深层.当发生这种情况时,通常会对我隐藏实际有问题的代码行:
tmp.rb:7:in `t': undefined method `bar' for nil:NilClass (NoMethodError)
from tmp.rb:10:in `s'
from tmp.rb:13:in `r'
from tmp.rb:16:in `q'
from tmp.rb:19:in `p'
from tmp.rb:22:in `o'
from tmp.rb:25:in `n'
from tmp.rb:28:in `m'
from tmp.rb:31:in `l'
... 8 levels...
from tmp.rb:58:in `c'
from tmp.rb:61:in `b'
from tmp.rb:64:in `a'
from tmp.rb:67
Run Code Online (Sandbox Code Playgroud)
那个"... 8级......"截断给我带来了很多麻烦.我在这方面没有太大的成功:我怎么告诉ruby我想要转储包含完整的堆栈?
我怎么能在这里做他们正在谈论的事情,但是在Ruby中呢?
你会如何对对象执行此功能?你将如何做一个全局函数(参见jetxee 在上述帖子中的回答)?
示例代码:
event_name = "load"
def load()
puts "load() function was executed."
end
def row_changed()
puts "row_changed() function was executed."
end
#something here to see that event_name = "load" and run load()
Run Code Online (Sandbox Code Playgroud)
更新: 你如何得到全球方法?还是我的全球职能?
我试过这个额外的一行
puts methods
Run Code Online (Sandbox Code Playgroud)
和load和row_change未列出的地方.
我有一个计数器哈希,我试图按计数排序.我遇到的问题是默认的Hash.sort函数将数字排序为字符串而不是数字大小.
即鉴于哈希:
metrics = {"sitea.com" => 745, "siteb.com" => 9, "sitec.com" => 10 }
Run Code Online (Sandbox Code Playgroud)
运行此代码:
metrics.sort {|a1,a2| a2[1]<=>a1[1]}
Run Code Online (Sandbox Code Playgroud)
将返回一个已排序的数组:
[ 'siteb.com', 9, 'sitea.com', 745, 'sitec.com', 10]
Run Code Online (Sandbox Code Playgroud)
尽管745的数字大于9,但9会出现在列表的第一位.当试图表明谁拥有最高统计数时,这让我的生活变得困难.:)
关于如何按数值大小排序哈希(或数组偶数)的任何想法?
我感谢任何帮助.
我们做得到:
i = Time.now.to_i
Run Code Online (Sandbox Code Playgroud)
例如当前:
i = 1274335854
Run Code Online (Sandbox Code Playgroud)
我可以把我转换回来吗?
我想在铁轨上做这样的事情
以下是我目前在rails中的内容:
<%= form_for @order do |f| %>
<%= f.hidden_field :service, "test" %>
<%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但后来我得到了这个错误:
undefined method `merge' for "test":String
Run Code Online (Sandbox Code Playgroud)
如何在rails中的hidden_field中传递值?
ruby ×10
bundler ×1
exception ×1
forms ×1
hash ×1
hidden-field ×1
imagemagick ×1
macos ×1
macos-sierra ×1
rmagick ×1
rspec ×1
ruby-1.8 ×1
ruby-1.9 ×1
rubygems ×1
stack-trace ×1
time ×1