这是我的带有哈希值的红宝石文件:
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
现在,我已经有五个键值对用于此哈希,它们位于名为 top.rb 的红宝石文件中:
$books1 = Book.new("lonely planet: ireland","damian harper","english","tourism",9781786574459,1,"available")
$books2 = Book.new("ninteen eighty four","george orwell","english","literature",9781374677817, 2,"available")
$books3 = Book.new("japanese in 30 days","naomi ono","japanese","education",9787928365729,3,"available")
$books4 = Book.new("brand famous: how to get everyone talking about your business","linzi boyd","english","business",9780857084903,4,"borrowed")
$books5 = Book.new("SQL in 10 mins","ming zhong, xiaoxia liu","chinese","hi tech",9787115313980,5,"unavailable")
并使用以下方法输出结果:
def status
bookstatus = gets.chomp.to_s
if bookstatus == "status"
puts "Status:" + "\n" + "#{$books1.booklist[:Book_ID]}: #{$books1.booklist[:Title]}: #{$books1.booklist[:Status]}"
puts "#{$books2.booklist[:Book_ID]}:#{$books2.booklist[:Title]}: #{$books2.booklist[:Status]}"
puts "#{$books3.booklist[:Book_ID]}:#{$books3.booklist[:Title]}: #{$books3.booklist[:Status]}"
puts "#{$books4.booklist[:Book_ID]}:#{$books4.booklist[:Title]}: #{$books4.booklist[:Status]}"
puts "#{$books5.booklist[:Book_ID]}:#{$books5.booklist[:Title]}: #{$books5.booklist[:Status]}"
else
puts "error"
end
end
现在,我将添加更多值,让用户输入书籍的信息(例如title = gets.chomp.to_s),并为添加的书籍创建新的键值对
据我所知,将新的键值对添加到ruby如下:
my_hash = {:a => 5}
my_hash[:key] = "value"
但是,book.rb中的哈希没有任何名称,我试图给它起一个类似名称
book_list = {
:Title => @title,
:Author => @author,
:Language => @language,
:Classification => @classification,
:ISBN => @isbn,
:Book_ID => @book_id,
:Status => @borrow_status,
}
它将输出错误。
我的问题是,我想知道如何向没有名称的红宝石文件中的哈希添加新的键值对? < / p>
谢谢。
答案 0 :(得分:0)
将booklist方法更改为此将解决您的直接问题:
SELECT uos.uid,
uos.value,
uos.action_key
FROM (SELECT action_key,
Max(createddate) AS maxcreateddate
FROM usersoptstatus
WHERE uid = 1607
GROUP BY action_key) A
INNER JOIN usersoptstatus AS uos
ON uos.action_key = A.action_key AND
uos.createddate = A.maxcreateddate
WHERE uos.uid = 1607
ORDER BY uos.id DESC;
现在您可以 def booklist
@booklist ||= {
:Title => @title.split(/ |\_|\-/).map(&:capitalize).join(" "),
:Author => @author.split(/ |\_|\-/).map(&:capitalize).join(" "),
:Language => @language.capitalize,
:Classification => @classification,
:ISBN => @isbn.to_s,
:Book_ID => @book_id.to_s,
:Status => @borrow_status,
}
end
了。
您问题中的代码有很多“你好,世界!”级别的错误,看来您似乎仍然需要了解一些Ruby基础知识,例如使用变量,booklist[:Xyz] = 'xyz'
,@isbn
,isbn
,{{1} }或$isbn
等
网上有比我准备好的更好的“学习红宝石”教程,所以我的回答可能不是很有帮助。