crystal-lang

crystal-lang : how to fill an array with all modules defined in files in a specific package of the project?

℡╲_俬逩灬. 提交于 2021-01-29 09:20:34
问题 All in the question, I would like to know if it's possible to do something like this in Crystal (pseudo-code) : modules = Array(Module).new foreach (file in specific_package) do if (file.is_a(crystal_module) do modules.push(file.module) end end 回答1: Crystal doesn't have packages but modules. If you want to get all modules in a file you can try this: require "compiler/crystal/syntax" Modules = [] of String class ModuleVisitor < Crystal::Visitor def visit(node : Crystal::ModuleDef) Modules <<

A WEBrick like server with Crystal

对着背影说爱祢 提交于 2020-04-16 04:27:10
问题 Is it possible to create a simple web server with Crystal that can serve HTML, CSS and JS pages? My current code is: require "http/server" Port = 8080 Mime = "text/html" server = HTTP::Server.new([HTTP::ErrorHandler.new, HTTP::LogHandler.new]) do |context| req = context.request if req.method == "GET" filename = File.join(Dir.current, "index.html") context.response.content_type = Mime context.response.content_length = File.size(filename) File.open(filename) { |file| IO.copy(file, context

Binding C to Crystal: preprocessor directives

£可爱£侵袭症+ 提交于 2019-12-25 11:49:29
问题 I'm working with the crt.cr Crystal shard, which binds ncurses. It's lacking some things I want, like mvhline() . So I'm adding the things I want. One thing I want is is ncurses alternative character sheet, so I can make nice boxes. As far as I can tell, this is pretty dang hard (but I'm not an expert in either Crystal or C). From what I can tell, the alternative character sheet characters are all unsigned chars, defined by the preprocessor. Can someone explain how I can get access to the

Crystal how to require implementing class operate on self, instead of all siblings

空扰寡人 提交于 2019-12-25 02:25:22
问题 Let's say I want my method to accept anything that is "number like" i.e. knows how to negate, add, subtract, multiply and divide. It needs to do these with itself and with numbers (Int32 and Float64 for my purposes) abstract struct Numberlike alias Num = (Int32 | Float64) abstract def - abstract def +(other : self) abstract def +(other : Num) abstract def -(other : self) abstract def -(other : Num) abstract def *(other : self) abstract def *(other : Num) abstract def /(other : self) abstract

Condition over object's type

人走茶凉 提交于 2019-12-24 11:36:39
问题 I need to use a simple if statement based on an object's type/class. I have a custom Array class and a Matrix class. Elements of the Array class don't have a number_of_cols attribute def method(other) if self.is_a?(Array) c = self.class.zeros(shape[0], shape[1]) elsif self.is_a?(Matrix) c = self.class.zeros(number_of_rows, other.number_of_cols) end end However, when running this, I get an error: undefined method 'number_of_cols' for Array which is exactyle why I have this if statement. I also

Process and TCPSocket not close properly in crystal

这一生的挚爱 提交于 2019-12-24 06:34:53
问题 I'm creating a tcp server who accept all the connection and execute incomming data has command line, but when i send "exit" to the tcpsocket, the process and the socket dont close properly # main.cr require "socket" PORT = 2022 def handle_connection(socket) Process.run("/bin/sh", input: socket, output: socket, error: socket) end server = TCPServer.new(PORT) loop do if socket = server.accept? spawn handle_connection(socket) else break end end for example, the following code work fine, after

Does it have a crystal-lang Queue?

流过昼夜 提交于 2019-12-24 03:32:14
问题 How to realize pattern producer - consumer on crystal lang? I'am looking for something like that - http://ruby-doc.org/core-2.2.0/Queue.html Probably i need to use Channel , but I don't understand how.. because it's wait while "consumer" will receive. I mean: channel = Channel(Int32).new spawn do 15.times do |i| # ... do something that take a time puts "send #{i}" channel.send i # paused while someone receive, but i want to continue do the job that takes a time.. end end spawn do loop do i =

running shell commands crystal language and capturing the output

戏子无情 提交于 2019-12-22 17:55:13
问题 I am used to using open3 to run commands in Ruby. Since there doesn't seem to be an equivalent lib in crystal-lang, I kludged up this: def run_cmd(cmd, args) stdout_str = IO::Memory.new stderr_str = IO::Memory.new result = [] of Int32 | String status = Process.run(cmd, args: args, output: stdout_str, error: stderr_str) if status.success? result = [status.exit_code, "#{stdout_str}"] else result = [status.exit_code, "#{stderr_str}"] end stdout_str.close stderr_str.close result end cmd = "ping"

Crystal How to check if the block argument is given inside the function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 13:01:14
问题 Suppose a function defined like this: def composition(text : String, k : Int32) : Array(String) kmers = Array(String).new (0 .. text.size - k).each do |i| kmers << text[i, k] yield text[i, k] end return kmers end How do I check if the block argument is given inside the function? If the block argument is given, kmers will be yielded. If not given, kmers will be returned as an array of strings. 回答1: Such a check is impossible, because a method accepting a block (using yield anywhere) simply has

Crystal How to check if the block argument is given inside the function

懵懂的女人 提交于 2019-12-22 12:59:26
问题 Suppose a function defined like this: def composition(text : String, k : Int32) : Array(String) kmers = Array(String).new (0 .. text.size - k).each do |i| kmers << text[i, k] yield text[i, k] end return kmers end How do I check if the block argument is given inside the function? If the block argument is given, kmers will be yielded. If not given, kmers will be returned as an array of strings. 回答1: Such a check is impossible, because a method accepting a block (using yield anywhere) simply has