crystal-lang

Crystal lang how to get binary file from http

女生的网名这么多〃 提交于 2019-12-13 16:06:05
问题 In Ruby: require 'open-uri' download = open('http://example.com/download.pdf') IO.copy_stream(download, '~/my_file.pdf') How to do the same in Crystal? 回答1: We can do the following: require "http/client" HTTP::Client.get("http://example.org") do |response| File.write("example.com.html", response.body_io) end This writes just the response without any HTTP headers to the file. File.write is also smart enough to not download the entire file into memory first, but to write to the file as it reads

Crystal installation on WSL fails

孤人 提交于 2019-12-13 03:32:19
问题 I'm following the current crystal installation docs, my installation stops at the first moment and give the error: gpg: connecting dirmngr at '/tmp/apt-key-gpghome.4GKHZljOFL/S.dirmngr' failed: IPC connect call failed gpg: keyserver receive failed: No dirmngr I've already installed dirmngr. 回答1: First of all, you need to remove crystal.list from sources directory to install from start manually. sudo rm /etc/apt/sources.list.d/crystal.list And then: curl -s "https://keyserver.ubuntu.com/pks

Any equivalent of Ruby's public_send method?

依然范特西╮ 提交于 2019-12-13 02:56:57
问题 In Ruby, to construct a method's name and send it to an object, one can do: class Foo def foo "FOO" end end Foo.new.public_send(:foo) # => "FOO" Foo.new.public_send("foo") # => "FOO" What is equivalent technique for Crystal? 回答1: You should remember that Crystal, unlike Ruby, is a compiled, statically-typed language, so dynamic features of Ruby don't map that well to it. Crystal's alternative to dynamism is support for macros - but they are not the same, and you shouldn't expect them to work

Pack the contents of arr into a binary sequence in Crystal

扶醉桌前 提交于 2019-12-12 16:06:11
问题 Is there a standard way to achieve the same result as in Ruby with Array#pack method: [1,2].pack­ "LL" => "\x01\x00\x00\x00\x02\x00\x00\x00" 回答1: Not yet, and most likely, it wont be. The reason is that usually the result of pack is used to be sent to an IO (do you have any other case in mind?), so instead of allocating the result in memory, we're thinking about providing equivalent methods in the IO itself to send the data directly to the socket, file, etc... It's not written in stone and

Method proc with parameters

我只是一个虾纸丫 提交于 2019-12-12 15:37:39
问题 Is possible to use method proc with parameters in Crystal ? Something like: def foo(baz) puts "#{baz} foo!" end proc = ->foo proc.call "Hi" 回答1: Yes. If the method has arguments, you must specify their types: proc = ->foo(String) proc.call "Hi" # Hi foo! Find more examples at crystal docs. 来源: https://stackoverflow.com/questions/46728581/method-proc-with-parameters

Crystal: how to implement multiple abstract methods with one method in child?

纵然是瞬间 提交于 2019-12-11 12:52:56
问题 Suppose I have an abstract struct that needs to operate on two kinds of inputs like so (for more context, see previous SO question). abstract struct Numberlike alias Num = (Int32 | Float64) abstract def - abstract def -(other : self) abstract def -(other : Num) end If my implementation can use self and Num interchangeably, it seems reasonable to just put them together: struct Term < Numberlike alias Num = (Int32 | Float64) getter coeff : Num getter sym : Symbol def initialize(@coeff, @sym);

Why do processes killed in a Signal trap block become zombies instead of terminated regularly?

爷,独闯天下 提交于 2019-12-11 08:58:34
问题 If I created processes with Process.new , they can be killed with .kill . However, they become zombies in a Signal trap: PROCESSES = {} of Int32 => Process spawn { loop { sleep 1 } } spawn { x = Process.new("sleep", "100".split) x.kill sleep 0.4 puts x.terminated? x = Process.new("sleep", "100".split) PROCESSES[x.pid] = x } Signal::INT.trap { Signal::INT.reset PROCESSES.each { |pid, x| puts "killing: #{pid}" x.kill sleep 1 puts "killed #{pid}? #{x.terminated?}" } } sleep Running this code

Read a single char from stdin without pressing enter

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:23:46
问题 How can I read a single char from the console without pressing enter / return? In ruby I would just use: require 'io/console' input = STDIN.getch 回答1: Try this: char = STDIN.raw &.read_char p char 来源: https://stackoverflow.com/questions/39964008/read-a-single-char-from-stdin-without-pressing-enter

How to specify the data types for JSON parsing?

谁都会走 提交于 2019-12-08 17:21:08
问题 I have a JSON response which is an Array of Hash: [{"project" => {"id" => 1, "name" => "Internal"}, {"project" => {"id" => 2, "name" => "External"}}] My code looks like this: client = HTTP::Client.new(url, ssl: true) response = client.get("/projects", ssl: true) projects = JSON.parse(response.body) as Array This gives me an Array but it seems I need to typecast the elements to actually use them otherwise I get undefined method '[]' for Nil (compile-time type is (Nil | String | Int64 | Float64

How to configure JSON.mapping for Array of Array of Strings to become a Hash?

。_饼干妹妹 提交于 2019-12-07 16:55:52
问题 I am trying to process the following JSON that I receive from an API. {"product":"midprice", "prices":[ ["APPLE","217.88"], ["GOOGLE","1156.05"], ["FACEBOOK","160.58"] ]} I can get a basic mapping working with: require "json" message = "{\"product\":\"midprice\",\"prices\":[[\"APPLE\",\"217.88\"],[\"GOOGLE\",\"1156.05\"],[\"FACEBOOK\",\"160.58\"]]}" class Midprice JSON.mapping( product: String, prices: Array(Array(String)), ) end midprice = Midprice.from_json(message) p midprice.product #