class << self在Ruby中做什么? First, the class << foo syntax opens up foo 's singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object. a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" Now, to answer the ques
class << self在Ruby中做什么? 首先, class << foo语法打开了foo的单例类(特征类)。 这使您可以专门化在该特定对象上调用的方法的行为。 a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" 现在,回答这个问题: class << self打开self的单例类,以便可以为当前self对象(类或模
如何在Ruby中将字符串转换为小写或大写? Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase : "hello James!".downcase #=> "hello james!" Similarly, upcase capitalizes every letter and capitalize capitalizes the first letter of the string but lowercases the rest: "hello James!".upcase #=> "HELLO JAMES!" "hello James!".capitalize #=> "
如何在Ruby中将字符串转换为小写或大写? Ruby有几种改变字符串大小写的方法。 要转换为小写,使用downcase : "hello James!".downcase #=> "hello james!" 类似地, upcase每一个字母大写,并capitalize字母的第一个字母,但将其余的字母upcase : "hello James!".upcase #=> "HELLO JAMES!" "hello James!".capitalize #=> "Hello james!" "hello James!".titleize #=> "Hello James!" 如果你想
I remember something about procs being allowed in case statements in Ruby 2.0, but I can't google it. I tried checking Ruby 2.0.0 NEWS and How to write a switch statement in Ruby. I also visited http://ruby-doc.org , but the link it had for keywords was for Ruby 1.9, not Ruby 2.0. Are procs allowed in case statements? Yes. 2.0.0p0 :001> lamb = ->(x){ x%2==1 } #=> #<Proc:0x
我记得在Ruby 2.0的case语句中允许使用procs,但我无法谷歌它。 我试着检查Ruby 2.0.0 NEWS和如何在Ruby中编写switch语句。 我还访问了http://ruby-doc.org,但是它的关键字链接是用于Ruby 1.9,而不是Ruby 2.0。 case语句中允许使用procs吗? 是。 2.0.0p0 :001> lamb = ->(x){ x%2==1 } #=> #<Proc:0x007fdd6a97dd90@(irb):1 (lambda)> 2.0.0p0 :002> case 3; when lamb then p(:yay); end :yay #=
I am trying to learn how to use the net-ssh gem for ruby. I want to execute the commands below, after I login to the directory - /home/james. cd / pwd ls When I do this with putty, it works and i can see a list of directories. But, when I do it with ruby code, it does not give me the the same output. require 'rubygems' require 'net/ssh' host = 'server' user = 'james' pass = 'password123'
我正在尝试学习如何使用net-ssh gem进行ruby。 在登录到目录 - / home / james后,我想执行下面的命令。 cd / pwd ls 当我用腻子做到这一点时,它可以工作,我可以看到目录列表。 但是,当我用ruby代码做它时,它不会给我相同的输出。 require 'rubygems' require 'net/ssh' host = 'server' user = 'james' pass = 'password123' def get_ssh(host, user, pass) ssh = nil begin ssh = Net::SSH.start(
如何将Unix时间戳记(自epoch以来的秒数)转换为Ruby DateTime? DateTime.strptime can handle seconds since epoch. The number must be converted to a string: require 'date' DateTime.strptime("1318996912",'%s') Sorry, brief moment of synapse failure. Here's the real answer. require 'date' Time.at(seconds_since_epoch_integer).to_datetime Brief example (this takes into account the current syst
如何将Unix时间戳记(自epoch以来的秒数)转换为Ruby DateTime? DateTime.strptime可以处理自纪元以来的秒数。 该数字必须转换为字符串: require 'date' DateTime.strptime("1318996912",'%s') 对不起,突然失败的短暂时刻。 这是真正的答案。 require 'date' Time.at(seconds_since_epoch_integer).to_datetime 简单的例子 (这考虑到了当前的系统时区): $ date +%s 1318996912 $ irb ruby-1.9.2-p180 :001 > r
I am trying to get a basic hello world application going using XCode and Interface Builder. However, in Interface Builder I can't see my outlets to wire things up with. I go to the connections tab of the objects inspector pane and it says "New Referencing Outlet". I am wondering if my code is wrong. Here it is class HelloWorldController attr_accessor :hello_label, :hello_
我正在尝试使用XCode和Interface Builder来获得基本的Hello World应用程序。 但是,在界面生成器中,我看不到我的网点连接。 我转到对象检查器窗格的连接选项卡,并显示“New Referencing Outlet”。 我想知道如果我的代码是错误的。 这里是 class HelloWorldController attr_accessor :hello_label, :hello_button, :hello def awakeFromNib @hello = true end def changeLabel(sender)
I've found this method here. start = DateTime.now sleep 15 stop = DateTime.now #minutes puts ((stop-start) * 24 * 60).to_i hours,minutes,seconds,frac = Date.day_fraction_to_time(stop-start) I have the following error: `<main>': private method `day_fraction_to_time' called for Date:Class (NoMethodError) I've checked /usr/lib/ruby/1.9.1/date.rb and I've found it:
我在这里找到了这个方法。 start = DateTime.now sleep 15 stop = DateTime.now #minutes puts ((stop-start) * 24 * 60).to_i hours,minutes,seconds,frac = Date.day_fraction_to_time(stop-start) 我有以下错误: `<main>': private method `day_fraction_to_time' called for Date:Class (NoMethodError) 我检查了/usr/lib/ruby/1.9.1/date.rb,我发现它: def day_fraction_to_time(fr) # :nodoc:
Setup with cucumber, capybara and selenium but some scenarios works only randomly. Running ruby 1.8.6 on rvm rails 2.3.8 selenium pops open firefox 3.6 I have tried to add this with no luck: with_scope(selector) do click_button(button) selenium.wait_for_page_to_load end The error output is sometimes: > Given I am logged in and have created newsletter and subscribers
安装黄瓜,水豚和硒,但一些情况只能随机使用。 运行 红宝石1.8.6上rvm 导轨2.3.8 硒弹出打开Firefox 3.6 我试图添加这个没有运气: with_scope(selector) do click_button(button) selenium.wait_for_page_to_load end 错误输出有时是: > Given I am logged in and have created newsletter and subscribers # features/step_definitions/newsletter_send_steps.rb:108 e
Ruby seems to use /bin/sh as the shell interpreter, which on a *nix machine doesn't understand /bin/bash commands such as pushd . This is what irb tells me: 1.9.3-p327 :011 > `pushd /tmp; echo foo` sh: 1: pushd: not found => "foon" On OSX, /bin/sh is an alias for bash, so the above snippet runs fine there. Is there a way to force Ruby on a *nix machine to use /bin/bash for interpret
Ruby似乎使用/bin/sh作为shell解释器,它在* nix机器上不理解/bin/bash命令,例如pushd 。 这是irb告诉我的: 1.9.3-p327 :011 > `pushd /tmp; echo foo` sh: 1: pushd: not found => "foon" 在OSX上, /bin/sh是bash的别名,所以上面的代码片段在那里运行良好。 有没有办法强制Ruby在* nix机器上使用/bin/bash来解释shell命令? 在Ruby中使用类似pushd Bash命令毫无意义,因为这些命令会影响Bash的Bash解释器的内部
Ryan Davis's Ruby QuickRef says (without explanation): Don't rescue Exception. EVER. or I will stab you. Why not? What's the right thing to do? TL;DR : Use StandardError instead for general exception catching. When the original exception is re-raised (eg when rescuing to log the exception only), rescuing Exception is probably okay. Exception is the root of Ruby's excep
瑞安戴维斯的红宝石QuickRef说(没有解释): 不要拯救例外。 EVER。 否则我会刺伤你。 为什么不? 什么是正确的做法? TL; DR :使用StandardError来代替一般异常捕获。 当原始异常被重新提出时(例如,当救援只记录异常时),拯救Exception可能没问题。 Exception是Ruby异常层次的根源,所以当你rescue Exception时,你可以从一切中解救出来,包括诸如SyntaxError , LoadError和Interrupt类的子类。 救援Interr