我有一个模型方法,我想从各种控制器调用.它看起来像这样:
def Post < ActiveRecord::Base
def read!
self.read_at = Time.now
self.save
self.thread.status = Status.find_by_name("read")
self.thread.save
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,如果我打电话@post.read!,这会回滚任何错误吗?
我有一个numpy数组,我想强制每个小于零的元素为零,并且高于255的每个元素将被强制降低到255.
例如.x =(-1,7,255,299)=>(0,7,255,255)
是否有一个不太复杂的单行程可以实现这一目标?
我遇到了遍历Hash的这个函数的问题.哈希可能包含一个哈希数组.我希望该方法搜索一个id,然后只返回它找到的嵌套哈希.
它似乎适用于遍历,但它返回传入的原始值.
require 'rubygems'
require 'ruby-debug'
def find_by_id(node, find_this="")
if node.is_a?(Hash)
node.each do |k,v|
if v.is_a?(Array)
v.each do |elm|
if elm["_id"] == find_this && !find_this.empty?
return elm # THIS IS WHAT I WANT!
else
find_by_id(elm, find_this)
end
end
end
end
end
end
x = {"name" => "first", "_id"=>'4c96a9a56f831b0eb9000005', "items"=>["name" => "second", "_id"=>'4c96a9af6f831b0eb9000009', "others"=>[{"name" => "third", "_id"=>'4c96a9af6f831b0eb9000007'}, {"name" => "fourth", "_id"=>'4c96a9af6f831b0eb9000008'}] ] }
find_by_id(x, '4c96a9af6f831b0eb9000008')
Run Code Online (Sandbox Code Playgroud) 我的输入纹理是852x640,输出纹理是612x612.我通过着色器传递输入,并希望缩放输出并正确裁剪.我遇到麻烦squareCoordinates,textureCoordinates并viewPorts以适当的共同努力.
我不想只裁剪,我想缩放它以获得尽可能多的图像.如果我使用Photoshop,我会分两步完成这个步骤(在OpenGL中,我试图一步完成):
我正在使用标准的方形顶点和纹理顶点:
static const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat squareTextureVertices[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
}
Run Code Online (Sandbox Code Playgroud)
我不确切知道viewPort应该是什么.
我有两个类,任务和子任务。子任务与任务几乎没有什么不同,除了一件重要的事情之外,它必须包含不同的模块。
出于我的目的,包含的模块 subtask_module 和 task_module 都具有相同的方法和别名,但一旦包含的模块扩展其方法,它们在内部的功能就会略有不同。对我来说没有办法解决这个问题,因为我正在使用插件。
例如,您在下面的belongs_to任务中看到。belongs_to是从包含的模块扩展而来的,但是它的功能根据我包含的模块而略有不同。
class Subtask < Task
include subtask_module
end
class Task
include task_module
# methods and aliases both classes use (defined in included file)
# but behavior changes based on
# included file
belongs_to :template
end
Run Code Online (Sandbox Code Playgroud)
做这样的事情最好的方法是什么?现在它就像现在一样工作。但它看起来很臃肿,因为任务中会声明一些我不需要的未使用的东西。
什么是最好的方法?
我正在尝试编写一个Bash脚本来转换一堆文件.
假设我有一个目录/path/to/my files/有三个文本文件:a b.txt,c d.txt和e.txt(注意空格)
我需要能够像这样调用脚本:
$ ./myscript.sh /path/to/my\ files/*.txt
Run Code Online (Sandbox Code Playgroud)
然后在bash中循环遍历它们,像这样处理它们:
dest='/desktop/'
for ARG in $@; do
/some/other/script $ARG $dest$ARG.new
done
Run Code Online (Sandbox Code Playgroud)
它不必像这样工作,无论什么最容易产生类似的结果
我正在重新创建一些 Photoshop 混合,我正在尝试使用线性光模式。在 Photoshop 中,你有一个 100% 不透明度的背景层,然后是一个 50% 不透明度的顶层,它设置为线性光作为混合模式。
我确实找到了有关如何进行线性光混合的信息,但它仅在两个图层都处于 100% 不透明度时才有效。
这是执行线性光模式的着色器代码,当图层都处于 100% 不透明度时,它会给出与 Photoshop 相同的结果:
#define BlendLinearDodgef BlendAddf
#define BlendLinearBurnf BlendSubstractf
#define BlendAddf(base, blend) min(base + blend, 1.0)
#define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0)
#define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
Run Code Online (Sandbox Code Playgroud)
我查看了http://en.wikipedia.org/wiki/Alpha_compositing,但仍有问题。
如何让混合模式适用于半透明图层?
我试图覆盖两个表单元素的默认表单样式,以便文本框和按钮都是相同的高度并排并排,这样它们看起来像是一个元素.
在某些浏览器中它看起来很好但在某些浏览器中它们是一个或两个垂直的像素.这是一个jsfiddle演示.OS X上的Opera和Firefox给了我一些问题.
*, *:before, *:after {-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;}
input[type="text"] {
background-color: #fafafa;
padding: 7px;
font-family: helvetica, arial, sans-serif;
font-size: 1.2em;
margin-bottom: 20px;
display: block;
border: solid 2px #bbb;
color: #6f6f6f;
}
input[type="submit"] {
background-color: #fafafa;
font-family: helvetica, arial, sans-serif;
font-size: 1.0em;
font-weight:bold;
padding: 7px;
color: #6f6f6f;
}
input[type="submit"]:hover {
background-color: #ff379f;
color: #fafafa;
}
#subscription-email-text-field {
display:inline-block;
font-size:0.9em;
font-weight:400;
border:0;
width:250px;
height:32px;
margin:0;
}
#subscribe-button {
display:inline-block;
border-width:0px 0px 0px 1px;
margin:0;
height:32px;
}
<div …Run Code Online (Sandbox Code Playgroud) 我正在尝试这样做,以便我可以有这样的网址:
/events
/events/sunday # => The day is optional
Run Code Online (Sandbox Code Playgroud)
然而,即使我知道它被调用,它似乎也没有起作用.它位于我的路线文件的底部.
match '/:post(/:day_filter)' => 'posts#index', :as => post_day_filter, :constraints => DayFilter.new
class DayFilter
def initialize
@days = %w[all today tomorrow sunday monday tuesday wednesday thursday friday saturday]
end
def matches?(request)
return @days.include?(request.params[:day_filter]) if request.params[:day_filter]
true
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的佣金路线输出:
post_day_filter /:post(/:day_filter)(.:format) {:controller=>"posts", :action=>"index"}
Run Code Online (Sandbox Code Playgroud) class Bear < ActiveRecord::Base
def feed!
self.transaction do
raise Exception unless self.foods_eaten << Food.new(:name => "fish")
self.fed_at = Time.now
save!
end
end
end
class Hippo < ActiveRecord::Base
def wash!
self.transaction do
@soap.inventory -= 1
@soap.save!
self.washed_at = Time.now
save!
end
end
end
class ZookeeperController < ApplicationController
def chores
@zookeeper = Zookeeper.find(params[:id])
Animal.transaction do
begin
@hippo.wash!
@bear.feed! # => FAIL AT THIS LINE
@zookeeper.finished_at = Time.now
@zookeeper.save!
redirect_to chores_completed_path
rescue Exception => e
render "new_chores"
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果Zookeeper#chores …
ruby ×2
transactions ×2
activerecord ×1
bash ×1
command-line ×1
constraints ×1
css ×1
graphics ×1
loops ×1
numpy ×1
opengl ×1
opengl-es ×1
python ×1
routes ×1
shader ×1
textures ×1
wildcard ×1