calling another method in super class in ruby

class A def a puts 'in #a' end end class B < A def a b() end def b # here i want to call A#a. end end class B < A alias :super_a :a def a b() end def b super_a() end end There's no nice way to do it, but you can do A.instance_method(:a).bind(self).call , which will work, but is ugly. You could even define your own method in Object to a

在ruby中调用超类的另一种方法

class A def a puts 'in #a' end end class B < A def a b() end def b # here i want to call A#a. end end class B < A alias :super_a :a def a b() end def b super_a() end end 没有很好的方法去做,但你可以做A.instance_method(:a).bind(self).call ,它可以工作,但很丑。 你甚至可以在Object中定义你自己的方法来像java中的super一样工作: class SuperProx

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark ( ? ) that ask a question like include? that ask if the object in question is included, this then returns a true/false. But why do some methods have exclamation marks ( ! ) where others don't? What does it mean? In general, methods that end in ! indicate that the method will modify the object it's called on . Ruby calls these as " da

为什么在Ruby方法中使用感叹号?

在Ruby中,有些方法有一个问号( ? ),提出一个像include?这样的问题include? 询问是否包含所讨论的对象,然后返回true / false。 但为什么有些方法在其他人不知道的地方有惊叹号( ! )? 这是什么意思? 一般来说,以!结尾的方法! 表明该方法将修改它所调用的对象 。 Ruby把这些称为“ 危险的方法 ”,因为它们改变了别人可能参考的状态。 这是一个简单的字符串示例: foo = "A STRING" # a string called foo foo.d

Can I invoke an instance method on a Ruby module without including it?

Background: I have a module which declares a number of instance methods module UsefulThings def get_file; ... def delete_file; ... def format_text(x); ... end And I want to call some of these methods from within a class. How you normally do this in ruby is like this: class UsefulWorker include UsefulThings def do_work format_text("abc") ... end end Problem include U

我可以在不包含它的情况下调用Ruby模块上的实例方法吗?

背景: 我有一个模块,它声明了许多实例方法 module UsefulThings def get_file; ... def delete_file; ... def format_text(x); ... end 我想从一个班级中调用其中的一些方法。 你通常如何在ruby中这样做是这样的: class UsefulWorker include UsefulThings def do_work format_text("abc") ... end end 问题 include UsefulThings带来所有从方法UsefulThings 。 在这种情况下,我只需要forma

Hidden features of Ruby

Continuing the "Hidden features of ..." meme, let's share the lesser-known but useful features of Ruby programming language. Try to limit this discussion with core Ruby, without any Ruby on Rails stuff. See also: Hidden features of C# Hidden features of Java Hidden features of JavaScript Hidden features of Ruby on Rails Hidden features of Python (Please, just one hid

Ruby的隐藏功能

继续“隐藏的...特性”,让我们分享Ruby编程语言鲜为人知的但有用的特性。 试着限制这个讨论与核心Ruby,没有任何Ruby on Rails的东西。 也可以看看: C#隐藏的功能 Java的隐藏功能 JavaScript的隐藏功能 Ruby on Rails的隐藏功能 Python的隐藏功能 (请每个答案只有一个隐藏功能。) 谢谢 从Ruby 1.9 Proc#===是Proc#调用的别名,这意味着Proc对象可以在case语句中使用,如下所示: def multiple_of(factor)

Having problems with native extensions.

I'm trying to install the curb gem on my Debian machine but I'm having a hard time building the gem with native extensions. I'm using RVM. Just a list of things I'm using: debian bundler rvm ruby-1.9.2-p180 curb I'm told I have to install the following packages and I've done so: sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev When I go

本机扩展存在问题。

我试图在我的Debian机器上安装遏制宝石,但我很难用本机扩展来构建宝石。 我正在使用RVM。 只是我正在使用的东西的列表: Debian的 捆绑 RVM 红宝石1.9.2-P180 抑制 我被告知我必须安装下列软件包,我已经这样做了: sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev 当我继续这样做时: 宝石安装路边 Building native extensions. This could take a while... ERROR: Error install

How do I get Ruby to parse time as if it were in a different time zone?

I'm parsing something like this: 11/23/10 23:29:57 which has no time zone associated with it, but I know it's in the UTC time zone (while I'm not). How can I get Ruby to parse this as if it were in the UTC timezone? 在解析它之前,您可以将UTC时区名称追加到字符串中: require 'time' s = "11/23/10 23:29:57" Time.parse(s) # => Tue Nov 23 23:29:57 -0800 2010 s += " UTC" Time.parse(s)

我如何让Ruby解析时间,就好像它在不同的时区一样?

我正在解析这样的事情: 11/23/10 23:29:57 它没有与它关联的时区,但我知道它在UTC时区(而我不是)。 我怎样才能让Ruby把它解析成好像它在UTC时区? 在解析它之前,您可以将UTC时区名称追加到字符串中: require 'time' s = "11/23/10 23:29:57" Time.parse(s) # => Tue Nov 23 23:29:57 -0800 2010 s += " UTC" Time.parse(s) # => Tue Nov 23 23:29:57 UTC 2010 如果您使用Rails,则可以使用ActiveSupport::TimeZ

How to install therubyracer gem on 10.10 Yosemite?

I don't manage to install therubyracer gem on Yosemite 10.10. Here is the log: 11:53 $ gem install libv8 -v '3.16.14.3' -- --with-system-v8 Building native extensions with: '--with-system-v8' This could take a while... Successfully installed libv8-3.16.14.3 Parsing documentation for libv8-3.16.14.3 Installing ri documentation for libv8-3.16.14.3 Done installing documentation for libv8

如何在10.10优胜美地上安装therubyracer宝石?

我无法在优胜美地10.10上安装therubyracer gem。 这里是日志: 11:53 $ gem install libv8 -v '3.16.14.3' -- --with-system-v8 Building native extensions with: '--with-system-v8' This could take a while... Successfully installed libv8-3.16.14.3 Parsing documentation for libv8-3.16.14.3 Installing ri documentation for libv8-3.16.14.3 Done installing documentation for libv8 after 0 seconds 1 gem in

Unable to rescue from Redis connection refusal

I am attempting to write a function which tries to connect to Redis using default TCP settings, and if that fails, tries to connect to Redis through a unix socket. My intent is to have a single connection script that works on all my systems, some of which use TCP and others which use sockets. However, I can't seem to rescue from the failed TCP connection. Here is my test script. require

无法从Redis连接拒绝中解救

我试图编写一个函数,它尝试使用默认TCP设置连接到Redis,如果失败,则尝试通过unix套接字连接到Redis。 我的意图是在我的所有系统上都有一个连接脚本,其中一些使用TCP和其他使用套接字的连接脚本。 但是,我似乎无法从失败的TCP连接中解救出来。 这是我的测试脚本。 require "redis" def r begin $redis ||= Redis.new rescue $redis = Redis.new(:path => "/tmp/redis.sock") end end puts "You have

Reuse Cucumber steps

I want to reuse some Cucumber steps but can't seem to find the right way. I want to write a step like: Given /^I login with (.*) credentials$/ |type| # do stuff with type being one of "invalid" or "valid" end But then have another step like: Given /^I login successfully$ # call "Given I login with valid credentials" end So in testing user authentication I can use the former, but mos

重复使用黄瓜步骤

我想重用一些黄瓜步骤,但似乎无法找到正确的方法。 我想写一个像这样的步骤: Given /^I login with (.*) credentials$/ |type| # do stuff with type being one of "invalid" or "valid" end 但是,接下来会有另一个步骤: Given /^I login successfully$ # call "Given I login with valid credentials" end 所以在测试用户身份验证时我可以使用前者,但大多数其他地方,我可以使用后者,而实际上并不需要重新编码

Initialize': getaddrinfo: Name or service not known (SocketError

I ve downloading cookbooks from object storage which has link https, require 'open-uri' open("/home/admini/Desktop/apache2.tgz","wb") do |write_file| open("https://region-a.geo-1.objects.hpcloudsvc.com:443/v1/68342917034742/cookbooks/marketplace/production/apache2.tar","rb") do |read_file| write_file.write(read_file.read) end end it gets downl

初始化':getaddrinfo:名称或服务未知(SocketError

我已经从具有链接https的对象存储下载食谱, 需要'open-uri'打开(“/ home / admini / Desktop / apache2.tgz”,“wb”)do | write_file | 打开(“https://region-a.geo-1.objects.hpcloudsvc.com:443/v1/68342917034742/cookbooks/marketplace/production/apache2.tar”,“rb”)do | read_file | write_file.write(read_file.read)结束 它为某些提供商获得下载,但对于某些提供商而言,它无法获得下载,如下所示