ruby或rails是否提供了按指定顺序排序字符串的方法?假设我有以下优先级"严重,高,中,低".
这些优先事项不会经常变化(如果有的话).我有一个优先级列的任务模型:
tasks
- id (integer)
- name (string)
- priority (string)
Run Code Online (Sandbox Code Playgroud)
我想获得按优先级排序的所有任务的数组.由于逻辑顺序不遵循字母顺序,因此无法按优先级顺序排序:
Task.all(:order => :priority)
Run Code Online (Sandbox Code Playgroud)
我所做的是创建优先级模型并定义关联:任务belongs_to优先级.在优先级表中,然后我为每个优先级名称分配了一个值并按该值排序.有一个更好的方法吗?我宁愿没有优先级表并声明PRIORITY常量(作为哈希),或者只是在task表中将优先级指定为字符串.
我有一个Category和一个Post模型,每个Post都属于一个Category.在创建或更新帖子之前,我需要检查所选的类别是否存在.验证此信息的最佳方法是什么?
目前,我正在控制器中进行查找,以确保该类别存在.是否可以在模型中加入这些验证?
是否可以将子域映射到资源?我有一个公司模特.目前,使用subdomain_fu,我的路由文件包含:
map.company_root '', :controller => 'companies', :action => 'show',
:conditions => { :subdomain => /.+/ }
Run Code Online (Sandbox Code Playgroud)
我的公司模型包含"子域"列.
虽然这是按预期工作的,但它是一条命名的路线而且并不安宁.基本上,我需要将"name.domain.com"映射到公司控制器的show动作.命名路由是可行的,还是可以使用资源路由?
我一直在尝试实现阴影贴图.虽然我认为我现在已经接近了,但我已经陷入了一种奇怪的效果(如下图所示):

如您所见,阴影区域显得太小.对立方体本身也有不寻常的影响.
正在渲染的几何体是尺寸为100.0的方形平面上的尺寸为1.0的立方体.场景包含一个聚光灯,其角度(从一侧到另一侧)为0.5弧度,范围为100.0.该聚光灯围绕y轴旋转并调整其旋转以查看原点.
我设置了帧缓冲和深度纹理(512 x 512),如下所示:
// Create and configure the depth texture.
glGenTextures(1, &m_depthTexture);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
GLfloat border[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (void*)0);
// Assign the depth texture to texture channel 0.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
// Create and configure the framebuffer.
glGenFramebuffers(1, &m_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, …Run Code Online (Sandbox Code Playgroud) 我有一个EventDispatcher实现发布 - 订阅模式的类.它的界面看起来像这样(简化):
class EventDispatcher
{
public:
void publish(const std::string& event_name, std::unique_ptr<Event> event);
std::unique_ptr<Subscription> subscribe(const std::string& event_name, std::unique_ptr<Callback> callback);
private:
std::unordered_map<std::string, std::vector<std::unique_ptr<Callback>>> m_subscriptions;
}
Run Code Online (Sandbox Code Playgroud)
我想将这个类暴露给Python.最新的SWIG文档指出:
std :: weak_ptr和std :: unique_ptr还没有特殊的智能指针处理.
我非常希望至少能够在c ++方面继续使用unique_ptr.我有什么选择?
我考虑使用SWIG的%扩展功能扩展类,但我无法m_subscriptions使用此方法访问私有成员().
唯一的其他选择,我可以看到的是使用SWIG预处理器定义额外的方法,swig_publish并且swig_subscribe,但这杂波我的接口文件.
我的计划是创建一个加载线程,我在其中加载游戏资源; 例如3D模型,着色器,纹理等.在主线程上,我执行所有游戏逻辑和渲染.然后,在我的加载线程上,我创建了一个仅用于加载的sf :: Context(SFML共享OpenGL上下文).
这适用于加载着色器.但是,xserver在尝试加载模型时有时会崩溃.我已经将崩溃缩小到glBufferData()调用.我已经检查过我发送的数据没有问题.
是否可以使用第二个OpenGL上下文从第二个线程调用glBufferData()?如果没有,为什么可以在第二个上下文中加载着色器?如果有可能,会出现什么问题?
#include <iostream>
#include <thread>
#include <GL/glew.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <X11/Xlib.h>
class ResourceLoader
{
public:
void Run()
{
sf::Context loadingContext;
loadingContext.setActive(true);
// Some test data.
float* testData = new float[3000];
for (unsigned int i = 0; i < 3000; ++i)
{
testData[i] = 0.0f;
}
// Create lots of VBOs containing our
// test data.
for (unsigned int i = 0; i < 1000; ++i)
{
// Create VBO.
GLuint testVBO = 0; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将静态库(GLFW)链接到我正在构建的库中.我在我的CMakeLists.txt文件中有以下内容,以便执行此操作:
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${LIBRARY_NAME} ${GLFW_STATIC_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
链接我的库时,我收到以下错误: ld: library not found for -lglfw3
然而,pkg-config --libs glfw3在控制台中运行会给出:
-L/usr/local/lib -lglfw3
Run Code Online (Sandbox Code Playgroud)
所以我知道安装了GLFW库.当我尝试使用CMake进行链接时,为什么找不到库?
我很好奇为什么unordered_set平均随机访问具有恒定时间复杂度的STL容器没有提供一种方法来访问容器中距离第一个元素一定距离的元素.例如:
T& unordered_set::operator[](size_t index)
{
return *(begin() + index);
}
Run Code Online (Sandbox Code Playgroud) 我有以下使用SWIG暴露给Python的c ++类(简化):
struct Component
{
virtual void update();
}
struct DerivedComponent : public Component
{
void update() { cout << "DerivedComponent::update()" << endl; }
void speak() { cout << "DerivedComponent::speak()" << endl; }
}
class Entity
{
public:
Component* component(const std::string& class_name)
{
return m_components[class_name];
}
private:
std::unordered_map<std::string, Component*> m_components;
}
Run Code Online (Sandbox Code Playgroud)
现在,在Python中,我可以成功调用component("DerivedComponent").update()Entity实例。但是,component("DerivedComponent").speak()由于返回的类型component("DerivedComponent")报告为,因此我无法调用<class 'module.Component'>。
component()为了调用中定义的方法,我显然需要转换函数的结果DerivedComponent。我曾希望Swig能像我相信Boost.Python一样执行自动向下转换。
缺少在c ++中定义一堆类型转换函数并将其公开给Python的方法,是否有使用Swig或Python进行向下转换的更好的解决方案?我有什么选择?