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.
592
questions
1
vote
2answers
27 views
In Crystal, what's the difference between inheritance and inclusion?
In most of the Crystal docs, class inheritance is used, with < syntax (e.g. https://stackoverflow.com/a/61053311/2954547).
However, HTTP::Handler says that custom handlers must include the HTTP::...
0
votes
0answers
33 views
Issue with starting Lucky server
I've just installed crystal and the lucky framework. I am on windows 10 using WSL2.
This is the error message
Unhandled exception: Unable to get file info: '/mnt/c/Python27/overmind': Input/output ...
0
votes
1answer
34 views
Crystal - Pass instance variable to parent class
I have a Crystal code with inheritance.
The Person is my parent class and it has a person_name string parameter in the initialize method.
The Employee is the child class and it is inherited from ...
1
vote
1answer
42 views
How do I generate a UUID in Crystal?
I'm experimenting with the Crystal standard library and was wondering how to generate a UUID? The UUID.new(..) constructors all seem to expect arguments, but how do I just generate a random one?
1
vote
1answer
46 views
Type specification in Crystal
I'm new to Crystal and i'm trying to translate the following program to Crystal:
#include <stdio.h>
long rev(long n) {
long m = 0;
while (n > 0) {
m = 10*m + n%10;
n = n/10;
}
...
1
vote
1answer
46 views
Tmp macro variable in the Crystal
How i can use temporary variables in macros for building code in the Crystal Lang. For examle, i have:
module Rule
macro included
{% rules = [] of TypeDeclaration %}
macro rule(...
2
votes
1answer
74 views
Why can't Crystal resolve the type of the assignment of 1 + 1?
What's the reason why Crystal can't/won't resolve the type of this? (I see that the documentation does not mention that the compiler could infer instance method calls, but what's the rationale behind ...
0
votes
0answers
41 views
ld unable to find libclang on linux mint 20
Total clang/llvm newbie here...
I just installed llvm and clang on my Linux Mint 20 Ulyana (based on Ubuntu 20.04 Focal Fossa) in order to use it with the Crystal Language automatic binding generation ...
1
vote
1answer
80 views
Can you require a whole set of modules or classes via one require statement?
I am trying to import many classes using one require statement. Is this possible?
I have the following file structure:
In my main.cr I have the following:
require "./module-a"
require "...
1
vote
1answer
53 views
How to interpret an unsigned integer as a signed one and vice versa in Crystal?
In C, to interpret the very bits of an integer as that of an integer of another type (not casting the integer, but rather using the same bits for a different type), one can do this:
uint8_t x = 0xFF;
...
3
votes
4answers
72 views
How to read a certain number of characters (as opposed to bytes) in Crystal?
In Crystal, if I have a string (or a file), how do I read a certain number of characters at a time? Using functions like IO#read, IO#gets, IO#read_string, and IO#read_utf8, one can specify a certain ...
-1
votes
1answer
47 views
How would i extract bits in crystal [closed]
How would i extract bits of a hex number in crystal lang which is mostly like ruby because i am able to extract a byte but i cannot extract 4 bits of one cause that is what i need to do here is what I ...
0
votes
2answers
54 views
Use PullParser to deserialize Range in Crystal
I'm trying to convert some json { "range": {"start": 1, "stop": 10} } into a Range object equivalent to Range.new(1,10).
It seems that if I want to do this in my Foo ...
3
votes
1answer
81 views
How do I check which Operating System (OS) is used in Crystal?
Is there something similar to Ruby's OS gem which allows me to check if I am running on Mac, Linux or Windows?
0
votes
1answer
62 views
In Crystal FFI, how do I access a type in the C library?
The task I'm working on is to add support for the create_function interface to Crystal's SQLite binding: https://github.com/crystal-lang/crystal-sqlite3/issues/61
To access the parameters for a user-...
0
votes
1answer
49 views
What am I supposed to pass to the following method in Crystal?
Background
I'm trying to use some Cairo bindings for Crystal, but I'm having some trouble with the syntax and how to call the method below.
It is implemented as such:
# Inside Module Cairo, class ...
0
votes
1answer
60 views
Subtract two times in crystal lang
I am writing a small benchmark for crystal, and want to compare the time taken without any other library.
In programming language like ruby I would do this:
t = Time.new
sleep 3
puts "Time: #{...
1
vote
1answer
45 views
Is there a more idiomatic way to cast json to a specific Hash type in Crystal?
I've got a JSON::Any, and when I access the "params" key I get a JSON::Any representing this:
{"a":0.9029593355545088,"b":2,"lh":1000,"ph":10,"pl&...
0
votes
2answers
64 views
Clear terminal / console in Crystal (CLI)
How does one clear the console like with console.clear() in Node, but on Crystal.
I have searched around for a while but there seems to be no solution.
0
votes
1answer
28 views
Lucky / Crystal init error: undefined method 'signal' for Process
I'm trying to start a new Lucky project and getting an error. I init the project, then cd and ./scripts/setup. Eventually I see the error
In tasks/watch.cr:154:17
154 | process.signal(:term) unless ...
0
votes
1answer
61 views
Extracting rounding parameters from a set of float values
Given the following float values :
n00.0, n0.0, n.0, 0.n, 0.0n, 0.00n, 0.000n
where n can be 1, 2 or 5, what is the smartest and fastest way to extract the corresponding integer values, to be used ...
1
vote
1answer
76 views
Testing non public routes results in KeyError
i am new to Crystal and Amber and i have an Issue with testing non public routes. I used the Amber authentication generator and afterwards geneated a scaffold for a Job entity and added related routes ...
0
votes
2answers
122 views
Is there a way to do one-liners in Crystal language?
I'm a user of the Ruby language, and while the idea of using the Crystal language as a one-liner may be silly, the Crystal language is so fast that even when you add up the compile time, it can ...
1
vote
1answer
94 views
Crystal-lang: Recursive JSON or Hash
I'm trying to create a JSON or a Hash that can have N depth.
Example:
X people with unique names might have Y kids and those kids might have Z kids (and goes on till N generations).
I want to create a ...
0
votes
1answer
81 views
Why can't Dart decode base64 from Crystal?
Why does Dart produce the error "invalid character at position 61" with base64 from Crystal Lang?
0
votes
0answers
57 views
Calling Mruby library functions in Crystal segfaults on MSVC environment
I am currently working on a way to bind an Mruby interpreter to Crystal projects. However, my current code segfaults on Windows (while running perfectly on WSL Ubuntu) with the 64bit Visual Studio ...
2
votes
1answer
93 views
View metrics or more insights on HTTP::Server
We're running a production system on Crystal/Kemal. The calling service sees quite often a Connection refused error. I was wondering how can I see more insights/metrics into a running instance of HTTP:...
1
vote
0answers
51 views
impossible to extract data from scalar / JSONB
I seem to have an issue with the column "data" in my table "commercial_action_agreements".
It is supposedly a JSONB type.
=# \d+ commercial_action_agreements
Column ...
0
votes
1answer
55 views
Save empty strings as NULL in the database. AmberFramework / Granite
It is possible to force Granite to save empty strings as NULL in the DB?
I tried this code:
before_save :nilify_blanks
def nilify_blanks
self.to_h.keys.each do |column|
if self[column].is_a?(...
1
vote
2answers
62 views
Crystal language: is the first argument to delay in seconds or milliseconds?
I'm looking to use the delay method, but am unsure as to what unit of time the first argument is.
As a follow-up, is the argument an integer, float, or something else?
0
votes
1answer
62 views
Crystal Lang new as a Prefix
I was looking at the source code of the crystal compiler, and I came across this:
def self.from(obj : Array)
case obj.size
when 0
Nop.new
when 1
obj.first
...
3
votes
1answer
92 views
Is there a way to see what a Crystal macro expands to?
I've a macro that refuses to work as expected and I was wondering if there was a way to see what it expands to, is there something like macroexpand-1 from lisp in Crystal? If so, how do I use it? ...
0
votes
1answer
127 views
Can this Crystal benchmark code be improved significantly?
I'm deciding on a language to use for back-end use. I've looked at Go, Rust, C++, and I thought I'd look at Crystal because it did reasonably well in some benchmarks. Speed is not the ultimate ...
0
votes
1answer
55 views
Create C function binding from a header file in Crystal
I'm trying to make a C binding with gai_strerrorA function in Windows. gai_strerrorA definition is exists on a header file (Ws2tcpip.h), not on the static library file (.lib file), Ws2_32.lib.
...
0
votes
1answer
36 views
Why I can't define a function in the `lib` scope?
For example, this code won't compile
def double
x * x
end
lib LibC
# Error
def double
x * x
end
end
I know that we can use module, but we can't have the same LibC name as both module and ...
1
vote
1answer
37 views
Error: no overload matches 'Log::Context.new' with type (Array(String) | Nil)
I did update to crystal 0.35.1 via official docker image. Unfortunately, my project got compilation error somewhere inside crystal itself Error: no overload matches 'Log::Context.new' with type (Array(...
0
votes
1answer
34 views
How to do an unsafe addition (addition without throwing OverflowException) in Crystal?
How do I able to add Int::MAX + 1 without getting OverflowException? I know I can do
def add_unsafe(a : Int, b : Int) : Int
((a.to_i128 + b.to_i128) % Int32::MAX).to_i
end
But is there any ...
0
votes
0answers
31 views
Web push shard for crystal lang
I would like send web push message to browser. I building PWA with Crystal on backend. I found libs for JS web-push and Ruby webpush.
Is there a shard for Crystal lang?
0
votes
1answer
37 views
How do you pass local variables to views in Kemal?
Edit: Link for documentation
I'm new to Crystal and I'm trying to build a small web-app with Kemal framework.
I have some experience working with Ruby and it's frameworks (Rails and Sinatra).
In ...
0
votes
1answer
105 views
How to handle fiber exception outside of fiber?
Sometimes you need to work with unmaintained, old, dirty, huge and sort of libraries that can be dangerous for our program.
Is there have the best practices for execution this code in a safe way?
...
0
votes
2answers
89 views
Send and read data through socket from fiber
Trying to figure out how to send/read data through socket. On remote server I create new netcat -l 4444 and from local send text data echo "test" | netcat remote.host 4444. This is always works fine.
...
0
votes
0answers
34 views
crystal amber framework undefined local variable or method 'parsed_response' for PostController
I am new to Amber framework.
Below is the sample post controller where I try to make an API request just for tesing.
require "crest"
class PostController < ApplicationController
getter post = ...
0
votes
2answers
83 views
How to get all property of a class
I wish get all the properties of a class.
When I pretty print an object, I have all the information of the object, so I suppose there is a way to get the properties of a class directly.
For example :...
0
votes
2answers
90 views
Crystal - How to dynamically get global variables from modules in multiple files
I have multiple files in this format:
car1.cr
module Cars
module Car1
BRAND = "TOYOTA"
end
end
All of these files are getting required by a main file where I can do this:
puts Cars::Car1::...
1
vote
1answer
98 views
Is there an equivalent of march=native in the Crystal compiler?
GCC and Clang support a compiler option named -march=native, which is handy if you want to optimize for the current machine's architecture. The resulting binary might not be portable, but that is OK ...
2
votes
2answers
89 views
Created Expected created to be (Time | Nil) but got (Float64 | String)
I am new to this amber framework,crystal lang and object oriented programming in general . I was following the tutorials and tried to create a simple form using this scaffold
amber g scaffold item ...
1
vote
1answer
128 views
What is the “&-=” operator in Crystal Lang?
I just learned about the existence &-= operator in Crystal. What does it do?
Here is an example from Mutex#try_lock:
private def try_lock
i = 1000
while @state.swap(1) != 0
while @state....
1
vote
1answer
93 views
Is there any alternative to python ast module in Crystal?
Does Crystal expose its internal parser as a standard library as python does with it's ast module? How to parse crystal source code and get the AST of it?
1
vote
1answer
57 views
Is mutation safe on a static variable from multiple thread in crystal-lang?
Consider this example -
class Test
@@store = ""
end
Here store is a class variable of Test and can be mutated from anywhere.
Suppose i am runing my program in multi thread mode & so ...
1
vote
1answer
84 views
What are < symbols in Crystal classes used for?
What is the < symbol in this class and what is its purpose?
class CommentSerializer < BaseSerializer
def initialize(@comment : Comment)
end
def render
{body: @comment.body}
end
end
...