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.

crystal-lang
Filter by
Sorted by
Tagged with
0 votes
1 answer
45 views

is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?

The question says it all. For example: class Foo INSTANCE = new def initialize p "initialized" end end Will print only when Foo::INSTANCE accessed, not automatically after compilation. ...
ClassyPimp's user avatar
0 votes
1 answer
393 views

execution of command failed with code: 1: `cc -o "/root/.cache/crystal/var-app-staging-new-http.cr

I am getting the following error while compiling crystal code: [root@ip-172-31-53-176 staging]# cat new-http.cr require "http/server" srv = HTTP::Server.new(3000) do |context| context.response....
naren nath's user avatar
3 votes
1 answer
894 views

How to convert a hex string to bytes array in Crystal?

How can I convert a hex string to bytes array in Crystal? Example: 87 A3 69 6E 74 01 A5 66 6C 6F 61 74 CB 3F E0 00 00 00 00 00 00 A7 62 6F 6F 6C 65 61 6E C3 A4 6E 75 6C 6C C0 A6 73 74 72 69 6E 67 ...
Rayner's user avatar
  • 33
3 votes
2 answers
677 views

Ruby gem native extension with Crystal?

I'm just trying to gather some general information, as I'm looking to improve performance of ActiveModelSerializers. I've also never written a native extension before, but have looked at some C ...
NullVoxPopuli's user avatar
0 votes
1 answer
437 views

How to control using CPU cores [Crystal]

I tried to run the same program on both Ruby and Crystal. In the program, there is no code which controls threads. As for Ruby, Only 1 core is used and the usage is 100% through the program as ...
elgoog's user avatar
  • 77
1 vote
1 answer
267 views

Crystal lang: understanding method return type of Void

It's not documented on the docs. But through experiments I've found out that typing the return type of method as Void, cancels the "last statement return" (that is good) making it returning Nil. def ...
ClassyPimp's user avatar
3 votes
2 answers
159 views

How to use union in "if" statement [Crystal]

Following code works well and print "5.0" $x : Float64 $y : Float64 $x = 3.0_f64 $y = 2.0_f64 puts $x + $y Now, I change the code to support "nil". $x : Float64? $y : Float64? $x = 3.0_f64 $y = 2....
elgoog's user avatar
  • 77
0 votes
1 answer
74 views

Crystal If doesn't work

I have the following function def fcn(lst) if (lst.size == 0) return [] of Int32 end lc = lst.map{|a| a[1]} end When doing: lst = [] of Int32 puts(fcn(lst)) I get: Error in line 8: ...
user3166747's user avatar
4 votes
2 answers
420 views

Crystal How to check if the block argument is given inside the function

Suppose a function defined like this: def composition(text : String, k : Int32) : Array(String) kmers = Array(String).new (0 .. text.size - k).each do |i| kmers << text[i, k] yield ...
Wilbur Luo's user avatar
2 votes
2 answers
443 views

Crystal-lang httpget basic_auth

I am writing some code in Ruby... But I can't figure out how basic authentication works with Crystal-lang. In ruby I had to always use request.basic_auth but that probably doesn't work in Crystal ...
Romano's user avatar
  • 117
2 votes
1 answer
240 views

How can I read a slice with other size in crystal?

I would like to read 1st 2bytes of a file as “unsigned int”. I checked the thread "Crystal reading x bytes from file" and I could get 1st 2bytes with following code. File.open("./test/test_data") do ...
elgoog's user avatar
  • 77
0 votes
1 answer
641 views

Failing to send multiple files with curl to send-anywhere.com

I was trying to build a CLI tool in Crystal to use send-anywhere.com from the command line. Sending multipart is not builtin in Crystal but before writing my own I thought I would try to use cURL to ...
ItsASecret's user avatar
  • 2,619
2 votes
1 answer
693 views

Crystal lang fiber and web socket

I'm beginner in crystal. I have question, maybe somebody can help me. I use Kemal framework. Have this code: require "kemal" require "json" channel = Channel(Card).new post "/posts" do |env| ...
schumi's user avatar
  • 21
1 vote
1 answer
222 views

Crystal Lang: can't compile App with MySQL library on OSX El Capitan

I am migrating Crystal Lang development into a new machine running OSX El-Capitan. I have encountered a problem where when compiling an App with MySQL bindings, the crystal compiler throws this error:...
Araejay's user avatar
  • 222
3 votes
2 answers
2k views

Cannot find -lxml2 when trying to run crystal spec

Hello I get an error when trying to run crystal spec /usr/bin/ld: cannot find -lxml2 Is this just a dependency error or? I've tried googling but still can't find a solution.
Twin Potato's user avatar
2 votes
1 answer
661 views

Crystal reading x bytes from file

I have this code: a = File.open("/dev/urandom") b = a.read(1024) a.close puts b I expected to get the first 1024 bytes from the /dev/urandom device\file, instead I got an Error which says read ...
Ba7a7chy's user avatar
  • 1,511
1 vote
1 answer
199 views

Unix-style password readline

I understand how to read a string from STDIN (noted below), but my problem is that the characters are displayed on the screen. How can I make the string hidden like the Unix/Linux password prompts? ...
TheLonelyGhost's user avatar
2 votes
3 answers
294 views

How do I access an object's eigenclass in Crystal?

In Ruby, it's possible to access the eigenclass (or "singleton class") of an object by reopening it. This is particularly useful for defining "private class methods": class Foo class << self ...
andrewdotnich's user avatar
1 vote
1 answer
125 views

Why does my code never reaches the end when waiting for pipe?

I'm trying to use IO.pipe to send messages between processes, but I run into some kind of starvation when I'm waiting for finish messages. Code: https://carc.in/#/r/12ly
Hugo Abonizio's user avatar
2 votes
3 answers
712 views

how to override stdin with /dev/tty?

I'm writing a git pre-commit hook, but it requires user input and hooks don't run in an interactive terminal. With Python I could do something like this to get access to user input: #!/usr/bin/python ...
masukomi's user avatar
  • 10.6k
3 votes
1 answer
343 views

Bootstrapping from nothing

I'm interested in the Crystal programming language but I'm a bit confused: The compiler is written in Crystal. I know that you can bootstrap a language (write it in itself), but you still need some ...
R.O.S.S's user avatar
  • 605
3 votes
1 answer
812 views

Getting the first element of an empty Array

Given an array which has zero or one elements, how do I return either the first element or nil? I have working code, but it is not very elegant: class Category < ActiveRecord::Model adapter ...
Kris's user avatar
  • 19.5k
0 votes
1 answer
111 views

Errors when running Crystal Play

I have check and libevent is installed, GCC is installed. LLVM is updated. Running Crystal Play works and then crashes with the below error. I can see the localhost output. But it will try to run the ...
codedownforwhat's user avatar
1 vote
3 answers
906 views

Crystal-lang file/image upload http server

Is it possible to have a crystal based web server which handles file upload? I have been looking in the docs, and in many of the crystal web frameworks. I have not found any reference to a simple file ...
Automatico's user avatar
  • 12.6k
4 votes
1 answer
120 views

Get __FILE__ of compiled script

How can I get the compiled version of my Crystal script to get its own __FILE__. Here is an example. I have a file called ~/test.cr with the following contents: puts __FILE__ I compile the script ...
Some Dinosaur's user avatar
6 votes
1 answer
1k views

Would one be able to use Ruby gems with Crystal?

Developers say that Crystal follows Ruby language syntax. So can I (or would I, in the future) just require a Ruby gem and it magically builds and properly working and so on?
Vlad Faust's user avatar
24 votes
1 answer
1k views

Why is Crystal faster than Ruby?

I would very much like to know what exactly makes Crystal faster than Ruby while code is so similar. The short answer could be that it is compiled, and Ruby is interpreted, yet I would like to ...
Marcelo Boeira's user avatar
6 votes
1 answer
362 views

Shorthand block syntax in Crystal

Is it possible to use a shorthand for blocks in Crystal, e.g. my_array.sort_by(&:size) This attempt returns an error: ... expected a function type, not Symbol
Charlie Egan's user avatar
  • 5,058
2 votes
1 answer
214 views

is there a way to auto recompile with crystal (aka nodemon / supervisor )

I'm just learning the language and playing with a file I'd like to recompile each time there are changes. Is there an equivalent to watch or like nodemon in the node world? Alternatively a little ...
dcsan's user avatar
  • 11.8k
18 votes
2 answers
2k views

Is there an equivalent to Pry for Crystal?

I am very new in crystal language. I would like to know if a debugger like Ruby's Pry exists in Crystal? It means that you can put in code something like 'binding.pry' at program stop execution at ...
Voldemar Duletskiy's user avatar
7 votes
3 answers
4k views

How to execute a shell script in Crystal while capturing output?

I want to execute a shell script while handling stdout and stderr output. Currently I execute commands using Process.run, with shell=false and three pipes for stdin, stdout and stderr. I spawn fibers ...
Sod Almighty's user avatar
  • 1,768
5 votes
1 answer
452 views

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 : ...
Serhii Potapov's user avatar
6 votes
1 answer
427 views

Crystal lang, is it possible to explicitly dispose (free) of instance (object) without waiting for GC?

The title says it all. Maybe there's a method can be called like def destruct; delete self;end?
ClassyPimp's user avatar
0 votes
1 answer
106 views

Can variable type in Crystal lang be restricted to classes that inherit from one class

I mean: class Foo; end class Bar < Foo; end class Cux < Foo; end Is it possible to restrict type of the variable to classes inheriting from Foo without unions, something like x : Foo+?
ClassyPimp's user avatar
5 votes
1 answer
1k views

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 ...
Serhii Potapov's user avatar
13 votes
2 answers
3k views

How can I produce a Crystal executable with no dependencies?

I'm writing a program in Crystal, that I intend to compile and move to other systems for execution. Ideally, it should have no dependencies, as the target systems will be fresh installations of linux. ...
Sod Almighty's user avatar
  • 1,768
7 votes
1 answer
754 views

Is it possible to create Crystal binds for Ruby?

I'm creating a m3u8 generator/parser for crystal, yet I want to use it later with ruby. Is that possible/easy to do it?
Marcelo Boeira's user avatar
3 votes
1 answer
160 views

Standard library missing on Linux?

I've been using Crystal in OS X, installed with Homebrew. Now I'm giving it a try on Linux, but when I compile a project that includes URI from the standard library, I get: while requiring "URI": ...
Lye Fish's user avatar
  • 2,538
2 votes
1 answer
144 views

Why Object can't be type of argument for Proc object

I have such code: p = ->(x : Object) { x } puts p.call(3) and when I compile it I receive such exception: cannot be used as a Proc argument type yet, use a more specific type could someone ...
Oleh Devua's user avatar
2 votes
1 answer
192 views

Is it possible to manually fetch `yield` values instead of in a loop?

Taking this example from the docs, we can send a block that receives the yield values from the function. def twice yield 1 yield 2 end twice do |i| puts i end What I'm wondering is if we can ...
Lye Fish's user avatar
  • 2,538
1 vote
1 answer
541 views

Hash of (Array of Tuples) causing compile/runtime error "index out of bounds for tuple {(String, String -> Void)}"

I am trying to make a Hash of Arrays with tuples in them. This is causing a weird issue which I suspect is a compiler bug, but I am not 100% sure. The syntax I am using: class SomeClass @@...
Automatico's user avatar
  • 12.6k
20 votes
1 answer
2k views

Can a Crystal library be statically linked to from C?

I've read through the "C bindings" in the tutorial but I'm a novice at C stuff. Could someone please let me know if a Crystal program can be built as a static library to link to, and if so could you ...
Lye Fish's user avatar
  • 2,538
5 votes
1 answer
154 views

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? ...
Lye Fish's user avatar
  • 2,538
3 votes
1 answer
106 views

Pack the contents of arr into a binary sequence in Crystal

Is there a standard way to achieve the same result as in Ruby with Array#pack method: [1,2].pack­ "LL" => "\x01\x00\x00\x00\x02\x00\x00\x00"
Vitalii Elenhaupt's user avatar
3 votes
1 answer
334 views

Crystal build fails with linker error

I've just tried to run the simple HTTP server from the language documentation. The program fails with an error. /usr/bin/ld: cannot find -lssl collect2: error: ld returned 1 exit status Error: ...
Herbstein's user avatar
  • 309
3 votes
1 answer
1k views

How to open a connection to a WebSocket server in Crystal (use Crystal as client)

I would like to consume a websocket server connection using Crystal as a client. Does the Crystal standard library provide a WebSocket client library?
Rick's user avatar
  • 8,596
3 votes
2 answers
453 views

Retrieve a value from the first node in an XML::Nodeset using Crystal

I am using Crystal, and am trying to retrieve the ID of a node in an XML document: <foo ID="bar"></foo> I am using the following code to get to the ID require "xml" file = File.read("...
ReggieB's user avatar
  • 8,160
1 vote
1 answer
138 views

why is the assignment of instance variables to local variables is need in crystal?

In Crystal compiler source code I've seen such code def dirname filename = @filename if filename.is_a?(String) File.dirname(filename) else nil end end def original_filename case ...
Oleh Devua's user avatar
7 votes
2 answers
3k views

How to specify the data types for JSON parsing?

I have a JSON response which is an Array of Hash: [{"project" => {"id" => 1, "name" => "Internal"}, {"project" => {"id" => 2, "name" => "External"}}] My code looks like this: ...
Kris's user avatar
  • 19.5k
6 votes
1 answer
777 views

Crystal language: what to use instead of runtime String::to_sym

I am trying to convert a Ruby program to Crystal. And I am stuck with missing string.to_sym I have a BIG XML file, which is too big to fit in memory. So parsing it all is out of question. Fortunately ...
jsaak's user avatar
  • 637