将Ruby 1.9.2与RubyMine和Matrix一起使用 [英] Using Ruby 1.9.2 with RubyMine and Matrix
问题描述
我正在使用ruby 1.9.2-p290和RubyMine.而且我尝试使用矩阵(需要矩阵").所以,我有几个问题.
I am using ruby 1.9.2-p290 and RubyMine. And i try to use Matrix (require 'matrix'). So, i have few questions.
- 如何更改矩阵的任何值?
例如:
require 'matrix'
matrix = Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]
matrix[0, 0] = 5
p matrix
下一个:
in `<top (required)>': private method `[]=' called for Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]:Matrix (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
- 是否可以通过RubyMine IDE中的代码完成向我展示矩阵方法?
- 我应该将哪些库用于矩阵?矩阵?玛特恩?还有别的吗
推荐答案
Ad 1)我知道文档说[]=
是一个公共实例方法,1.9.2中的现实似乎与之不匹配:
Ad 1) I know the documentation says that []=
is a public instance method, reality in 1.9.2 does not seem to match that:
matrix.private_methods.grep(/\[\]/) #=> [:[]=]
我看到了两种解决方法.第一种是使用send
绕过private
:
I see two ways around this. The first is using send
to bypass private
:
matrix.send(:[]=, 0, 0, 5) #=> 5
第二个是通过数组:
m = *matrix
m[0][0] = 5
matrix = Matrix[*m]
如果您确实愿意,可以更改该方法的可见性:
If you really wanted to, you could change the visibility of the method:
matrix.class.class_eval { public :[]= }
请注意,我不鼓励使用这些方法,该类的实现方式强烈暗示了作者将矩阵视为不可变的对象.
Note that I don't encourage any of these, the way the class is implemented is a strong hint that the authors consider matrices to be immutable objects.
广告2)不幸的是,我并不了解RubyMine,但是可以找到Matrix
类的文档
Ad 2) I don't know RubyMine unfortunately, but the documentation for the Matrix
class can be found here.
广告3)我还没有在Ruby中广泛使用矩阵,但是对于我需要的它们,Matrix
类已经足够了.
Ad 3) I haven't had an extensive use for matrices in Ruby yet, but for what I needed them the Matrix
class was good enough.
这篇关于将Ruby 1.9.2与RubyMine和Matrix一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!