将 Ruby 1.9.2 与 RubyMine 和 Matrix 结合使用
我正在使用 ruby 1.9.2-p290 和 RubyMine。我尝试使用矩阵(需要“矩阵”)。所以,我有几个问题。
- 我怎样才能改变矩阵的任何值?
例如:
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 中通过代码补全向我展示矩阵的方法?
- 我应该使用什么库来存储矩阵?矩阵?马特恩?还有别的事吗?
I am using ruby 1.9.2-p290 and RubyMine. And i try to use Matrix (require 'matrix'). So, i have few questions.
- How can i change any value of matrix?
For example:
require 'matrix'
matrix = Matrix[[1, -2, 3], [3, 4, -5], [2, 4, 1]]
matrix[0, 0] = 5
p matrix
Gives next:
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>'
- Is it possible to show me methods for matrix by code completion in RubyMine IDE?
- What library(s) should i use for matrices? Matrix? Mathn? Something else?
广告 1)我知道文档说
[]=
是一个公共实例方法,1.9.2 中的现实似乎与此不符:我看到了两种解决方法。第一个是使用
send
绕过private
:第二个是遍历数组:
如果您确实想要,您可以更改方法的可见性:
请注意,我不这样做不鼓励任何这些,类的实现方式强烈暗示作者认为矩阵是不可变的对象。
广告 2) 不幸的是,我不知道 RubyMine,但是可以找到
Matrix
类的文档 此处。Ad 3) 我还没有在 Ruby 中广泛使用矩阵,但是对于我所需要的,
Matrix
类就足够了。Ad 1) I know the documentation says that
[]=
is a public instance method, reality in 1.9.2 does not seem to match that:I see two ways around this. The first is using
send
to bypassprivate
:The second is going through an array:
If you really wanted to, you could change the visibility of the method:
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.
Ad 2) I don't know RubyMine unfortunately, but the documentation for the
Matrix
class can be found here.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.