Tag: oop

关于Ruby <<运算符的澄清

我是Ruby的新手,我想知道<<运算符。 当我用Google搜索这个运算符时,它说这是一个二进制左移运算符给出了这个例子: a << 2将给出15这是1111 0000 但是,它在这段代码中似乎不是“二进制左移运算符”: class TextCompressor attr_reader :unique, :index def initialize(text) @unique = [] @index = [] add_text(text) end def add_text(text) words = text.split words.each { |word| do add_word(word) } end def add_word(word) i = unique_index_of(word) || add_unique_word(word) @index << i end def unique_index_of(word) @unique.index(word) end def add_unique_word @unique << word unique.size […]

如何自动创建目录中每个类的实例?

我如何在ruby中创建目录中每个文件中每个类的实例并将其作为数组提供? 先感谢您!

Square和Rectangleinheritance有什么问题?

我已经阅读了一些关于使Square成为Rectangle类的inheritance类的做法的一些文章,这说明它违反了LSP(Liskov替换原则)。 我仍然没有得到它,我在Ruby中做了一个示例代码: class Rectangle attr_accessor :width, :height def initialize(width, height) @width = width @height = height end end class Square < Rectangle def initialize(length) super(length, length) end def width=(number) super(number) @height = number end def height=(number) super(number) @width = number end end s = Square.new(100) s.width = 50 puts s.height 谁能告诉我它有什么问题?

使用不同的参数和默认值在Ruby中初始化类的最有效方法是什么?

我想有一个类和一些属性,您可以在初始化期间设置或使用其默认值。 class Fruit attr_accessor :color, :type def initialize(color, type) @color=color ||= ‘green’ @type=type ||=’pear’ end end apple=Fruit.new(red, apple)

inheritance如何在Ruby中工作?

根据Dave Thomas在关于Ruby对象模型的讨论中,Ruby中没有“类方法”。 方法的接收者是“类对象”还是“实例对象”之间只有区别。 class Dave def InstaceMethod ### will be stored in the current class (Dave) puts “Hi” end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end 默认情况下, ancestors方法不显示元类: class Dave class << self def metaclass ### A way to show the hidden eigenclass class […]

“内核”的目的是什么?

Kernel模块的目的是什么? 如果在Kernel上定义的所有内容都是在Object上定义的,并且没有像Kernel这样的模块,会发生什么变化? 当我想定义一个可以在任何对象上调用的方法时,我应该在Kernel或Object上定义它吗?

“猴子补丁”真的那么糟糕吗?

像Ruby和JavaScript这样的语言有开放类,允许你修改偶数核心类的接口,如数字,字符串,数组等。显然,这样做可能会使熟悉API的其他人感到困惑,但是有充分的理由要避免使用它们。 ,假设您正在添加到界面而不是更改现有行为? 例如,将一个Array.map实现添加到未实现ECMAScript第5版的Web浏览器(如果您不需要所有jQuery)可能会很好。 或者你的Ruby数组可能会受益于使用“inject”的“sum”方便方法。 只要更改被隔离到您的系统(例如,不是您发布用于分发的软件包的一部分),是否有充分的理由不利用此语言function?

Ruby模块中的私有类(不是类方法)?

我是Ruby的新手(熟悉Python,C ++和C)。 我需要创建一个只能由模块中的其他类和方法使用的类。 在Python中,我只是将其称为__classname。 我在C ++中使用了一个空的typedef。 我如何在Ruby中执行此操作(或者我是在咆哮错误的树而不是以“Ruby方式”执行此操作?)

attr vs attr_accessor

在Ruby中,实例变量有四种不同的getter和setter方法, attr , attr_reader , attr_writer和attr_accessor 。 问题是,在Ruby attr :dilithium attr_reader :dilithium ,与attr_accessor :dilithium相同,并且与attr_accessor :dilithium相同,如果传递了一个额外的参数true? 那就是说 class Enterprise attr :dilithium, true 相同 class Enterprise attr_accessor :dilithium 两个函数attr和attr_accessor多余还是多余?

Ruby动态类。 如何修复“警告:来自toplevel的类变量访问”

我正在尝试编写一个程序,根据从文件中读取的配置动态定义ruby类。 我知道我可以使用Class.new来做到这一点。 这是一个示例程序: x = [1,2,3] Test = Class.new do @@mylist = x def foo puts @@mylist end end Test.new.foo 当我运行它时,我得到以下输出(与ruby 1.9.3p0一起运行): c:/utils/test.rb:4:警告:来自toplevel的类变量访问 c:/utils/test.rb:7:警告:来自toplevel的类变量访问 1 2 3 有谁知道导致这些警告的原因以及如何摆脱这些警告? 我已经尝试更换线路了 @@mylist = x 有了这个 class_variable_set(:@@mylist, x) 但是当我这样做时,我得到了这个错误: c:/utils/test.rb:7:警告:来自toplevel的类变量访问 c:/utils/test.rb:7:在`foo’中:未初始化的类变量@@ mylist in Object(NameError) 来自c:/utils/test.rb:11:in” 提前致谢!