crystal-lang

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

冷暖自知 提交于 2019-12-06 10:06:50
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. Such a check is impossible, because a method accepting a block (using yield anywhere) simply has a different signature. But that also means that you don't need the check, just make 2 methods like this: #

Is it possible for a Crystal lang process to change it's user (euid/uid)?

夙愿已清 提交于 2019-12-06 07:41:14
If a Crystal language process is running as root, can it change it's euid/uid to something else? For example: old_euid, old_ui = Process.euid, Process.uid Process.euid = someone_else Process.uid = someone_else Yes, by using bindings to libc: lib LibC fun setuid(uid_t : Int) fun getuid : Int end LibC.getuid #=> 0 Process.run("whoami", output: true) #=> root LibC.setuid(uid) LibC.getuid #=> my uid Process.run("whoami", output: true) #=> my user name The program needs to be running as root of course (i. e. sudo crystal source.cr ) 来源: https://stackoverflow.com/questions/47557469/is-it-possible

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

拟墨画扇 提交于 2019-12-06 02:09:12
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 # Outputs the String p midprice.prices # Outputs Crystal 0.26.1 Code: https://play.crystal-lang.org/#/r/515o

Anybody tried the Crystal Programming Language (machine-code compiled Ruby)? [closed]

 ̄綄美尐妖づ 提交于 2019-12-03 04:11:46
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . Like many others, I always hold true that "A pure compiler will never exist for Ruby because the language is far too dynamic for a static compiler to work." But I recently stumbled upon these: The Crystal programming language at GitHub Statically compiled Ruby Both projects

Anybody tried the Crystal Programming Language (machine-code compiled Ruby)? [closed]

纵然是瞬间 提交于 2019-12-02 17:29:13
Like many others, I always hold true that "A pure compiler will never exist for Ruby because the language is far too dynamic for a static compiler to work." But I recently stumbled upon these: The Crystal programming language at GitHub Statically compiled Ruby Both projects seem to be very interesting. They could give us the speed of a native-compiled language (and the often commercially-required, obfuscated code of a compiled language) while keeping all (or most) of the elegance and flexibility of Ruby. Add a good support library (or, more likely, the possibility to access the existing C++

ld: library not found for -lssl

你。 提交于 2019-12-01 18:11:32
问题 I installed crystal with homebrew brew install crystal-lang . I was able to compile and run a "Hello World!" program, but when I try to compile the example http server (with one slight modification) I get an error. HTTP server: require "http/server" port = 3000 server = HTTP::Server.new(port) do |context| context.response.content_type = "text/plain" context.response.print "Hello world! The time is #{Time.now}" end puts "listening on http://localhost:" + port.to_s puts "listening on http:/