之前可能已经提出过这个问题,但快速搜索只会提出与C#相同的问题.看这里.
我基本上想要做的是检查给定对象是否实现了给定的接口.
我有点想出了一个解决方案,但是在if或case语句中经常使用它并不够舒服,我想知道Java没有内置的解决方案.
public static Boolean implementsInterface(Object object, Class interf){
for (Class c : object.getClass().getInterfaces()) {
if (c.equals(interf)) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我正在努力增强我的通知脚本.脚本的工作方式是我把它放在一个长时间运行的shell命令后面,然后在长时间运行的脚本完成后调用各种通知.
例如:
sleep 100; my_notify
Run Code Online (Sandbox Code Playgroud)
获取长时间运行脚本的退出代码会很好,问题是调用my_notify会创建一个无法访问$?变量的新进程.
相比:
~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID"
ls: nonexisting_file: No such file or directory
exit code: 1
PPID: 6203
Run Code Online (Sandbox Code Playgroud)
与
~ $: ls nonexisting_file; my_notify
ls: nonexisting_file: No such file or directory
exit code: 0
PPID: 6205
Run Code Online (Sandbox Code Playgroud)
该my_notify脚本包含以下内容:
#!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来获取上一个命令的退出代码而不会过多地改变命令的结构.我知道如果我将其更改为更好的工作time,例如my_notify longrunning_command...我的问题将得到解决,但我实际上喜欢我可以在命令结束时解决它并且我担心第二种解决方案的复杂性.
这可以完成,还是从根本上与shell的工作方式不兼容?
我的shell是,zsh但我希望它也可以使用bash.
我正在尝试开发RESTful Sinatra应用程序.现在,我知道如何用类似的方式响应删除请求
delete '/user/:id' do |id|
#do something in the model
end
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是如何执行该方法.我不能拥有DELETE而不是GET的链接,是吗?
到目前为止我找到的唯一解决方案是通过jQuery发送DELETE请求:如何在jQuery中发送PUT/DELETE请求?
我尝试在github上查看不同的RESTful Sinatra项目,但我的Ruby知识可能仅限于了解它们是如何做到的.
我正在尝试测试我的控制器并保持关注点的分离.
第一个问题是"谁能够执行哪个行动?"
我使用authlogic进行身份验证,使用be9的acl9进行授权.但这应该不重要,我所有的授权问题都是在a before_filter.我正在before_filter通过类似的东西测试这样的:
describe SomeModelsController, "GET to index (authorization)" do
before(:each) do
@siteadmin = mock_model(User)
@siteadmin.stub!(:has_role?).with("siteadmin", nil).and_return(true)
end
it "should grant access to a siteadmin" do
controller.should_receive(:current_user).at_least(:once).and_return(@siteadmin)
get :index
response.should be_success
end
end
Run Code Online (Sandbox Code Playgroud)
这个规格工作得很好!
现在,第二个问题是"行动是否做了应该做的事情?"
这不涉及检查授权.最好/最干净的解决方案是将before_filter所有内容一起跳过,并执行以下操作:
describe SomeModelsController, "GET to index (functional)" do
it "should find all Models" do
Model.should_receive(:find).with(:all)
end
end
Run Code Online (Sandbox Code Playgroud)
无需担心具有哪个角色的用户必须首先登录.现在我解决了这个问题:
describe SomeModelsController, "GET to index (functional)" do
before(:each) do
@siteadmin = mock_model(User)
@siteadmin.stub!(:has_role?).with("siteadmin", nil).and_return(true)
controller.stub!(:current_user).and_return(@siteadmin)
end
it …Run Code Online (Sandbox Code Playgroud) 参考之前提出的问题,我想知道如何获取当前活动文档的标题.
我在上面问题的答案中尝试了脚本.这有效,但只给出了应用程序的名称.例如,我正在写这个问题:当我启动脚本时它会给我应用程序的名称,即"Firefox".这很整洁,但并没有真正帮助.我想要捕获我当前活动文档的标题.看图像.
Firefox标题http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg
我正在使用Leopard,因此不需要向后兼容性.我也使用Python的Appkit来访问NSWorkspace类,但如果你告诉我Objective-C代码,我可以找到Python的翻译.
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
Run Code Online (Sandbox Code Playgroud)
保存为脚本并使用shell中的osascript调用它.
我试图用RSpec测试我的观点.导致我麻烦的特定视图根据url参数改变其外观:
link_to "sort>name", model_path(:sort_by => 'name') 结果 http://mydomain/model?sort_by=name
然后我的视图使用这个参数:
<% if params[:sort_by] == 'name' %>
<div>Sorted by Name</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
RSpec看起来像这样:
it "should tell the user the attribute for sorting order" do
#Problem: assign params[:sort_for] = 'name'
render "/groups/index.html.erb"
response.should have_tag("div", "Sorted by Name")
end
Run Code Online (Sandbox Code Playgroud)
我想在RSpec中测试我的视图(没有控制器),但我无法将此参数输入到我的params变量中.我试过assign各种不同的口味:
assign[:params] = {:sort_by => 'name'}assign[:params][:sort_by] = 'name'到目前为止没有成功.每个想法都表示赞赏.
以下场景几乎总结了我的问题:
Scenario: problems with subprocesses
Given the date is 01/01/2012 10:31
When I run `ruby -e "puts Time.now"`
Then the output should contain "10:31"
Run Code Online (Sandbox Code Playgroud)
它归结为When I run ruby -e "puts Time.now"启动一个子进程,从而使我的所有Timecop.freeze存根都无效,因为它们只在主进程上工作.我需要以某种方式将当前上下文"注入"到运行的命令中,但我似乎无法想出任何东西.我在这里尝试不可能的事吗?
步骤:
require 'timecop'
Given /^the date is (\d+)\/(\d+)\/(\d+) (\d+):(\d+)$/ do |month, day, year, hour, minute|
Timecop.freeze(Time.local(year.to_i, month.to_i, day.to_i, hour.to_i , minute.to_i, 0))
end
Run Code Online (Sandbox Code Playgroud) 我有以下ActiveRecord迁移:
class CreateSubjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.string :title
t.timestamps
end
change_table :projects do |t|
t.references :subjects
end
end
def self.down
drop_table :subjects
remove_column :projects, :subjects_id #defeats the purpose of having references
end
end
Run Code Online (Sandbox Code Playgroud)
我其实喜欢这种references风格.不幸的是我找不到references该self.down部分中的回滚等价物.如果我写,remove_column :projects, :subjects_id我也可以写t.integer :subjects_id,这将使它更安全.
我想做点什么
let colors = execute(":highlight")
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的,我所能做的就是execute(":highlight")打开一个窗口,但我真正需要的是将该窗口的内容转换为变量 - 就像system()调用外部命令一样.可以这样做吗?
目前我正在解决我的问题,boost::shared_ptr但语义不太正确,因为我将成员从一个对象"移植"到另一个对象.我正在查看此列表,但它没有产生太多.我的简短谷歌搜索也是如此.
基本上我正在寻找一个unique_ptr适用于我的gcc4.2的实现(因此限制不使用C++ 11)
ruby ×3
rspec ×2
activerecord ×1
aruba ×1
bash ×1
c++ ×1
cucumber ×1
exit-code ×1
http-delete ×1
httprequest ×1
java ×1
macos ×1
objective-c ×1
python ×1
rest ×1
shell ×1
sinatra ×1
time-travel ×1
vim ×1
zsh ×1