我需要一种方法将数组拆分为另一个大小相等的数组中的一堆数组.有人有任何方法吗?
例如
a = [0, 1, 2, 3, 4, 5, 6, 7]
a.method_i_need(3)
a.inspect
=> [[0,1,2], [3,4,5], [6,7]]
Run Code Online (Sandbox Code Playgroud) 当ruby中的nil为零时,是否存在将事物分配给哈希的简短手或最佳实践?例如,我的问题是我正在使用另一个哈希来构建它,如果其中的某些内容是nil,它会为该键分配nil,而不是仅仅将其保留.我理解为什么会这样,所以我的解决方案是:
hash1[:key] = hash2[:key] unless hash2[:key].nil?
Run Code Online (Sandbox Code Playgroud)
因为我不能在键中实际指向nil的值中有值.(我宁愿有一个空哈希比一个有{:key => nil}的哈希,这是不可能的)
我的问题是有更好的方法吗?我不想在作业结束时执行delete_if.
我有一个非常令人困惑的数据库,其中一个表在一个单独的表中包含我需要的两个值.这是我的问题:
Table1
- id
Table2
- id
- table1_id
- table3_id_1
- table3_id_2
Table3
- id
- value
Run Code Online (Sandbox Code Playgroud)
我需要从table1开始并进行一个连接,它会从table3两个单独的列中返回值.所以我想要这样的东西:
table1.id | table2.id | table2.table3_id_1 | table2.table3_id_2 | X | Y
Run Code Online (Sandbox Code Playgroud)
其中X和Y是由连接该行的值table3_id_1和table3_id_2分别.
可能使它们成为变量或其他东西,所以我也可以在WHERE子句中过滤它们?
所以我有三个表涉及我的问题,2个常规表和一个连接表有一个很多,属于很多关系.它们看起来像这样:
table1
--id
--data
table2
--id
--data
table1_table2
--table1_id
--table2_id
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是我如何查询(使用连接)表格中的一个项目中table1_table2中有一个或多个值的内容.例如:
Table 1
+----------+
|id | data |
+----------+
|1 | none |
+----------+
|4 | match|
+----------+
Table 2
+----------+
|id | data |
+----------+
|1 | one |
+----------+
|2 | two |
+----------+
table1_table2
+----------------------+
|table1_id | table2_id |
+----------------------+
|1 | 1 |
+----------------------+
|4 | 1 |
+----------------------+
|4 | 2 |
+----------------------+
Run Code Online (Sandbox Code Playgroud)
我需要一个与表1行id 4相匹配的查询,因为它通过连接有一个链接到表2中的第1行和第2行.如果这很令人困惑,请问任何问题.
也许我有点不清楚,我使用table1_table2作为连接而不是来自.我需要确保它与表2中的1和2相匹配.
这是我到目前为止的查询...
SELECT DISTINCT table1.id,
table2.data
FROM table1
LEFT JOIN …Run Code Online (Sandbox Code Playgroud) 所以,我使用SceneKit来渲染一组参数曲面(其总和构成一个对象).为了将这些放在屏幕上,我通过采样点和创建三角形来创建自定义几何.这是我如何做到这一点的快速视图.
Loop through the collection of surfaces
Generate a random color C
For each surface calculate a grid of N x N points (both positions and normals)
Assign all vertexes for that surface the color C
Add groups of 3 vertexes from this surface to the face index list
Run Code Online (Sandbox Code Playgroud)
这似乎有效.在我获得所有这些数据后,我将其转换为正确的结构(SCNGeometrySource和SCNGeometryElement)并制作SCNGeometry,如此
SCNGeometry(sources: [vertexSource, normalSource, colorSource], elements: [element])
Run Code Online (Sandbox Code Playgroud)
这样可以在屏幕上显示我的曲面作为单个几何元素.我的问题是,我有一些非常复杂的对象,我正在尝试使用它,并且在查看对象时移动相机的速度非常慢.渲染大约需要500毫秒.这使我的帧率和体验变得糟糕.
所以问题是,我可以采取哪些步骤来加速SceneKit的性能?我使用具有相同数据量的Three.js的WebGL做了同样的项目,并且能够使用轨道相机很好,所以我无法相信场景套件至少无法与之竞争.我可以调整和关闭哪些功能以加快性能?我使用的是三角形基本类型,轨道相机的allowsCameraControl = true,以及SCNView的金属.
对于那些好奇的人,我正在努力的模型为面部生成231,900个顶点和347,850个索引(11.1312 MB的顶点数据(位置和法线)和1.3914 MB的面部数据(基本上只是顶点的索引位置以便三角形.))
在Mac OS X Lion中访问内置于MacBook/MacBook Pro和iMac中的FaceTime/iSight摄像头的正确方法是什么?我不得不想象有一种方法可以在使用Xcode之外访问它.我需要哪些库来提供给g ++以及如何编译它?我知道人们已经这样做了,我只是不确定我会怎么做.
我刚开始尝试使用OpenCV,我从一本非常简单的书中写了一个小程序.问题是当我尝试编译它时,我得到了这个错误.我会告诉你我的所有信息.我使用自制软件为Mac OS X 10.7安装了openCV.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread("usf.gif");
cv::namedWindow("My Image");
cv::imshow("My Image", image);
cv::waitKey(5000);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我编译喜欢这个:
g++ -o test opencvtest.cc -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy
Run Code Online (Sandbox Code Playgroud)
这是我试图运行它时得到的.
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /tmp/homebrew-opencv-2.4.2-oQmu/OpenCV-2.4.2/modules/core/src/array.cpp, line 2482
terminate called throwing an exceptionAbort trap: 6
Run Code Online (Sandbox Code Playgroud)
再次感谢.
我确信这是一个简单的问题,但我很难完成任务.我正在使用http://www.fftw.org/fftw2_doc/fftw_2.html上的代码片段.我安装了库(通过自制程序).include文件夹(/ usr/local/include)有
fftw3.f
fftw3.f03
fftw3.h
fftw3l.f03
fftw3q.f03
Run Code Online (Sandbox Code Playgroud)
以下是该网站的代码段.我用fttw.h和fttw3.h都试过了
#include <fftw.h>
int main (int argc, char** argv){
fftw_complex in[N], out[N];
fftw_plan p;
p = fftw_create_plan(N, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_one(p, in, out);
fftw_destroy_plan(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它一直在投掷
fftwtest.c:1:10: fatal error: 'fftw3.h' file not found
#include <fftw3.h>
^
1 error generated.
Run Code Online (Sandbox Code Playgroud) 所以,我在嵌入式文档中遇到了一些用户身份验证问题.我有两个文件,一个嵌入另一个.一个企业有很多成员.模型看起来像这样:
class Member
include Mongoid::Document
field :username, type: String
field :password, type: String
embedded_in :business
validates :username, :presence => true, :uniqueness => true, :length => 5..60
end
class Business
include Mongoid::Document
field :name, type: String
embeds_many :members
end
Run Code Online (Sandbox Code Playgroud)
问题是它没有验证每个模型中用户名的唯一性.当我在一个企业中保存一个成员时,我可以保存一千个相同的名字.这当然不适用于良好的身份验证系统.我使用的是Mongoid 2,Rails 3和Ruby 1.9
当我在这个程序上运行make我正在尝试构建时,我收到这些警告:
ld: warning: ignoring file ../lib/libiptools.a, file was built for archive which is not the architecture being linked (x86_64)
ld: warning: ignoring file ../lib/libmpeg.a, file was built for archive which is not the architecture being linked (x86_64)
Run Code Online (Sandbox Code Playgroud)
然后它显然无法编译:
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [iptool] Error 1
Run Code Online (Sandbox Code Playgroud)
我只是很难弄清楚如何改变我的make文件并正确编译,因为我不熟悉档案.任何建议都值得欣赏,因为我确信我错过了一些简单的东西.我正在使用Mac OS X Mountain Lion.
我有三件不同的事情
xxx
xxx>xxx
xxx>xxx>xxx
Run Code Online (Sandbox Code Playgroud)
其中xxx可以是字母和数字的任意组合
我需要一个可以匹配前两个而不是第三个的正则表达式.