Prevent duplicate objects in Ruby

This isn't exactly a singleton, but it's close, so I imagine it's common. I have a class (Foo) in which instances correspond to external data structures with unique IDs. I want to ensure that no two instances of Foo can have the same ID - if a constructor is called with the same id value, the original Foo instance with that ID, and all the other values are simply updated. In other

在Ruby中防止重复的对象

这不完全是单身,但它很接近,所以我认为这很常见。 我有一个类(Foo),其中实例对应于具有唯一ID的外部数据结构。 我想确保没有两个Foo实例可以拥有相同的ID - 如果使用相同的ID值调用构造函数,使用该ID的原始Foo实例,并且所有其他值都会被简单更新。 换句话说,就像: class Foo def initialize(id, var1, var2) if Foo.already_has? id t = Foo.get_object(id) t.var1 = var1 t.var2 = var2

About class definition in Ruby

Recently, I was investigating into some details about classes in Ruby, and was confused by class definition. In Ruby, the class definition is as follows, class A def self.my_method end end and it's the same as class A class << self def my_method end end end then I was confused. For the 1st case, self can be regarded as a pointer to currently using

关于Ruby中的类定义

最近,我正在研究关于Ruby类的一些细节,并且被类定义弄糊涂了。 在Ruby中,类的定义如下, class A def self.my_method end end 和它一样 class A class << self def my_method end end end 然后我很困惑。 对于第一种情况,self可以被看作是当前使用对象的指针,而当前上下文的类是A.并且递归地执行查找方法。 但我的问题是, def做什么? 它如何改变当前的对象和上下文? 第

Ruby operator method calls vs. normal method calls

I'm wondering why calls to operator methods don't require a dot? Or rather, why can't normal methods be called without a dot? Example class Foo def +(object) puts "this will work" end def plus(object) puts "this won't" end end f = Foo.new f + "anything" # "this will work" f plus "anything" # NoMethodError: undefined method `plus' for main:Object The implementati

Ruby运算符方法调用与常规方法调用

我想知道为什么调用操作符方法不需要点? 或者说,为什么普通的方法不能没有点的调用呢? 例 class Foo def +(object) puts "this will work" end def plus(object) puts "this won't" end end f = Foo.new f + "anything" # "this will work" f plus "anything" # NoMethodError: undefined method `plus' for main:Object 这个实现不需要额外的复杂性来允许通用定义新的操作符。 相反,Ruby有一个Yac

ruby how to define writer method with "<<"

I have a skeleton class: class Foo def bar # returns some sort of array end end but how can one add the 'writer' method to 'bar' so to enable the Array#push behavior? Foo.new.bar<<['Smile'] _.bar #=> ['Smile'] EDITED: I should expand my question further. There are two classes. Foo, and Bar, much like the ActiveRecord has_many relation where Foo has_many Ba

红宝石如何用“<<”来定义写作者的方法

我有一个骨架类: class Foo def bar # returns some sort of array end end 但是如何将'writer'方法添加到'bar'以启用Array#推送行为? Foo.new.bar<<['Smile'] _.bar #=> ['Smile'] 编辑:我应该进一步扩展我的问题。 有两个类。 Foo和Bar,非常像ActiveRecord has_many关系,其中Foo has_many Bars 但我实际上是在Foo的一个方法里存储Bar的id。 我将该方法命名为bar_ids 所以

Ruby access to symbol "invoked by"

I want to (efficiently) get the symbol an aliased method is called with at runtime. A direct efficient access to a stack frame object of some sort to get it would be the fantasy. ie: class Foo def generic_call(*args) puts("generic_call() was called by using #{???}") end alias :specific_call1 :generic_call alias :specific_call2 :generic_call end Foo.new.specific_call1

Ruby访问符号“由......调用”

我想(高效地)获取符号,在运行时调用别名方法。 直接高效地访问某种堆栈框架对象以获得它将是幻想。 即: Foo级 def generic_call(* args) puts(“generic_call()通过使用#{???}”调用)) 结束 别名:specific_call1:generic_call 别名:specific_call2:generic_call 结束 Foo.new.specific_call1 Foo.new.specific_call2 我想要的结果 generic_call()是通过使用specific_call1()调用的

What exactly is the singleton class in ruby?

It seems as if I'm missing the point or misunderstanding the significance of the singleton class in Ruby. I've heard and read about it in many ways—some more complicated than others—but I'm left more confused as to what it is. Is it a class in and of itself? Is it the reason why all objects belong to "class?" The concept is fuzzy , but I believe it has something to do wi

Ruby中的单例类是什么?

看起来好像我错过了这个观点或者误解了Ruby中单例类的重要性。 我听过并且以多种方式阅读过它 - 有些比其他更复杂 - 但我对它是什么感到困惑。 它本身是一个阶级吗? 这是所有对象属于“课堂”的原因吗? 这个概念很模糊 ,但我相信它与我为什么可以定义一个类方法(class foo; def foo.bar ...)有关。 那么:Ruby中的单例类是什么? 首先是一个小的定义: 单例方法是仅为单个对象定义的方法。 例: irb(main):001:0>

Declaring variables in .rb file result unwanted output

I am following chapter 2 of Understanding Computation to build a simple Ruby interpreter: At some point after Class Assign , Variable and Number are defined (which I think is actually irrelevant), I do this in irb irb(main):001:0> statement = Assign.new(:x, Add.new(Variable.new(:x), Number.new(1))) => <<x = x + 1>> irb(main):002:0> environment = { x: Number.new(2) }

在.rb文件中声明变量会导致不需要的输出

我遵循Understanding Computation的第2章构建一个简单的Ruby解释器: 在Class Assign之后的某个时刻,定义了Variable和Number (我认为这实际上是不相关的),我在irb irb(main):001:0> statement = Assign.new(:x,Add.new(Variable.new(:x),Number.new(1))) => <<x = x + 1>> irb(main):002:0> environment = {x:Number.new(2)} => {:x=><<2>>} irb

How to concatenate byte in Ruby

In Programming Ruby Second Edition (1.8), page 124, there is an example said str2=""; str2 << 1 << 2 << 3 ; will produce "010203". I've tried it in irb and have got this result, but just one time. When I tried to do it again, and again, and it never occurred. Can anyone pls tell me why? BTW, my environment is in ruby 2.0.0p353 (2013-11-22 revisi

如何连接Ruby中的字节

在编程红宝石第二版(1.8),第124页,有一个例子说 STR2 = “”; str2 << 1 << 2 << 3; 会产生“ 001 002 003”。 我已经在irb上试了一下,得到了这个结果,但只有一次。 当我试图再次这样做,并且再也没有发生过。 任何人都可以告诉我为什么? 顺便说一句,我的环境是在红宝石2.0.0p353(2013-11-22修订版43784)[x86_64-linux] -> irb irb(main):001:0> str2 = "" => "" irb(ma

Calculate time duration into percentages of a whole month

I want to calculate the duration of time from a specific start and end time and output it in hours/minuts/seconds and how long that duration is compared to the whole month in percent. Sometimes the total duration during a month is in multiple duration blocks. Here is an example: Thinking out load with Ruby: Start = Date.new(2011-09-01 00:00:00) #24h clock - start of month End = Date.new(20

计算整个月的百分比时间

我想从特定的开始和结束时间计算持续时间,并以小时/分钟/秒为单位输出,并将持续时间与整个月的百分比进行比较。 有时候一个月内的总时长是在多个时间段内。 这里是一个例子: 考虑用Ruby加载: Start = Date.new(2011-09-01 00:00:00) #24h clock - start of month End = Date.new(2011-09-31 24:00:00) # end of month Duration1 = Date.new(2011-09-03 12:30:00) Date.new(2011-09-11 12:30:00) #From - to Duration2

How to break out from a ruby block?

Here is Bar#do_things : class Bar def do_things Foo.some_method(x) do |x| y = x.do_something return y_is_bad if y.bad? # how do i tell it to stop and return do_things? y.do_something_else end keep_doing_more_things end end And here is Foo#some_method : class Foo def self.some_method(targets, &block) targets.each do |target| begin r

如何从红宝石块中跳出来?

这里是Bar#do_things : class Bar def do_things Foo.some_method(x) do |x| y = x.do_something return y_is_bad if y.bad? # how do i tell it to stop and return do_things? y.do_something_else end keep_doing_more_things end end 这里是Foo#some_method : class Foo def self.some_method(targets, &block) targets.each do |target| begin r = yiel