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
5
votes
2 answers

Crystal lang : How to indicate require path for crystal compiler

If some of my libraries locate out of default directory lib, could I indicate the require path to compile successfully without lib directory nor shards.yml.
XQY
  • 552
  • 4
  • 17
5
votes
2 answers

Is there a #between? for numbers in Crystal?

I was wondering if I simply cannot find a between method for numbers in Crystal. In Ruby, there's the Comparable#between? method which can (among others) compare two numeric values (my specific case). Background: I want to achieve a not-between…
GHajba
  • 3,665
  • 5
  • 25
  • 35
5
votes
1 answer

What's the difference between Hash and NamedTuple in Crystal?

Both syntaxes look rather equivalent, and their use cases are similar too. Example: # Hash hash = {"name" => "Crystal", "year" => 2011} hash["name"] # outputs: Crystal # NamedTuple tuple = {name: "Crystal", year: 2011} tuple[:name] # outputs:…
Claudio Holanda
  • 2,455
  • 4
  • 21
  • 25
5
votes
2 answers

Curly bracket constructors?

While reading through the Crystal docs, I came across this line: deq = Deque{2, 3} So I think this calls the Deque.new(array : Array(T)) constructor. However, I did not find any documentation about this syntax whatsoever. (EDIT: The documentation…
Jens Nedregård
  • 199
  • 1
  • 13
5
votes
1 answer

Why does Crystal's macro syntax for iterating differ from the rest of Crystal

Coming from the Ruby world, I instantly understood why Crystal chose not to implement a for method. But then I was surprised to see that Crystal does implement a for method for macros. I was even more surprised to find that macros don't allow an…
John
  • 9,249
  • 5
  • 44
  • 76
5
votes
1 answer

How does JSON.mapping macro work with union types of arguments?

In JSON.mapping documentation explicitly stated the value of type property should be single type. However, in practice union types also works: json1 = %q({"ok": true, "result": [{"type": "update", "id": 1}, {"type": "update", "id": 2}]}) json2 =…
vtambourine
  • 2,109
  • 3
  • 18
  • 27
5
votes
1 answer

How to map JSON::Any to custom Object in Crystal language?

How to map parsed JSON as JSON::Any type to custom Object? In my case, I am working on chat client. Chat API can respond to requests with following JSON: {"ok" => true, "result" => [{"update_id" => 71058322, "message" => {"message_id" =>…
vtambourine
  • 2,109
  • 3
  • 18
  • 27
5
votes
1 answer

Crystal debugging with GDB

I am trying to learn to debug programs written in Crystal with GDB. Here is a sample: class Demo @array = [] of String def bar(url) ret = url.downcase * 2 if ret == "alsj" return false else return ret end end …
DP.
  • 581
  • 4
  • 15
5
votes
1 answer

Parse Array of JSON Objects in Crystal lang

Suppose I've got a simple JSON mapped object in Crystal lang, e.g.: class Item JSON.mapping( id: UInt32, name: String, ) end I can parse individual objects from JSON strings easily like so: foo =…
maerics
  • 151,642
  • 46
  • 269
  • 291
5
votes
1 answer

Does Crystal store any sensitive information?

If a developer compiles a Crystal program, what metadata will the binary file store and how to remove any sensitive information? By sensitive I mean device identificators, local IP addresses or anything else.
Vlad Faust
  • 542
  • 6
  • 18
5
votes
3 answers

Crystal: How can I find the SHA256 hash of a binary value?

I'm new to Crystal. I'd like to try and find the SHA256 hash of a hex string. I've managed to get something working: sha256 = OpenSSL::Digest.new("sha256") puts sha256.update("abcd") But I'm not sure how to put the binary value of "abcd" in to the…
inersha
  • 422
  • 5
  • 20
5
votes
1 answer

Binding glib into Crystal lang (GC issue)

I am trying to bind some functions from glib into Crystal. I've done this and it works: @[Link("glib-2.0")] lib LibG fun g_utf8_strup(str : UInt8*, len : UInt32) : UInt8* fun g_utf8_strdown(str : UInt8*, len : UInt32) : UInt8* end However it…
Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
5
votes
1 answer

How to write binary into file in Crystal

I have an array of UInt32, what is the most efficient way to write it into a binary file in Crystal lang? By now I am using IO#write_byte(byte : UInt8) method, but I believe there should be a way to write bigger chunks, than per 1 byte.
Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
5
votes
1 answer

What is the proper way to check if an Iterator is complete?

As the title states, I'd like to know the proper way to check if an iterator is complete. I couldn't find anything in the docs but I found something like this in the source: iter.next.is_a? Iterator::Stop Toy example: a = "a世c" b =…
Lye Fish
  • 2,538
  • 15
  • 25
5
votes
1 answer

Get filepath of importing file at compile time

If I have a file that's meant to be required by other files, is it possible to get the absolute filepath of the file that's requiring it? So if lib_file.cr has a macro that is meant to be called by the app_file.cr that imported it, can that macro…
Lye Fish
  • 2,538
  • 15
  • 25