Ruby refinements subtleties

There is a pretty good documentation of the current implementation of refinements in ruby here: http://ruby-doc.org//core-2.2.0/doc/syntax/refinements_rdoc.html, but there are some strange corner cases. First, include module is orthogonal to using module (one include the instance method of module while the other activates the refinement). But there is a trick to include a refinement module its

Ruby改进了微妙之处

这里有一个相当不错的ruby细化实现文档:http://ruby-doc.org//core-2.2.0/doc/syntax/refinements_rdoc.html,但有一些奇怪的角落案例。 首先, include module与using module正交(一个包含using module的实例方法而另一个激活细化)。 但是有一个技巧来包含一个优化模块本身,请参阅更好的方法来将Ruby类转换为模块,而不是使用优化? def to_module(klass) Module.new do #note that we return the refinement mod

How to check if a svn command needs authentication

Is there any possible way to check if a SVN update needs authentication or not? Scenario : I have written a ruby GUI app which updates SVN repositories (from a static path) in a scheduled manner. This executes as a windows service. Also have tortoisesvn installed. In ruby when I execute svn update local_path_to_repository --username user --password password in my script then it works fine

如何检查一个svn命令是否需要验证

是否有任何可能的方法来检查SVN更新是否需要身份验证? 场景:我已经编写了一个ruby GUI应用程序,它以预定方式更新SVN存储库(从静态路径)。 这作为一个Windows服务来执行。 还有tortoisesvn安装。 在我执行的时候是红宝石 svn update local_path_to_repository --username user --password password在我的脚本中,然后它工作正常,因为我通过更新命令的用户名和密码。 但是,tortoisesvn在我第一次签出仓库时会保存

Why doesn't this work if in Ruby everything is an Object?

Considering that in the Ruby programming language everything is said to be an Object, I safely assumed that passing arguments to methods are done by reference. However this little example below puzzles me: $string = "String" def changer(s) s = 1 end changer($string) puts $string.class String => nil As you can see the original Object wasn't modified, I wish to know why , and also,

为什么这不工作,如果在Ruby中一切都是对象?

考虑到在Ruby编程语言中,所有东西都被认为是一个Object,我可以放心地认为将参数传递给方法是通过引用完成的。 然而,下面这个小例子使我感到困惑: $string = "String" def changer(s) s = 1 end changer($string) puts $string.class String => nil 正如你可以看到原始的Object没有被修改,我想知道为什么 ,以及我如何完成所需的行为,即。 让方法实际更改由其参数引用的对象。 Ruby的工作方式是按值传递和按

How to generate a random string in Ruby

I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z": value = ""; 8.times{value << (65 + rand(25)).chr} but it doesn't look clean, and it can't be passed as an argument since it isn't a single statement. To get a mixed-case string "a" .. "z" plus "A" .. "Z", I changed it to: val

如何在Ruby中生成一个随机字符串

我目前正在为“A”生成一个8个字符的伪随机大写字符串..“Z”: value = ""; 8.times{value << (65 + rand(25)).chr} 但它看起来并不干净,并且它不能作为参数传递,因为它不是一个单独的语句。 要获得混合大小写的字符串“a”..“z”加上“A”..“Z”,我将它改为: value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr} 但它看起来像垃圾。 有没有人有更好的方法? (0...8).map { (65 + rand(26)).chr

How to execute a Ruby script in Terminal?

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this? Just call: ruby your_program.rb or start

如何在终端中执行Ruby脚本?

我已经在我的Mac(Ruby,Rails,Homebrew,Git等)上设置了所需的一切,甚至还编写了一个小程序。 现在,我如何在终端中执行它? 我用Redcar编写了程序并将其保存为.rb,但我不知道如何通过终端执行它。 我想运行该程序,看看它是否真的有效。 我该怎么做呢? 只需调用: ruby your_program.rb 要么 用#!/usr/bin/env ruby启动你的程序, 通过运行chmod +x your_program.rb使您的文件可执行 并执行./your_program.

Terminal on the web browser?

Is there a way to put the terminal on the web browser instead, so I just fire up localhost:80 and then I'll have a terminal on it that I can use and whatever I execute it will execute on my local web server? And the outputs I want to be displayed on the web browser too, just like a normal Terminal. I'm using Ruby on Mac OS X/Ubuntu. 你可以使用shellinabox,这里有deb包。 There are sev

Web浏览器上的终端?

有没有办法将终端放在网络浏览器上,所以我只需启动localhost:80,然后我就可以使用一个终端,我可以使用它,而我执行的任何终端都将在我的本地Web服务器上执行? 我想要在Web浏览器上显示输出,就像普通的终端一样。 我在Mac OS X / Ubuntu上使用Ruby。 你可以使用shellinabox,这里有deb包。 有几个软件包允许你通过web浏览器使用ssh登录到计算机,包括anyterm和AjaxTerm。 看这里。

What constitutes a valid URI query parameter key?

I'm looking over Section 3.4 of RFC 3986 trying to understand what constitutes a valid URI query parameter key, but I'm not seeing a clear answer. The reason I'm asking is because I'm writing a Ruby class that composes a URI with query parameters. When a new parameter is added I want to validate the key. Based on experience, it seems like the key will be invalid if it requires

构成有效的URI查询参数密钥的是什么?

我正在查看RFC 3986的第3.4节,试图了解什么构成了有效的URI查询参数密钥,但我没有看到明确的答案。 我问的原因是因为我正在编写一个Ruby类,它构成了一个包含查询参数的URI。 当添加新参数时,我想验证密钥。 根据经验,如果它需要任何转义,似乎关键将是无效的。 我也应该说我打算验证关键。 我不确定如何验证这些数据,但我知道在任何情况下我都应该逃避这个价值。 建议表示赞赏。 在通过说Ruby Gem验证可能已经成

How to get a random number in Ruby

如何生成0到n之间的随机数字? Use rand(range) From Ruby Random Numbers: If you needed a random integer to simulate a roll of a six-sided die, you'd use: 1 + rand(6) . A roll in craps could be simulated with 2 + rand(6) + rand(6) . Finally, if you just need a random float, just call rand with no arguments. As Marc-André Lafortune mentions in his answer below (go upvote it), Ruby 1.9.2

如何在Ruby中获得一个随机数字

如何生成0到n之间的随机数字? 使用rand(range) 从Ruby随机数字: 如果你需要一个随机整数来模拟一个六面骰子的卷,你可以使用: 1 + rand(6) 。 掷骰子可以用2 + rand(6) + rand(6)来模拟。 最后,如果你只需要一个随机的浮点数,只需要调用rand而不需要参数。 正如Marc-AndréLafortune在他的回答中提到的(注意它),Ruby 1.9.2有它自己的Random类(Marc-André自己帮助调试,因此1.9.2的目标)。 例如,在这个需要

Why isn't the eigenclass equivalent to self.class, when it looks so similar?

I've missed the memo somewhere, and I hope you'll explain this to me. Why is the eigenclass of an object different from self.class ? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do attr_accessor symbol end end end My train of logic that equates the eigenclass with class.self is rather simple: class &

为什么特征类不等于self.class,看起来如此相似?

我错过了某处的备忘录,我希望你能向我解释这一点。 为什么对象的特征类与self.class不同? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do attr_accessor symbol end end end 我的一系列逻辑将特征类与class.self等同class.self很简单: class << self是一种声明类方法的方法,而不是实例方法。 这是def Foo.bar的快捷方

Can Ruby print out time difference (duration) readily?

Can Ruby do something like this? irb(main):001:0> start = Time.now => Thu Nov 05 01:02:54 -0800 2009 irb(main):002:0> Time.now - start => 25.239 irb(main):003:0> (Time.now - start).duration => "25 seconds" (the duration method doesn't exist now)... and similarly, report 23 minutes and 35 seconds 1 hour and 33 minutes 2 days and 3 hours (either report the whole duration

Ruby可以随时打印时间差(持续时间)吗?

Ruby可以做这样的事吗? irb(main):001:0> start = Time.now => Thu Nov 05 01:02:54 -0800 2009 irb(main):002:0> Time.now - start => 25.239 irb(main):003:0> (Time.now - start).duration => "25 seconds" (持续时间方法现在不存在)...并且同样地报告 23 minutes and 35 seconds 1 hour and 33 minutes 2 days and 3 hours (或者报告整个持续时间,多达几秒钟,或者报告多达2个数字和单位(如果