In Ruby, you can do this:
prc = lambda{|x, y=42, *other|}
prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
In particular, I'm interested in being able to get the names of the parameters which are x and y in the above example.
In…
For example, if we want to check an array is either nil or empty, we can write as follows:
if !a || a.empty?
puts "nil or empty!"
end
#-> OK
however, if we want to check two arrays in the same way, an error occurs:
if !a || !b || a.empty? ||…
This is a general question. In Crystal, what is the difference between an Exception and an Error?
For example, in the JSON package, there is both JSON::Error and a JSON::ParseException, which inherits from JSON::Error. Also, how do we know if a…
I have the following accessor method:
def self.database : DB::Database
if @@database.nil?
config = Utils.config["database"].as(Hash)
connection = [
"postgres://#{config["user"]}:#{config["password"]}",
"@localhost/stats",
…
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 = []…
See the OpenSSL::HMAC documentation.
I am trying this:
require "openssl"
puts OpenSSL::HMAC.hexdigest(:sha256, "secret key", "data")
and I am getting this error:
undefined constant OpenSSL::HMAC
Other OpenSSL methods are working fine, like…
I have been trying to create a non-blocking server using Crystal and Kemal which will (a) listen for a stream of UDP messages being sent to it, and (b) then forwarding that message to a WebSocket to any browsers who have started a ws connection.
So…
Is it possible to do static methods in modules as in Ruby ?
module Test
self.def test
puts "test"
end
end
Test::test
I get a expecting token 'EOF', not 'end' if the call is in the same file ( as shown in the exemple ) and a…
Given a binary format with a header that include the number of records and records of format:
{ type : Int8, timestamp : UInt32, user_id : UInt64 }
0000 0004 0153 0927 d139 6747 c045 d991
2100 53d1 6287 4fd2 69fd 8e5f 0475 0153
f323 a72b 4984 a40b…
In Crystal, a String can be turned into an Array(Int32) of codepoints:
"abc".codepoints # [97,98,99]
Is there a way to turn the Array back into a String?
Just out of curiosity and to learn a little bit about the general structure of Crystal, I was looking for some reflection features that would allow me to better understand how the inheritance chain is built.
I was thinking something like ruby's…
Hey i'm looking for some html parsing libraries in crystal. Something similar to nokogiri for ruby. I have a working regular expression but would prefer a html parsing library because html + regex == bad. Thanks.
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?
I wanted to experiment with Crystal's C interop facilities, so I wrote a small POC library in C. I'm able to link the library into a C program and make use of it. However, when I try to link to it from my Crystal program (built using crystal build…