我有一个网络应用程序,大多数情况下使用MRI Ruby和JRuby.但...
当我使用MRI时,我可以做到这一点.
> rails console
irb(main):001:0> a=Time.new(1,1,1,1,1)
=> 0001-01-01 01:01:00 -0500
Run Code Online (Sandbox Code Playgroud)
即它在指定时间内创建一个新的Time对象.
当我切换到JRuby时,我这样做
> rails console
irb(main):001:0> a=Time.new(1,1,1,1,1)
ArgumentError: wrong number of arguments (5 for 0)
from (irb):2: in `evaluate`
from org/jruby/RubyKernel.java:1088 in `eval`
from /home/user/.rbenv/versions/jruby-1.6.4/lib/ruby/1.8/irb.rb:158 in `eval_input`
from /home/user/.rbenv/versions/jruby-1.6.4/lib/ruby/1.8/
....
Run Code Online (Sandbox Code Playgroud)
如何在JRuby中创建一个Time对象,其中包含我控制的年,月,日,小时和分钟?如果是这样,这可以用于MRI Ruby吗?
我不知道这是否相关,但是
irb(main):002:0> RUBY_VERSION
"1.8.7"
Run Code Online (Sandbox Code Playgroud)
我的问题可能是因为我处于1.8.7模式吗?如果是这样,如何将我的rbenv/jruby-1.6.4安装切换为RUBY_VERSION = 1.9.2?
我想从一个方法ruby中获取true如果每个帖子都被人们跟踪,如果没有,则为false .
我有这个方法:
def number_of_posts_that_are_followed
user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
user_to_be_followed.posts.each do |this_post|
if current_user.follows?(this_board) == true #method that returns true if the current_user is following this post of the user whose posts will be followed
return true
else
return false
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是如果第一个帖子(在第一次迭代中)后跟current_user,则此方法返回true.如果每个帖子都被跟踪,我想要返回true,否则返回false.
我试过把这样的计数:
count = user_to_be_followed.posts.count
Run Code Online (Sandbox Code Playgroud) 有没有办法在一个类中使用宇宙飞船方法和神奇的"method_missing"?undefined method '<'
每当我尝试foo1 < foo2
使用以下定义时,下面的示例都会给出:
class Foo
def initialize(params)
@parent= params[:parent]
end
def <=>(o)
...
end
def method_missing(sym, *args, &block)
@parent.send sym, *args, &block
end
end
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏:)
什么是在Ruby中实现这一目标的简洁方法?我只是想等到所有对象都在运行.这似乎太罗嗦了.
# arr contains objects that respond to running?
all_running = false
until all_running
sleep 0.5
all_running = true
arr.each{ |obj|
all_running = all_running and obj.running?
}
end
Run Code Online (Sandbox Code Playgroud) 我已经决定摆脱rvm,我在使用新版本的ruby 1.9.2编译gem时遇到了麻烦.宝石需要1.9.2,我有它,但说不能没有安装,所以错误消息没有意义.
如何明确告诉gem使用所述版本的ruby进行编译?
Gem::InstallError: linecache19 requires Ruby version >= 1.9.2.
An error occured while installing linecache19 (0.5.12), and Bundler cannot continue.
Make sure that `gem install linecache19 -v '0.5.12'` succeeds before bundling.
apps2 ~/projects/sms/apps2/apps2_admin $ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.8.0]
apps2 ~/projects/sms/apps2/apps2_admin $ which ruby
/usr/local/bin/ruby
Run Code Online (Sandbox Code Playgroud) 考虑一些功能foo
:
def foo(input)
input * 2
end
Run Code Online (Sandbox Code Playgroud)
如何获取某些数组的输入的最大值a
?
a = [3, 5, 7, 9, 6]
Run Code Online (Sandbox Code Playgroud)
类似下面的内容(不起作用)应该返回9:
a.max do |value|
foo(value)
end
Run Code Online (Sandbox Code Playgroud)
怎么做?
Ruby 1.9.2
ruby-1.9.2 ×6
ruby ×5
block ×1
count ×1
each ×1
enumeration ×1
enumerator ×1
env ×1
environment ×1
gem ×1
jruby ×1