有没有办法在调试器中设置某些默认值?我找不到这样做的方法..
我想用卡片中的每张卡片制作一个数组,所以它会是["Ac","Ad","Ah","As","Kc",...]虽然顺序并不重要.
是不是有一种注入可以用来解决这个问题的方法?这是我能得到的尽可能接近.
cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]
suits = ["c", "s", "d", "h"]
ruby-1.9.2-p180 :025 > cards.inject(suits) { |suit, card| suit.map{|s| "#{card}#{s}"}}
=> ["23456789TJQKAc", "23456789TJQKAs", "23456789TJQKAd", "23456789TJQKAh"]
Run Code Online (Sandbox Code Playgroud) 这个方法只是验证我能够正确地看到ruby数组的元素.
static VALUE
print_cards(self)
VALUE self;
{
VALUE cards;
int i;
cards = rb_ivar_get(self, rb_intern("@cards"));
VALUE *ary_ptr = RARRAY_PTR(cards);
int ary_length = RARRAY_LEN(cards);
for(i=0; i< ary_length; i++)
printf("%d\n", ary_ptr[i]);
return Qnil;
}
void Init_ev() {
rb_eval_string("require './lib/ev/pair_counter'");
VALUE PairCounter = rb_path2class("EV::PairCounter");
rb_define_method(PairCounter, "print_cards", print_cards, 0);
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用该方法时,数组的元素是错误的.奇怪的是,它看起来并不像我得到某种地址信息,因为打印的数字大小大致与ruby数组中的数字大小相对应.每次创建新对象并运行print_cards时,数字也是一致的.
ruby-1.9.2-p180 :001 > p = EV::PairCounter.new #=> #<EV::PairCounter:0x000001046a10f8 @pairs={}, @cards=[]>
ruby-1.9.2-p180 :002 > p.add_card(1) #=> 1
ruby-1.9.2-p180 :003 > p.print_cards
3 #=> nil
ruby-1.9.2-p180 :004 > p.add_card(5) #=> 2
ruby-1.9.2-p180 :005 > p.add_card(88) …Run Code Online (Sandbox Code Playgroud) 在这个页面http://swtch.com/~rsc/regexp/regexp3.html上它说RE2支持命名表达式。
RE2 支持 Python 风格的命名捕获,但不支持 .NET和Perl 使用的
(?P<name>expr)替代语法。(?<name>expr)(?'name'expr)
ruby-1.9.2-p180 :003 > r = RE2::Regexp.compile("(?P<foo>.+) bla")
#=> #<RE2::Regexp /(?P<foo>.+) bla/>
ruby-1.9.2-p180 :006 > r = r.match("lalal bla")
#=> #<RE2::MatchData "lalal bla" 1:"lalal">
ruby-1.9.2-p180 :009 > r[1] #=> "lalal"
ruby-1.9.2-p180 :010 > r[:foo]
TypeError: can't convert Symbol into Integer
ruby-1.9.2-p180 :011 > r["foo"]
TypeError: can't convert String into Integer
Run Code Online (Sandbox Code Playgroud)
但我无法访问与名称的匹配,因此这似乎是一个无用的实现。我错过了什么吗?
如果我有两个查询,如下所示:
Store.any_in(:store_id => @user.stores_followed)
Store.any_in(:store_id => @category.stores)
Run Code Online (Sandbox Code Playgroud)
如何使用any_of将它们加入OR?我试过这个但事实并非如此.我试过了
Store.any_of({:store_id.any_in => @user.stores_followed},
{:store_id.any_in => @category.stores})
Run Code Online (Sandbox Code Playgroud) 有谁知道这样做的插件?在TextMate中很容易找到,但到目前为止Sublime 2没有运气(我更喜欢).
我有这样的陈述:
@obj[:attribute].eql?("TestValue").should be_true
Run Code Online (Sandbox Code Playgroud)
如果不是只告诉我测试失败,它会告诉我它失败了,因为@obj [:属性]是零或者是1234或者它是什么,这将是很好的.有没有办法做到这一点?
我有一堆非常重复的rspec测试,它们都具有相同的格式:
it "inserts the correct ATTRIBUTE_NAME" do
@o.ATTRIBUTE_NAME.should eql(VALUE)
end
Run Code Online (Sandbox Code Playgroud)
如果我能做一个像以下那样的线测试会很好:
compare_value(ATTRIBUTE_NAME, VALUE)
Run Code Online (Sandbox Code Playgroud)
但是,似乎并不适合这些类型的测试.还有其他选择吗?
我跟随一个轨道广播,并被告知,打开全球宝石对于一组特定的宝石是有益的.但是现在当我尝试安装宝石时,我会遇到错误.所以使用后
rvm gemset use global
Run Code Online (Sandbox Code Playgroud)
我该如何还原呢?
$ rvm gemdir
/Users/jeremysmith/.rvm/gems/ruby-1.9.2-p290@global
Run Code Online (Sandbox Code Playgroud) 我有一个半复杂和水平对称的形状,我试图使用HTML5构建.当我试图完成它时,我意识到如果我可以复制一半的形状,镜像并移动它以将两个图像连接在一起会更容易.我找到了如何镜像和移动形状的示例,但没有找到如何复制它的示例.
显然,我希望我不需要两个单独的画布元素.
这是我的代码供参考:
var canvas = document.getElementById(id),
context = canvas.getContext("2d"),
color,
height = 50;
width = 564;
arrowWidth = 40,
arrowHeight = 15,
arrowStart = height - arrowHeight,
edgeCurveWidth = 50;
if (parseInt(id.substr(-1), 10) % 2) {
color = "#F7E5A5";
} else {
color = "#FFF";
}
context.beginPath();
context.lineWidth = 4;
context.strokeStyle = "#BAAA72";
context.moveTo(0, 0);
context.quadraticCurveTo(-10, arrowStart, edgeCurveWidth, arrowStart);
context.quadraticCurveTo(width/2 - arrowWidth/2 - 15, arrowStart - 15, width/2 - arrowWidth/2, arrowStart);
context.quadraticCurveTo(width/2, height, width/2, height);
context.stroke();
context.lineTo(width/2, 0);
context.closePath(); …Run Code Online (Sandbox Code Playgroud) ruby ×9
c ×1
canvas ×1
html5 ×1
html5-canvas ×1
javascript ×1
mongodb ×1
mongoid ×1
re2 ×1
regex ×1
rspec ×1
ruby-debug ×1
rvm ×1
shoulda ×1
sublimetext ×1
textmate ×1