如何在不同文件中使用属性?

时间:2018-10-23 21:40:39

标签: ruby

我正在完成我的课程,一个简单的图书馆系统。

我正面临如下问题:

book.rb

class Book
  attr_accessor :title, :author, :language, :classification, :isbn, :book_id, :borrow_status



  def initialize(title, author, language, classification, isbn, book_id, borrow_status)
    @title = title
    @author = author
    @language = language
    @classification = classification
    @isbn = isbn
    @book_id = book_id
    @borrow_status = borrow_status
  end

  def bookid
    @book_id
  end

  def booklist
    @title = @title.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @author = @author.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @language = @language.capitalize!
    @isbn.to_s
    @book_id.to_s

    {
        "Title" => @title,
        "Author" => @author,
        "Language" => @language,
        "Classification" => @classification,
        "ISBN" => @isbn,
        "Book ID" => @book_id,
        "Status" => @borrow_status,
    }
  end


end

user.rb

require_relative 'book.rb'

class User
  attr_accessor :name, :address, :gender, :age, :id, :borrow

  def initialize(name, address, gender, age, id, borrow)
    @name = name
    @address = address
    @gender = gender
    @age = age
    @id = id
    @borrow = borrow
  end

  def userlist
    @name = @name.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @address = @address.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @age.to_s
    @id.to_s

    if @borrow.nil?
      puts "nothing"
    elsif
      puts #I wish I can put book's name here, if I entered correct @borrow. eg.,for user3's @borrow=4(in top.rb), user3.@borrow=book4.@book_id, then print @name of book4
    else
      puts "error"
    end

问题出在if.elsif-else循环中的user.rb内部,即

  

如果我输入正确的@borrow,我希望可以在此处输入书名。   例如,对于user3的@ borrow = 4(在top.rb中),user3。@ borrow = book4。@ book_id,   然后打印@ book4的名称

任何解决方案?

1 个答案:

答案 0 :(得分:0)

#user.rb
require_relative 'book.rb'

class User
  attr_accessor :name, :address, :gender, :age, :id, :borrow

  def initialize(name, address, gender, age, id, borrow)
    @name = name
    @address = address
    @gender = gender
    @age = age
    @id = id
    @borrow = borrow
  end

  def userlist
    @name = @name.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @address = @address.split(/ |\_|\-/).map(&:capitalize).join(" ")
    @age.to_s
    @id.to_s

    if borrow.nil?
      puts "nothing"
    elsif borrow
      puts borrow.title
    else
      puts "error"
    end
  end
end

@book = Book.new('Book1', 'Auth1', 'EN', 'fiction', 'isb123', 1, 'borrowed')
@user = User.new('Mr Foo', '123 st', 'male', 23, 123, nil)
@user.borrow=@book
@user.userlist 
#=>Book1

但是您可能想发送一个书本数组,然后需要修改方法以处理书本

def userlist
  @name = @name.split(/ |\_|\-/).map(&:capitalize).join(" ")
  @address = @address.split(/ |\_|\-/).map(&:capitalize).join(" ")
  @age.to_s
  @id.to_s

  if borrow.nil?
    puts "nothing"
  elsif borrow.length > 0 # checks if anythint in the array
    borrow.each{ |b| puts b.title }
  else
    puts "error"
  end
end