简体   繁体   English

Ruby类如何工作

[英]How Ruby classes work

This is an exercise from the odin project where they give you a file with the test specifications and you have to write the program in order to pass them. 这是odin项目的一项练习,他们给您一个包含测试规范的文件,您必须编写程序才能通过它们。

The program is working, but I've just started Ruby classes and I do not understand why doesn't work if I do a modification that I'll show: 该程序正在运行,但是我刚刚启动了Ruby类,并且我不明白为什么我做一个修改后显示为什么不起作用:

Rspec 规格

describe Book do

  before do
    @book = Book.new
  end

  describe 'title' do
    it 'should capitalize the first letter' do
      @book.title = "inferno"
      @book.title.should == "Inferno"
    end

    it 'should capitalize every word' do
      @book.title = "stuart little"
      @book.title.should == "Stuart Little"
    end

    describe 'should capitalize every word except...' do
      describe 'articles' do
        specify 'the' do
          @book.title = "alexander the great"
          @book.title.should == "Alexander the Great"
        end

        specify 'an' do
          @book.title = "to eat an apple a day"
          @book.title.should == "To Eat an Apple a Day"
        end
      end

      specify 'conjunctions' do
        @book.title = "war and peace"
        @book.title.should == "War and Peace"
      end

      specify 'the first word' do
        @book.title = "the man in the iron mask"
        @book.title.should == "The Man in the Iron Mask"
      end
    end
  end
end

Code

class Book
  def title
    @title
  end

  def title=(title)
    @title = titlieze(title)
  end

  private
  def titlieze(title)
    stop_words = %w(and in the of a an)
    title.capitalize.split.map{|w| stop_words.include?(w) ? w : w.capitalize}.join(' ')
  end

end

Why if I write my code like this 为什么要这样写代码

class Book

  def title=(title)
    @title = titlieze(title)
    @title
  end

  private
  def titlieze(title)
    stop_words = %w(and in the of a an)
    title.capitalize.split.map{|w| stop_words.include?(w) ? w : w.capitalize}.join(' ')
  end

end

then I get this error: 然后我得到这个错误:

Failure/Error: expect(@book.title).to eq("Inferno")

     NoMethodError:
       undefined method `title' for #<Book:0x000000017bd0a8 @title="Inferno">
       Did you mean?  title=

But the method "title" is defined no? 但是方法“ title”是否定义为否? why it says it is not? 为什么说不是?

Because def title=(title) defines setter method named title= to assign a value to the @title variable. 因为def title=(title)定义了名为title= setter方法,以将值分配给@title变量。 And it is important to note that the = is part of the method name. 重要的是要注意=是方法名称的一部分。

Whereas the specifications complain about a missing getter method named title (without a = ). 规范抱怨缺少名为title getter方法(没有= )。

Your second version is missing that title getter method for the @title variable. 您的第二个版本缺少@title变量的title getter方法。 You may just use the attr_reader macro to add such a getter method: 您可以只使用attr_reader宏添加这样的getter方法:

class Book
  attr_reader :title

  # ...
end

attr_reader :title is just a shortcut that generates a method like in your first version: attr_reader :title只是一个快捷方式,它会生成类似于您的第一个版本中的方法:

def title
  @title
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM