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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
27 views

Looking for a way to compress text (strings, not files) using Crystal Lang : having problem with the reverse operation

Compressing strings in RAM can be done by require "compress/gzip" compressed_string = String.build do |io| Compress::Gzip::Writer.open(io) do |gzip| gzip << "Compress me&...
1 vote
0 answers
79 views

How do I create an iterator over a recursive data structure in Crystal?

I have a tree structure and currently I am trying to return an iterator that iterates over the elements of the datastructure so that my function can accept a block. I have currently reduced my code to ...
0 votes
1 answer
75 views

Taking the address of a two dimensional array's element

I been trying to follow the Crystal specification. It shows an example of taking the address of an instance variable. So I have an analogue function, but pointing to an instance of a 2d array def ...
  • 457
0 votes
1 answer
91 views

Creating an two dimensional array in Crystal

What is a Crystal equivalent of a Ruby 2d Array accessed with indexes like: rooms = Array.new(SIZE) { Array.new(SIZE) } rooms[0][0] = :element I have tried to initialize a 2d Array in Crystal in a ...
  • 457
1 vote
1 answer
52 views

Crystal lang - overriding "to string" method

How to override the "to string" method of an object? I'm getting the following when printing my object to console: #<User:0x7fdd9811eb80> I'm required to have a user-readable version ...
  • 139
1 vote
1 answer
34 views

"Error: unterminated parenthesized expression" when using function of object after parenthesis

i'm running into the problem: Error: unterminated parenthesized expression when trying to compile: expiry_epoch = (expiry_date_bytes.pointer(expiry_date_bytes.size) as Int64*).value I'm not that ...
  • 15
0 votes
1 answer
59 views

I'm having problems downloading the Github AHK_X11 App on Ubuntu 20.04.2 LTS

I downloaded Crystal with brew I don't think the error is related to that /usr/bin/ld: warning: libpcre.so.3, needed by /lib/x86_64-linux-gnu/libglib-2.0.so.0, may conflict with libpcre.so.1 /root/....
0 votes
1 answer
50 views

Get classes from JSON in Crystal

I have some JSON file like this: { "2": { "_id": 2, "_date": "Mon Apr 05 2021", "_timestamp": 1617654662313, "...
  • 1,087
1 vote
1 answer
28 views

Compilation error in PullParser method when using UInt128 field, not with UInt64, in Crystal

I have a strange error in crystal lang: if I make a serializable class with the field _timestamp as UInt128, the compilation it causes and error of JSON::PullParsermethod: $ crystal build src/...
  • 1,087
0 votes
0 answers
29 views

Two independent specs seem to interfere. Can someone tell me why?

I have two specs below: spec/domain/cve/id_spec.cr spec/domain/cwe/id_spec.cr Only 'cve' and 'cwe' differ; the others are the same. When I ran them one by one below, crystal spec spec/domain/cve/...
1 vote
1 answer
166 views

How to create Crystal language Hash where values can be any type?

I'm working on representing an API where a response contains a hash where the keys are of type String and the values can be of any type. Is there a way to represent this in Crystal? I did not see a ...
  • 14.6k
0 votes
3 answers
83 views

Adding a nilable variable to a non-nilable array

I have a string array named current_todos and am trying to add a variable of type (String | Nil) named new_task by doing the following: current_todos << new_task if typeof(new_task) == String I ...
1 vote
1 answer
108 views

How do I list files and directories those are not hidden in current directory using crystal language?

I wrote my own minimal version of "ls" command (Linux) using crystal language and here is my code: require "dir" require "file" def main() pwd = Dir.current ...
2 votes
1 answer
78 views

Making a blocking call asynchronous

I'm using Crystal bindings for X11 and typical usage looks something like this: loop do event = display.next_event # <- blocking! # do stuff with event end This works fine but I can't do ...
  • 9,834
0 votes
1 answer
45 views

How to make an array of a composed type in Crystal?

I was trying class Output alias Type = String | Array(Output) | Hash(Symbol, Output) getter raw def initialize(@raw : Type) end end hash = Output.new({ :a => Output.new("1") }...
  • 6,934
2 votes
1 answer
77 views

What does the ** (Double splat) in Crystal lang do?

What does the ** prefix do in this method call using Crystal-lang? This is from the shrine file package. Can you explain how I would use a double splat? class FileImport::AssetUploader < Shrine ...
1 vote
1 answer
62 views

Why is include JSON::Serializable needed in this struct in Crystal?

In order to do .to_json on this Struct of Book, the include JSON::Serializable is needed, there are answers for this in other languages, but I thought there should be something about this in Crystal ...
0 votes
1 answer
51 views

How to get the type of anything in Crystal?

In Javascript the use of typeof() allows you to quickly see what type of anything is, what is the equivalent in the Crystal? Say the code below I wanted to check what type Crystal thought the object ...
1 vote
2 answers
102 views

Array of objects in Crystal

Beginner question: How to make an Array of objects from a Struct in Crystal? Or how to make an array of objects in Crystal? I am trying to emulate the go code. struct Book def initialize( @Id ...
0 votes
1 answer
36 views

Hash equality in Crystal specs

I'm getting: Expected: {text: "Hello World"} got: {:text => "Hello World"} When the key is a symbol. And Expected: {text: "Hello World"} ...
  • 6,934
1 vote
1 answer
144 views

Why does the backspace key print ^? when calling gets() in crystal?

When I try to correct input for my crystal program (in a zsh terminal on my Mac), a ^? character is printed to the screen for each press of backspace. It's disorienting and does not delete any ...
0 votes
1 answer
37 views

Amberframework Granite decimal number in model

I have a Rails application running Postgres with model containing decimal field. Now I'm creating an Amber API with Granite. How do I define a decimal field in my model in Crystal? class User < ...
4 votes
1 answer
101 views

Understanding enum's types in Crystal

I am trying to implement the famous Lox language based on Robert Nystrom's book, but in Crystal. Now I stumbled upon the following flaw/error: I have an enum for OpCodes, with type UInt8: enum OpCode :...
1 vote
1 answer
106 views

How do I parse a String to Time in Crystal Lang?

I have a String in MM-DD-YYYY format such as: 04-08-2022. I want to parse that to a Time type. How do I do that in Crystal?
1 vote
1 answer
235 views

How Do I Read a File in Crystal Lang?

I am familiar with Ruby and am trying to write a program in Crystal. I have a file called special_file.txt that I want to read in my Crystal program, how do I do that?
3 votes
2 answers
166 views

How to just get a list of command-line arguments in Crystal lang

I'm a newbie in Crystal lang, and I just wanted to know how to get a list (array) of command-line arguments in Crystal. I am aware that there's OptionParser. But I just want to get a list of arguments ...
  • 868
0 votes
1 answer
59 views

Crystal error: undefined method 'member?' for Array

I'm attempting to convert some Ruby code to Crystal. One of my methods is: def check_and_pop_arg(match : String, args : Array(String)) result = args.member?(match) args.delete_if{|v| v == match} ...
  • 7,710
-1 votes
1 answer
86 views

How to return self in Crystal

I'm a newbie in Crystal. I'm trying to return self in Crystal, like in this Python: class Something: def some_method(): # do something return self
  • 868
-3 votes
1 answer
68 views

.get method for hash in Crystal language

I have this Python code: # some_dic is a dictionary value = some_dic.get(var_name, None) How can I do the same in Crystal?
  • 868
3 votes
2 answers
148 views

Why does array "each" do not work anymore in Crystal 1.3.0? What should I use instead?

The code below does not print the elements of the array. Why is that? array = [1, 2, 3] array.each do |x| puts x end array.each { |x| puts x } Crystal Play Output It used to work in previous ...
1 vote
1 answer
596 views

How to turn off the GC in Crystal and do memory management yourself, without any GC?

I'm evaluating the Crystal programming language for real-time, low-latency applications, that can't be interrupted by the garbage collector. Two questions: Is it possible to deallocate some class from ...
1 vote
1 answer
164 views

instance and class variables in crystal seem impossible to use

You'll have to forgive me as I haven't worked with a statically typed language in 15 years and I'm sure the answer to this is incredibly simple but I've been banging my head against it for days with ...
  • 461
1 vote
2 answers
73 views

Crystal lang, type of type

How i can define type for other types, modules, classes. For example: module One end module Two end array_of_modules = Array(?).new array_of_modules << One array_of_modules << Two Or ...
1 vote
2 answers
58 views

Generic type convertion

I am trying to create a method which gets input from the user and converts that to a specified type. Is there a way to do this without rewriting for each type. Something like this: struct Input(...
  • 39
1 vote
1 answer
81 views

How can I generate code from file at compile time using a macro?

I have a CSV file that looks like this: CountryCode,CountryName AD,Andorra AE,United Arab Emirates AF,Afghanistan AG,Antigua and Barbuda // -- snip -- // and a class that looks like this: module ...
3 votes
2 answers
700 views

Crystal lang on Apple Silicone M1

When i try to connect to DB(Postgres), via this driver https://github.com/will/crystal-pg, require "pg" cnn = PG.connect("postgres://root:password@localhost/my_db_dev") i get ...
0 votes
1 answer
91 views

How to manipulate binary numbers efficiently in Crystal?

I'm trying to implement the Bitcoin specification BIP-39, specifically the part Generating the mnemonic. The following causes some headaches: Next, these concatenated bits are split into groups of 11 ...
  • 11.1k
4 votes
3 answers
1k views

How to handle application level redirects in Nginx with Reverse Proxy

I am writing an Nginx configurations for Crystal-Lang based application to send all the traffic http://example.com/videos/ to http://0.0.0.0:3000 via reverse proxy. I have written the following config ...
  • 331
1 vote
1 answer
181 views

Install Crystal Linux Mint. Error: Could not locate compatible llvm-config

Following steps to install Crystal from source: https://crystal-lang.org/install/from_sources/ Need to run make command that raise error: Makefile:65: *** Could not locate compatible llvm-config, make ...
1 vote
1 answer
88 views

How to .each_char.map?

In Crystal, if I try this: numbers = [1, 2, 3, 4, 5] a = numbers.map { 0 } p a The output will be nice like this: [0, 0, 0, 0, 0] However if I have a string and try to manipulate each char of that ...
  • 39
0 votes
0 answers
54 views

How do I make this app factor a polynomial with quadratic formula or some other trick?

How do I make this app factor a polynomial with quadratic formula or some other trick?. The polynomial 3x^2 +10x -8 can be factored to (x +4)(3x -2) with the use of grouping or the X trick. I would ...
0 votes
1 answer
164 views

Is it possible to pass a Proc into a function?

I'm trying to implement Ruby's function composition operator << into Crystal's proc. In Ruby it seems pretty straight forward. def << block proc { |*args| self.call( block.to_proc....
0 votes
1 answer
65 views

Is running crystal app.cr the same as crystal run app.cr

In the documentation crystal says to build and run with the shortcut command: crystal run app.cr and this won't create a permanent binary. However I could also just run crystal compiler directly on ...
1 vote
1 answer
103 views

How to parse an 80-bit floating point number in Crystal?

I want to read an 80-bit floating point number (according to the IEEE 754 standard) from an IO object and store it in a usable type like BigDecimal, Float64 or something similar. (After reading the ...
0 votes
1 answer
103 views

Crystal + Amber: "cannot find -lsqlite3 (this usually means you need to install the development package for libsqlite3)"

After running the command to install Ubuntu dependencies, I am still receiving the following error when trying to run a shards build: /usr/bin/ld: cannot find -lsqlite3 (this usually means you need to ...
2 votes
1 answer
137 views

Depedency injection in crystal language

Is there any way to do dependency injection in crystal, I only found it with some frameworks(like athena and shivneri) but not by itself
0 votes
1 answer
171 views

In this case, how do I handle nil well?

Situation I wrote the following code. class Gear getter :chainring, :cog, :wheel def initialize(@chainring : Int32, @cog : Int32, @wheel : Wheel?) end def ratio chainring / cog.to_f ...
0 votes
1 answer
149 views

Crystal build fails with error - cannot find -levent

I was getting this error while trying to run a simple crystal program in my ubuntu. /usr/bin/ld: cannot find -levent (this usually means you need to install the development package for libevent) ...
  • 1,011
2 votes
1 answer
572 views

VS Code Highlight Text Mate Rules Priority

Is there a way to make the meta.function-call text mate rule to take precedence over entity.name.function? In VS Code Crystal (Ruby-like language) setting both method definition and method call marked ...
  • 13.1k
1 vote
1 answer
40 views

Why Array.to_s is not recursive?

The Array.to_s uses inspect on its content, instead of calling to_s recursively. This code class Some def to_s; "some" end end puts [Some.new].to_s Would produce [#<Some:0x10078ce80&...
  • 13.1k

1
2 3 4 5
14