我有一个非常重复的条件句.我想知道是否可以通过一些元编程来清理.
这是我正在处理的一个简化示例:
FILTERS = [
:filter1,
:filter2,
:filter3
]
def filter1; true; end
def filter2; true; end
def filter3; true; end
if(
send( FILTERS[0] ) &&
send( FILTERS[1] ) &&
send( FILTERS[2] )
)
puts "DONE!"
end
Run Code Online (Sandbox Code Playgroud)
(在我的实例中,FILTERS数组包含27个元素)
目标是通过所有过滤方法进行某种自动迭代,将三行替换为if句子.
另一个重要的比赛是保持快速出的行为&&命令:如果filter1是false没有filter2或filter3将被执行.
该FILTERS阵列只是帮助您找到优雅的解决方案,您不必使用它.
我已经准备好部署我的Rails应用程序,并且我想包装所有依赖项,包括nginx作为前端Web服务器,mysql数据库和cron执行循环任务。
使用新的 InputSystem 时。如果我将 Canvas 组件添加到我的场景中,它会自动带来旧的 EventSystem 并且我在控制台中看到此错误:
InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
UnityEngine.Input.get_mousePosition () (at <213e6bf8f2dd495fbd693e6ce506136b>:0)
UnityEngine.UI.MultipleDisplayUtilities.GetMousePositionRelativeToMainDisplayResolution () (at /Applications/Unity/Hub/Editor/2020.1.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/MultipleDisplayUtilities.cs:40)
UnityEngine.EventSystems.BaseInput.get_mousePosition () (at /Applications/Unity/Hub/Editor/2020.1.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/BaseInput.cs:75)
UnityEngine.EventSystems.StandaloneInputModule.UpdateModule () (at /Applications/Unity/Hub/Editor/2020.1.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:175)
UnityEngine.EventSystems.EventSystem.TickModules () (at /Applications/Unity/Hub/Editor/2020.1.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:328)
UnityEngine.EventSystems.EventSystem.Update () (at /Applications/Unity/Hub/Editor/2020.1.14f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:343)
Run Code Online (Sandbox Code Playgroud) 两个阵列:
a1 = ["a", "b", "c", "d", "e", "f"]
a2 = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
如何插入a2到a1,保持A2顺序,但随机指标的A1?
在测试环境中,我需要采用特定百分比的数组元素.
我的请求的规范可以在此测试中描述:
def test_percent_elements
array = [1,2,3,4,5,6,7,8,9,10]
assert_equal([], array.percent_elements(0))
assert_equal([1], array.percent_elements(1))
assert_equal([1], array.percent_elements(10))
assert_equal([1,2], array.percent_elements(11))
assert_equal([1,2,3,4,5], array.percent_elements(50))
assert_equal([1,2,3,4,5,6,7,8,9,10], array.percent_elements(100))
end
Run Code Online (Sandbox Code Playgroud)
哪个是在Ruby中解决这个问题的最佳方法?
我想在我的DOM上调用特定元素的函数,例如:
$(".red").css({backgroundColor: "pink"});
Run Code Online (Sandbox Code Playgroud)
它的工作原理确定为已经存在的到DOM任何元素,但我也希望这种方法在动态添加到DOM元素被调用.
我尝试过这样的事情:
$(".red").on(function(){ this.css({backgroundColor: "pink"}) });
Run Code Online (Sandbox Code Playgroud)
要么:
$(".red").on("load", function(){ this.css({backgroundColor: "pink"}) });
Run Code Online (Sandbox Code Playgroud)
但没有任何成功.
检查jsFiddle
这个.css()电话只是一个例子,我实际上想要调用其他类型的函数
我正在使用 Ruby 进行一些开发简单游戏的测试。我非常需要使用 2D 矢量。例如如何轻松计算二维坐标系中两点之间的距离?
我知道我可以计算坐标减法的斜边,但我想知道是否有任何现成的库或方法可以做到这一点。
我找到了Math.distance但它对我不起作用 ( undefined method 'distance' for Math:Module (NoMethodError)) 即使require "facets"
A具有父视图OpenOrderListView,该视图创建OpenOrderViews具有事件的实例.我想获得的结果是,当markCompleted一个按钮OpenOrderView被点击的函数调用来告诉模型来设置属性.
该功能正在运行,但它在父(OpenOrderListView)内的所有OpenOrderViews上调用,而不仅仅是处理click事件的视图.如何才能使此事件仅在已执行的视图上触发?
代码如下
window.OpenOrderListView = Backbone.View.extend({
el: 'table.openOrders tbody',
initialize: function() {
_.bindAll(this,'render');
this.render();
},
render : function() {
var $openOrders
var models = this.collection.open();
for (var i = models.length - 1; i >= 0; i--) {
console.log('model', models[i]);
console.log("this.template", this.template);
new OpenOrderView({'model': models[i], 'el': this.el});
};
}
});
window.OpenOrderView = Backbone.View.extend({
initialize: function() {
console.log('this', this);
_.bindAll(this,'render',
'markCompleted',
'markInProgress');
this.render();
},
events : …Run Code Online (Sandbox Code Playgroud) 有这个:
class User < ActiveRecord::Base
after_save :execute_after_save
def execute_after_save
Kernel.puts "Actual object still not saved" if changed?
end
end
Run Code Online (Sandbox Code Playgroud)
Kernel.puts应该永远调用该句子,因为在保存对象后它不会被更改.
1.9.3p286 :003 > u = User.create!(:name => "Wadus Name")
Actual object still not saved
=> #<User id: 1, name: "Wadus Name">
1.9.3p286 :004 > u.changed?
=> false
1.9.3p286 :004 > u.name = "Other Name"
=> "Other Name"
1.9.3p286 :005 > u.changed?
=> true
1.9.3p286 :006 > u.save!
Actual object still not saved
=> …Run Code Online (Sandbox Code Playgroud) 在emberjs标签下有几个问题,讨论如何管理不同的环境,但所有问题都只适用于非常具体的场景.
我正在寻找一种如何为不同环境设置不同配置变量的通用解决方案.
比方说,我的开发环境有一个API URL,生产环境有另一个API URL .
production_api_url: "http://production.server.com/api"
development_api_url: "http://development.server.com/api"
Run Code Online (Sandbox Code Playgroud)
在我的适配器中,我想使用适当的API URL:
# /app/adapters/application.js
import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend({
namespace: 'api',
host: [[API_URL]]
});
Run Code Online (Sandbox Code Playgroud) 我必须确定请求是否来自本地主机。
我在尝试:
要求 http://localhost:3000
# MyController
request.local? # -> 0
request.host # -> localhost
request.ip # -> ::1
request.remote_ip # -> ::1
Run Code Online (Sandbox Code Playgroud)
要求 http://127.0.0.1:3000
# MyController
request.local? # -> 0
request.host # -> 127.0.0.1
request.ip # -> 127.0.0.1
request.remote_ip # -> 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
我想知道是否有标准的方法。看起来像解决方案:ApplicationController.local_request?但受到保护且由于request.local?返回0本地请求而无法正常工作。
我也不能相信,request.host因为它可以被/etc/hosts花招欺骗。
我正在尝试优化一些代码,而我想要在每个方法调用上检查一个值,只需定义方法来响应已经预先计算的检查,因为这个检查不会在实例的整个实时中发生变化.
我决定为每个创建的实例定义不同版本的方法.或多或少这样:
class TestingSingletonMethodsWithVariable
METHODS = %w(a b c d)
def initialize(favorite_method)
class << self
METHODS.each do |method_name|
if( favorite_method == method_name )
define_method method_name do
puts "#{method_name} its my favorite method"
end
else
define_method method_name do
puts "#{method_name} its not my favorite method"
end
end
end
end
end
end
t = TestingSingletonMethodsWithVariable.new('b')
t.a
t.b
t.c
t.d
# $ ruby test/testing_singleton_methods_with_variable.rb
# test/testing_singleton_methods_with_variable.rb:7:in `initialize': undefined local variable or method `favorite_method' for #<Class:#<TestingSingletonMethodsWithVariable:0x1001a77b8>> (NameError)
# …Run Code Online (Sandbox Code Playgroud) 当我想向现有表添加多列时,我可以这样做:
rails g migration AddColumnsToUser col1:integer col2:integer .. etc.
Run Code Online (Sandbox Code Playgroud)
这将生成包含几行的迁移:
def change
add_column :users, :col1, :integer
add_column :users, :col2, :integer
end
Run Code Online (Sandbox Code Playgroud)
这将在几个alter table命令中转换为后端数据库:
ALTER TABLE users ADD COLUMN col1 SMALLINT(6) NOT NULL;
ALTER TABLE users ADD COLUMN col2 SMALLINT(6) NOT NULL;
Run Code Online (Sandbox Code Playgroud)
问题是如果你正在处理一个大表,每一个alter table都会花费很多时间,在 MySQL 中更少,因为后端引擎会生成一个表的重复,并且会做很多昂贵的过程。所有这些工作都必须为我要添加的每一列完成。
所以,我的问题是,我怎样才能将所有这些add_column句子聚合成一个,这样结果将是alter table批处理模式,如下所示:
ALTER TABLE users
ADD COLUMN col1 SMALLINT(6) NOT NULL,
ADD COLUMN col2 SMALLINT(6) NOT NULL;
Run Code Online (Sandbox Code Playgroud) ruby ×6
arrays ×2
javascript ×2
activerecord ×1
alter-table ×1
backbone.js ×1
callback ×1
coordinates ×1
docker ×1
ember-cli ×1
ember.js ×1
ember.js-2 ×1
environments ×1
events ×1
jquery ×1
jquery-on ×1
localhost ×1
migration ×1
optimization ×1
request ×1
sorting ×1
vector ×1
views ×1