我有以下代码用于将哈希集合转换为我的类上的方法(有点像活动记录).我遇到的问题是我的二传手不能正常工作.我对Ruby仍然很陌生,并且相信我已经让自己扭转了一下.
class TheClass
def initialize
@properties = {"my hash"}
self.extend @properties.to_methods
end
end
class Hash
def to_methods
hash = self
Module.new do
hash.each_pair do |key, value|
define_method key do
value
end
define_method("#{key}=") do |val|
instance_variable_set("@#{key}", val)
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这些方法已创建,我可以在课堂上阅读它们,但设置它们不起作用.
myClass = TheClass.new
item = myClass.property # will work.
myClass.property = item # this is what is currently not working.
Run Code Online (Sandbox Code Playgroud) 我开始使用EF Code First和MVC,我有点难过.我有以下数据库结构(很抱歉,我不允许发布图片):
表 - 产品
表 - 相关产品
Products.ProductID上的1-Many - > RelatedProducts.ProductID
1 - Products.ProductID上的很多 - > RelatedProducts.RelatedProductID
基本上我有一个产品,可以有一系列与之相关的产品.它们保存在RelatedProducts表中,其中包含ProductID和相关产品的ProductID定义的关系,我将其命名为RelatedProductID.在我的代码中,我生成了以下类:
public class MyDBEntities : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<RelatedProduct> RelatedProducts { get; set; }
}
public class Product
{
public Guid ProductID { get; set; }
public string Name { get; set; }
public string Heading { get; set; }
public string Description { get; set; }
public decimal Price { get; set; } …
Run Code Online (Sandbox Code Playgroud) many-to-many entity-framework-4 self-reference entity-framework-ctp5
我最近开始使用 ANTLR。我目前正试图编码与表达语法+
,-
,*
并array[index]
和几个结构。
这是所需的语法:
Exp -> Exp (+ | - | * | < | &&) Exp
| Exp [ Exp ]
| -Exp
| ( Exp )
| Exp.length
| true
| false
| Id
| this
| ! Exp
Run Code Online (Sandbox Code Playgroud)
我首先将其重构为AndExp
, SumExp
,ProdExp
等以解决优先级问题。大致是这样的:
Exp -> AndExp
AndExp -> CmpExp (&& CmpExp)*
CmpExp -> SumExp (< SumExp)*
SumExp -> ProdExp ((+|-) ProdExp)*
ProdExp -> UnaryExp (Times UnaryExp)*
UnaryExp -> …
Run Code Online (Sandbox Code Playgroud) 我们希望创建一个网站,向网站的所有新访问者显示信息,即欢迎,请阅读我们的帮助指南等.
最大的问题是我们的网站不允许使用持久的cookie(即超过20分钟).有没有人知道我们能否确定用户之前是否访问过该网站.作为另一个限制,我们不能向系统添加任何形式的注册.
该应用程序正在ASP.Net 3.5中创建.
谢谢您的帮助
我想实现以下逻辑:当我在主servlet的doService方法(在主Web容器线程中)中收到HttpServletRequeset和HttpServletResponse时,我启动A,B,C三个线程(由我自己的程序管理的线程)来处理其他servlet并行模式,然后在主线程中加入来自这些servlet的每个响应,如果我自己的一个线程(假设一个线程)工作缓慢,主线程将完成,所以主响应将返回给user.A 线程必须继续如果工作正常,我稍后会在浏览器端使用AJAX请求A线程的响应.
因此,我想克隆Servlet容器提供的HttpServlettRequest和HttpServletResponse,并且必须分离克隆的请求和响应(当容器的HttpServletTrequest和HttpServletResponse完成时,克隆的请求和响应仍然可以正常工作).
克隆的请求和响应的行为必须与我的代码视图中的Container相同.它可以被跟踪和包含.
任何的想法?
非常感谢!
所需产物
__getattr__
在ruby中是否有等效的python (至少可以找到方法)?
class X(object):
def __getattr__(self, name):
return lambda x: print("Calling " + name + ": " + x)
x = X()
x.some_method("some args")
Run Code Online (Sandbox Code Playgroud)
所以它可能是这样的:
class X
# .. ??? ..
def default_action(method_name, x)
puts "Calling {method_name}: {x}"
end
end
x = X.new()
x.some_method("some args")
Run Code Online (Sandbox Code Playgroud) 我需要从字符串中提取和删除一个单词.字应该是大写,和之后的定界符之一/
,;
,(
,-
或的空间.
一些例子:
"this is test A/ABC"
预期产出:"this is test A"
和"ABC"
"this is a test; ABC/XYZ"
预期产出:"this is a test; ABC"
和 "XYZ"
"This TASK is assigned to ANIL/SHAM in our project"
预期产出:"This TASK is assigned to ANIL in our project"
和 "SHAM"
"This TASK is assigned to ANIL/SHAM in OUR project"
预期产出:"This TASK is assigned to ANIL/SHAM in project"
和 "OUR"
"this is test AWN.A"
预期产出: …
有没有办法让$ .getJSON返回变量数组?
我知道它的异步和超出范围,但我会在ajax回调中使用它,我只需要先获取所有值并检查它们与另一个数组.
就像是:
$.getJSON('itemManager.php?a=getItems', function(data){
// itemArray = new Array(data);
// idsArray = new Array(data.id);
for (var i in someOtherArray){
if($.inArray(i, idsArray) == -1){
// do something...
// get jason variable by id?
// itemArray[i].someVariable
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: JSON结构
[{"id":"786","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"787","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"785","user_id":"1","seller_id":"2","address_id":"1","time":1299852114,"publicComment":null,"personalComment":null},
{"id":"784","user_id":"1","seller_id":"2","address_id":"1","time":1299852113,"publicComment":null,"personalComment":null},
{"id":"783","user_id":"1","seller_id":"2","address_id":"1","time":1299852111,"publicComment":null,"personalComment":null}]
Run Code Online (Sandbox Code Playgroud)
这基本上就是这个想法.
我猜这里有各种解决方案,但我正在寻找代码最少的东西.
我想知道是否有人可以向我展示使用 C# 在 MSChart 中设置线条颜色的示例代码。
我知道我可以从选项中选择一个方案,但我想为我展示的不同系列选择我自己的颜色。
预先感谢
我有一个主UI线程,它运行应用程序并创建主窗口表单(让我们称之为W
).我还有一个辅助线程,我旋转并创建一个对话框(让我们称之为B
).
我想将对话框的所有者设置B
为主窗口W
.B
s所有者的设置发生在创建的线程上B
.基本上:
b.Owner = w;
Run Code Online (Sandbox Code Playgroud)
但这会引发一个跨线程异常,告诉我我正在尝试W
从错误的线程访问该对象.
于是,我就用主UI线程上执行代码,Control.Invoke
上W
.但是,我得到同样的错误,告诉我我正在尝试B
从错误的线程访问:
System.InvalidOperationException was unhandled by user code
Message=Cross-thread operation not valid: Control 'B' accessed from a
thread other than the thread it was created on.
Source=System.Windows.Forms
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?