晶体中的对象数组

发布于 2025-02-08 00:05:45 字数 1198 浏览 2 评论 0 原文

初学者问题:

如何用晶体中的结构制作一系列对象?还是如何在晶体中制作一系列对象?我正在尝试模仿GO代码。

struct Book 
   def initialize(
     @Id : Int32,
     @Title : String,
     @Author : String, 
     @Desc : String
   )
   end
 end

 books = [] of Int32 | String <--- This is wrong
 book1 = Book.new(1, "Hello", "Me", "This is a test.")


GO CODE: 

type Book struct {
   Id     int    `json:"id"`
   Title  string `json:"title"`
   Author string `json:"author"`
   Desc   string `json:"desc"`
}

var Books = []models.Book{
   {
       Id:     1,
       Title:  "Golang",
       Author: "Gopher",
       Desc:   "A book for Go",
   },
}

更改为课程可以使我返回一个新对象。我敢肯定,在晶体中制作新对象的错误方法是错误的?然后,我可以在数组中添加对象。

  class Book 
    def initialize(id : Int32, title : String, author : String, desc : String)
      @id = id
      @title = title
      @author = author
      @desc = desc
    end

    def object 
      {
        @id,
        @title,
        @author,
        @desc
    }
    end 

  end


  books = [] of Book <--- HOW TO SET ARRAY OF OBJECTS ON THE TYPE?
  book1 = Book.new(1, "Hello", "Me", "This is a test.")
  puts book1.object
  books << book1.object
  puts books

Beginner question:

How to make an Array of objects from a Struct in Crystal? Or how to make an array of objects in Crystal? I am trying to emulate the go code.

struct Book 
   def initialize(
     @Id : Int32,
     @Title : String,
     @Author : String, 
     @Desc : String
   )
   end
 end

 books = [] of Int32 | String <--- This is wrong
 book1 = Book.new(1, "Hello", "Me", "This is a test.")


GO CODE: 

type Book struct {
   Id     int    `json:"id"`
   Title  string `json:"title"`
   Author string `json:"author"`
   Desc   string `json:"desc"`
}

var Books = []models.Book{
   {
       Id:     1,
       Title:  "Golang",
       Author: "Gopher",
       Desc:   "A book for Go",
   },
}

Changing to a class allows me to return a new object. Which I am sure is the wrong way to make a new object in Crystal? I can then add objects to the array.

  class Book 
    def initialize(id : Int32, title : String, author : String, desc : String)
      @id = id
      @title = title
      @author = author
      @desc = desc
    end

    def object 
      {
        @id,
        @title,
        @author,
        @desc
    }
    end 

  end


  books = [] of Book <--- HOW TO SET ARRAY OF OBJECTS ON THE TYPE?
  book1 = Book.new(1, "Hello", "Me", "This is a test.")
  puts book1.object
  books << book1.object
  puts books

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2025-02-15 00:05:45

您可以写作

books = [] of Book
books << book1

,也可以简单地执行此操作,然后让Crystal识别类型:

books = [book1]

最接近原件的是

books = [
  Book.new(1, "Hello", "Me", "This is a test.")
]

编辑:如果您需要JSON,则需要实现它,例如 with-to-json“ rel =“ rel =“ nofollow noreferrer”>。最简单的是包括JSON :: serializable

require "json"

module TestBookJSON

  struct Book
    include JSON::Serializable      # <-- this

    def initialize(
      @Id : Int32,
      @Title : String,
      @Author : String,
      @Desc : String
    )
    end
  end

  books = [] of Book
  book1 = Book.new(1, "Hello", "Me", "This is a test.")
  books << book1
  puts books.to_json
  # => [{"Id":1,"Title":"Hello","Author":"Me","Desc":"This is a test."}]

end

You can write

books = [] of Book
books << book1

or you can simply do this, and let Crystal recognise the type:

books = [book1]

The closest to the original is

books = [
  Book.new(1, "Hello", "Me", "This is a test.")
]

EDIT: If you need JSON, you need to implement it, as described in the documentation. The easiest is to include JSON::Serializable:

require "json"

module TestBookJSON

  struct Book
    include JSON::Serializable      # <-- this

    def initialize(
      @Id : Int32,
      @Title : String,
      @Author : String,
      @Desc : String
    )
    end
  end

  books = [] of Book
  book1 = Book.new(1, "Hello", "Me", "This is a test.")
  books << book1
  puts books.to_json
  # => [{"Id":1,"Title":"Hello","Author":"Me","Desc":"This is a test."}]

end
素食主义者 2025-02-15 00:05:45

似乎我想要的是,与Go中的结构版本不一样,是一系列哈希,这反过来是一系列对象,因为一切都是晶体中的对象?*。

  hash = [] of Hash(Int32, String) # Array of Hashes
  hash0 = {0 => "Jackson0"}
  hash1 = {1 => "Jackson1"}
  hash2 = {2 => "Jackson2"}
  hash3 = {3 => "Jackson3"}

  #Append the hashes to the array

  hash << hash0
  hash << hash1
  hash << hash2
  hash << hash3

OR

  (0..3).each do |i| 
    hash << {i => "Jackson#{i}"}
  end
  RESULT : [{0 => "Jackson0"}, {1 => "Jackson1"}, {2 => "Jackson2"}, {3 => "Jackson3"}]

  # Display them as JSON

  get "/" do
    hash.to_json
  end

RESULT : [{"0":"Jackson0"},{"1":"Jackson1"},{"2":"Jackson2"},{"3":"Jackson3"}]

Which in turn can be queried using hash[0][1] 

RESULT : {"1": "Jackson1"}

It seems really what I wanted, and what was not the same as the struct version in go was an Array of Hashes, which in turn is an Array of Objects, as everything is an Object in Crystal?*.

  hash = [] of Hash(Int32, String) # Array of Hashes
  hash0 = {0 => "Jackson0"}
  hash1 = {1 => "Jackson1"}
  hash2 = {2 => "Jackson2"}
  hash3 = {3 => "Jackson3"}

  #Append the hashes to the array

  hash << hash0
  hash << hash1
  hash << hash2
  hash << hash3

OR

  (0..3).each do |i| 
    hash << {i => "Jackson#{i}"}
  end
  RESULT : [{0 => "Jackson0"}, {1 => "Jackson1"}, {2 => "Jackson2"}, {3 => "Jackson3"}]

  # Display them as JSON

  get "/" do
    hash.to_json
  end

RESULT : [{"0":"Jackson0"},{"1":"Jackson1"},{"2":"Jackson2"},{"3":"Jackson3"}]

Which in turn can be queried using hash[0][1] 

RESULT : {"1": "Jackson1"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文