在Ruby的最新版本中,许多方法在没有块的情况下被调用时Enumerable
返回Enumerator
:
[1,2,3,4].map
#=> #<Enumerator: [1, 2, 3, 4]:map>
[1,2,3,4].map { |x| x*2 }
#=> [2, 4, 6, 8]
Run Code Online (Sandbox Code Playgroud)
我想在我自己的方法中做同样的事情,如下:
class Array
def double(&block)
# ???
end
end
arr = [1,2,3,4]
puts "with block: yielding directly"
arr.double { |x| p x }
puts "without block: returning Enumerator"
enum = arr.double
enum.each { |x| p x }
Run Code Online (Sandbox Code Playgroud) 对于我正在编写的一些代码,我可以debounce
在Java中使用一个很好的通用实现.
public interface Callback {
public void call(Object arg);
}
class Debouncer implements Callback {
public Debouncer(Callback c, int interval) { ... }
public void call(Object arg) {
// should forward calls with the same arguments to the callback c
// but batch multiple calls inside `interval` to a single one
}
}
Run Code Online (Sandbox Code Playgroud)
当使用相同的参数call()
多次以interval
毫秒为单位调用时,应该只调用一次回调函数.
可视化:
Debouncer#call xxx x xxxxxxx xxxxxxxxxxxxxxx
Callback#call x x x (interval is 2)
Run Code Online (Sandbox Code Playgroud)
我使用Devise 3有一个非常标准的Rails 4应用程序
我想让注册表单在当前(Mavericks)版本的Safari中触发密码建议:
iCloud Keychain已启用,我在其他页面上获得建议,因为某些原因我的表单无效.
我似乎无法弄清楚启用建议究竟需要什么.
以下是设计生成的表单:
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="SKtqmi1L/BKcTE/Hvlvw1H3ZRH8nd2UNiNnVILuLS/E=" />
</div>
<div>
<label for="user_email">Email</label><br />
<input autofocus="autofocus" id="user_email" name="user[email]" type="email" value="" />
</div>
<div>
<label for="user_password">Password</label><br />
<input id="user_password" name="user[password]" type="password" />
</div>
<div>
<label for="user_password_confirmation">Password confirmation</label><br />
<input id="user_password_confirmation" name="user[password_confirmation]" type="password" />
</div>
<div><input name="commit" type="submit" value="Sign up" /></div>
</form>
Run Code Online (Sandbox Code Playgroud)
如何更改表单以触发密码建议
有关该功能的任何可用文档吗?
我有一个使用buildr构建的java项目,它有一些外部依赖项:
repositories.remote << "http://www.ibiblio.org/maven2"
repositories.remote << "http://packages.example/"
define "myproject" do
compile.options.target = '1.5'
project.version = "1.0.0"
compile.with 'dependency:dependency-xy:jar:1.2.3'
compile.with 'dependency2:dependency2:jar:4.5.6'
package(:jar)
end
Run Code Online (Sandbox Code Playgroud)
我希望这个构建一个包含所有这些依赖项的独立jar文件.
我怎么做?
(这是一个逻辑上的后续问题:如何从包含的依赖项中删除所有未使用的代码,只打包我实际使用的类?)
我正在查看传入文件的目录(使用来自apache commons的FileAlterationObserver).
class Example implements FileAlterationListener {
public void prepare() {
File directory = new File("/tmp/incoming");
FileAlterationObserver observer = new FileAlterationObserver(directory);
observer.addListener(this);
FileAlterationMonitor monitor = new FileAlterationMonitor(10);
monitor.addObserver(observer);
monitor.start();
// ...
}
public void handleFile(File f) {
// FIXME: this should be called when the writes that
// created the file have completed, not before
}
public void onFileCreate(File f) {
handleFile(f);
}
public void onFileChange(File f) {
handleFile(f);
}
}
Run Code Online (Sandbox Code Playgroud)
文件由我无法控制的进程写入.
我对该代码的问题是在最初创建文件时触发了我的回调.我需要它来在文件被更改并且对文件的写入完成时触发.(可能通过检测文件何时停止更改)
最好的方法是什么?
在我的Ruby应用程序中,我需要处理来自用户输入的URI(实际上是IRI)
str = "http://??????.???????/?????_?????"
Run Code Online (Sandbox Code Playgroud)
我使用Addressable对这些进行规范化,并且只存储规范化的形式:
normalized = Addressable::URI.parse(str).normalize
normalized.to_s
#=> http://xn--p1b6ci4b4b3a.xn--11b5bs3a9aj6g/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0%A4%AA%E0%A5%83%E0%A4%B7%E0%A5%8D%E0%A4%A0
Run Code Online (Sandbox Code Playgroud)
这很好用,但显然不适合显示给最终用户.
为此,我想将此URI转换回其原始形式(非punycode,非百分比编码路径)
可寻址有display_uri
,但只转换主机:
nicer = normalized.display_uri.to_s
#=> http://??????.???????/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0%A4%AA%E0%A5%83%E0%A4%B7%E0%A5%8D%E0%A4%A0
Run Code Online (Sandbox Code Playgroud)
这看起来很有效:
display_s = Addressable::URI.parse(str).display_uri.to_s
pretty = Addressable::URI.unencode(display_s.force_encoding("ASCII-8BIT"))
Run Code Online (Sandbox Code Playgroud)
但是,该代码看起来不对(我不应该使用force_encoding
),我完全不相信它是正确的.
将整个URI转换为可供最终用户使用的东西的好方法是什么?("http://??????.???????/?????_?????"
)
存储规范化的URI即使是一个好主意,还是会产生我可能不知道的后果?
代码:https://gist.github.com/levinalex/6115764
我怎么转换这个:
"http://xn--p1b6ci4b4b3a.xn--11b5bs3a9aj6g/" +
"%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4" +
"%AF_%E0%A4%AA%E0%A5%83%E0%A4%B7%E0%A5%8D%E0%A4%A0"
Run Code Online (Sandbox Code Playgroud)
对此:
"http://??????.???????/?????_?????"
Run Code Online (Sandbox Code Playgroud) 我在Amazon EC2上的CoreOS实例上运行了kubernetes(0.15)集群
当我创建一个我想要公开访问的服务时,我目前将EC2实例的一些私有IP地址添加到服务描述中,如下所示:
{
"kind": "Service",
"apiVersion": "v1beta3",
"metadata": {
"name": "api"
},
"spec": {
"ports": [
{
"name": "default",
"port": 80,
"targetPort": 80
}
],
"publicIPs": ["172.1.1.15", "172.1.1.16"],
"selector": {
"app": "api"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以将这些IP添加到ELB负载均衡器并将流量路由到这些机器.
但为了实现这一点,我需要在我运行的所有服务中维护集群中所有机器的列表,这感觉不对.
目前推荐的解决方法是什么?
(我知道createExternalLoadBalancer
,但这似乎还不支持AWS)
C#有BitArray,C有位字段..我在Ruby核心中找不到等价物.谷歌向我展示了BitField
彼得库珀为此写过的课程.
我一直在阅读Jon Bentley的Programming Pearls,在尝试第一个处理BitMap排序的例子时,我需要一个比特数组的类型.我用过彼得的课
class BitMapSort
def self.sort(list, value_range_max)
bitmap = BitField.new(value_range_max)
list.each{|x| bitmap[x] = 1 }
sorted_list = []
0.upto(value_range_max-1){ |number|
sorted_list << number if (bitmap[number] == 1)
}
sorted_list
end
end
Run Code Online (Sandbox Code Playgroud)
在[0,10,000,000]范围内的一组1M唯一数字上运行它,产生了一些有趣的结果,
user system total real
bitmap 11.078000 0.015000 11.093000 ( 11.156250)
ruby-system-sort 0.531000 0.000000 0.531000 ( 0.531250)
quick-sort 21.562000 0.000000 21.562000 ( 21.625000)
Benchmark.bm(20){|x|
x.report("bitmap"){ ret = BitMapSort.sort(series, 10_000_000);}
x.report("ruby-system-sort"){ ret = series.sort; }
x.report("quick-sort"){ ret = QuickSort.sort( series, 0, series.length-1); }
}
Run Code Online (Sandbox Code Playgroud)
在10M位向量上,ruby的默认排序比1M BitField.set …