Questions tagged [crystal-lang]

Crystal is a programming language with a Ruby inspired syntax but statically type checked and compiled to native and efficient code.

Links

Projects

660 questions
4
votes
2 answers

How to get the name of the arguments passed into a block?

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…
Martimatix
  • 1,553
  • 1
  • 15
  • 18
4
votes
2 answers

Nil check in `||`, `!a || a.method` can't be done with multiple variables

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? ||…
statiolake
  • 53
  • 4
4
votes
1 answer

Exceptions & Errors in Crystal

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…
Ravern Koh
  • 57
  • 5
4
votes
1 answer

Initialize on first access

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", …
James Taylor
  • 6,158
  • 8
  • 48
  • 74
4
votes
1 answer

running shell commands crystal language and capturing the output

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 = []…
lewis
  • 131
  • 1
  • 9
4
votes
1 answer

How to create an HMAC in crystal-lang

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…
twharmon
  • 4,153
  • 5
  • 22
  • 48
4
votes
2 answers

Using Crystal/Kemal to listen for UDP packets

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…
CyberFerret
  • 275
  • 3
  • 8
4
votes
1 answer

Has Crystal got static methods?

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…
4
votes
3 answers

Parsing a binary format in Crystal

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…
kreek
  • 8,774
  • 8
  • 44
  • 69
4
votes
2 answers

How do you turn an Array of codepoints (Int32) to a string?

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?
dgo.a
  • 2,634
  • 23
  • 35
4
votes
1 answer

Nested hash generation error

Given the following code: require "big" alias Type = Nil | String | Bool | Int32 | BigFloat | Array(Type) | Hash(String | Symbol, Type) alias HOpts = Hash(String | Symbol, Type) ctx = HOpts.new ctx["test_int"] = 1 ctx["test_s"] = "hello" c1 =…
binduck
  • 41
  • 1
4
votes
1 answer

Getting info about inheritance chain in Crystal

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…
opensas
  • 60,462
  • 79
  • 252
  • 386
4
votes
3 answers

Are there any HTML parsing libraries?

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.
Sag0Sag0
  • 95
  • 8
4
votes
2 answers

Crystal lang how to get binary file from http

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?
SeventhSon
  • 71
  • 5
4
votes
1 answer

Trouble linking against static C library in Crystal program

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…
pdoherty926
  • 9,895
  • 4
  • 37
  • 68