Skip to content

Instantly share code, notes, and snippets.

@BrianPurgert
Created January 4, 2024 09:51
Show Gist options
  • Save BrianPurgert/43a06f7827b6dc2af064f08b1baf835d to your computer and use it in GitHub Desktop.
Save BrianPurgert/43a06f7827b6dc2af064f08b1baf835d to your computer and use it in GitHub Desktop.
testing code blocks in tables
Ruby CodeRBS Type Signature
module ChatApp
	VERSION = "1.0.0"

	class User
		attr_reader :login, :email

		def initialize(login:, email:)
			@login = login
			@email = email
		end
	end

	class Bot
		attr_reader :name, :email, :owner

		def initialize(name:, owner:)
			@name  = name
			@owner = owner
		end
	end

	class Message
		attr_reader :id, :string, :from, :reply_to

		def initialize(from:, string:)
			@from   = from
			@string = string
		end

		def reply(from:, string:)
			@reply_to = Message.new(from: from, string: string)
		end
	end

	class Channel
		attr_reader :name, :messages, :users, :bots

		def initialize(name:)
			@name     = name
			@messages = []
			@users    = []
			@bots     = []
		end

		def each_member
			(@users + @bots).each do |member|
				yield member
			end
		end
	end
end
module ChatApp
    VERSION: String

    class User
        attr_reader login: String
        attr_reader email: String

        def initialize: (login: String, email: String) -> void
    end

    class Bot
        attr_reader name: String
        attr_reader email: String
        attr_reader owner: User

        def initialize: (name: String, owner: User) -> void
    end

    class Message
        attr_reader id: String
        attr_reader string: String
        attr_reader from: User | Bot
        # `|` means union types: `#from` can be `User` or `Bot`
        attr_reader reply_to: Message?
        # `?` means optional type: `#reply_to` can be `nil`

        def initialize: (from: User | Bot, string: String) -> void

        def reply: (from: User | Bot, string: String) -> Message
    end

    class Channel
        attr_reader name: String
        attr_reader messages: Array[Message]
        attr_reader users: Array[User]
        attr_reader bots: Array[Bot]

        def initialize: (name: String) -> void

        def each_member: () { (User | Bot) -> void } -> void
        # `{` and `}` means block.
                | () -> Enumerator[User | Bot, void]
        # Method can be overloaded.
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment