我是.NET开发人员,最近开始用ruby_koans学习ruby.Ruby的一些语法是惊人的,其中之一是它处理"三明治"代码的方式.
以下是红宝石三明治代码.
def file_sandwich(file_name)
file = open(file_name)
yield(file)
ensure
file.close if file
end
def count_lines2(file_name)
file_sandwich(file_name) do |file|
count = 0
while line = file.gets
count += 1
end
count
end
end
def test_counting_lines2
assert_equal 4, count_lines2("example_file.txt")
end
Run Code Online (Sandbox Code Playgroud)
我很着迷,每次访问文件时我都可以摆脱繁琐的"文件打开和关闭代码"但却无法想到任何C#等效代码.也许,我可以使用IoC的动态代理来做同样的事情,但有什么方法我可以纯粹用C#做到这一点?
提前谢谢了.
在调用块之前/之后,retaincount始终是1.从apple block doc我们知道self应该保留.谁能知道为什么?
NSLog(@"Before block retain count: %d", [self retainCount]);
void (^block)(void) = ^(void){
UIImage* img = [UIImage imageNamed:@"hometown.png"];
[self setImage:img];
NSLog(@"After block retain count: %d", [self retainCount]);
};
block();
Run Code Online (Sandbox Code Playgroud) 我对传球有一点疑问.
def a_method(a, b)
a + yield(a, b)
end
Run Code Online (Sandbox Code Playgroud)
这很好用.
k = a_method(1, 2) do |x, y|
(x + y) * 3
end
puts k
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
puts a_method(1, 2) do |x, y|
(x + y) * 3
end
# LocalJumpError: no block given (yield)
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这个吗?
谢谢.Paolo Perrotta从Metaprogramming Ruby中获取的示例.好书.
有没有办法阻止skype中的群聊而不阻止所有群聊.我不想离开小组,因为这会导致我的朋友重新加我.想要在不通知其他小组成员的情况下逃离小组.可能吗?如果有,怎么样?
我正在尝试在tableViewCell 上ImageView
使用AFNetworking 设置图像setImageWithURLRequest
.如果我不在方法中包含success
和failure
块,如下所示:
[cell.thumbnailImageView setImageWithURL:url placeholderImage:nil];
Run Code Online (Sandbox Code Playgroud)
一切都很完美,图像设置.不幸的是我需要块.
这是我目前所拥有的cellForRowAtIndexPath
:
if (self.images) { //check if the array of images is set, fetched from a URL
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: [[self.images objectAtIndex:indexPath.row] objectForKey:@"tbUrl"]]];
[cell.thumbnailImageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success"); //it always lands here! But nothing happens
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"fail");
}];
}
Run Code Online (Sandbox Code Playgroud) 我一直认为rubyists因为风格偏好而选择隐藏在ruby中的回报(更少的单词=更简洁).但是,有人可以向我确认在下面的示例中,您实际上必须隐式返回,否则预期的功能将无效吗?(预期的功能是能够将一个句子分成单词,并为每个单词返回"以元音开头"或"以辅音开头")
# With Implicit Returns
def begins_with_vowel_or_consonant(words)
words_array = words.split(" ").map do |word|
if "aeiou".include?(word[0,1])
"Begins with a vowel" # => This is an implicit return
else
"Begins with a consonant" # => This is another implicit return
end
end
end
# With Explicit Returns
def begins_with_vowel_or_consonant(words)
words_array = words.split(" ").map do |word|
if "aeiou".include?(word[0,1])
return "Begins with a vowel" # => This is an explicit return
else
return "Begins with a consonant" # => This is another explicit return …
Run Code Online (Sandbox Code Playgroud) 我确信这是一个非常基本的问题,所以我提前道歉,因为我是新手.
我正在开发一个首先设计为移动设备的网络应用.由于我的所有初始布局都是针对小屏幕设计的,因此我推出了一款手机jpg作为<img>
.然后我用绝对定位将我的画布覆盖到了这个上面.这给了我一个伪移动屏幕,我可以在试验我的设计时使用,而无需经常用手机测试.
我们的想法是使用合适的媒体查询,当遇到较小的屏幕时,使用这些查询display:block
来防止图像被显示.
很短的时间我让它工作,但现在我已经打破它(没有备份)),看不出怎么样!它可以在更宽的桌面屏幕上正常工作.将显示图像容器,并且背景画布正确放置在顶部.然而,图像容器也在移动设备上显示(并且由于没有绝对位置),然后我的实际布局显示在.
HTML看起来像这样......
<div id="container">
<img src='phone.jpg' class="desktop-visible"/>
</div>
<div id="backdrop">
Text
</div>
Run Code Online (Sandbox Code Playgroud)
我的CSS目前是这个......
// Set Defaults
.desktop-visible { display:none;}
// Desktop and landscape tablets
@media (min-width: 768px) {
.desktop-visible { display: block; margin: 0 auto; }
#container {
position:relative;
width: 538px;
margin: 0 auto;
}
#container img {
position:absolute;
margin: 0 auto;
}
#backdrop {
margin: 0 auto;
position:absolute;
top:86px;
left:26px;
width:483px;
max-height: 862px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
// Portrait …
Run Code Online (Sandbox Code Playgroud) 我正在执行两个异步方法,都是通过块来回答.在调用第三个块之前,我必须等待两个块都被调用.如果我的方法是同步的,使用dispatch_group会起作用,但我不想改变它们,因为它需要大量的代码重构.
更好地解释问题:
[myLib doTask1:^{
// do stuff when task1 is done
}];
[myLib doTask2:^{
// do stuff when task2 is done
}];
Run Code Online (Sandbox Code Playgroud)
然后我想做一些像:
[self executeThisWhenBothTasksDone:^{
// do stuff after both tasks are done
}
Run Code Online (Sandbox Code Playgroud) concurrency block objective-c grand-central-dispatch conditional-statements
这段代码写在market.phtml中
<?php echo $this->getLayout()->createBlock('core/template')->setData('vendorId',$vendor->getCustomerId())->setTemplate('marketplace/vendors/badge.phtml')->toHtml();?>
Run Code Online (Sandbox Code Playgroud)
在Badge.php
echo $this->vendorId;
Run Code Online (Sandbox Code Playgroud)
但我的输出为空.这是将数据传递给阻止的正确方法吗?
可以使用下面的HTML将锚设置为块元素吗?我之前在某处看过它是错的,但是我看不清楚为什么这太方便了!这种方法有什么缺点,如果有的话?
<style>
a.button{
display:inline-block;
padding: 10px 20px;
border: 1px solid grey;
}
</style>
<a class="button" href="#link">Click me!</a>
Run Code Online (Sandbox Code Playgroud)