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

How to execute code from a string variable in Crystal?

I like eval in Ruby because it works pretty straightforward: eval("puts 7 * 8") # => 56 What is an eval's equivalent in Crystal ? I know that we can do something similar with macro: macro eval(code) {{code.id}} end eval("puts 7 * 8") # => 56 But…
Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43
4
votes
1 answer

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 : UInt8 OP_RETURN = 1 end and an array of…
Alectionik
  • 51
  • 4
4
votes
2 answers

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 users typed in.
bichanna
  • 954
  • 2
  • 21
4
votes
2 answers

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 error during compilation Undefined symbols for architecture arm64: …
4
votes
1 answer

Why Crystal fails to infer type for instance variable?

Why this code fails for the instance variable? a = 4.days # Works class A @a = 4.days # Fails end P.S. Are there any plans to improve it in the future? Seems to be very common and useful thing.
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
4
votes
2 answers

Error: undefined method 'x' for Nil (compile-time type is (Point | Nil))

I'm writing a test that checks coordinates of a point to have a certain value, e.g.: it "should work" do p = do_something # returns a Point(x, y) p.x.should eq 0 # errors (see below) end But it fails to compile with the following…
q9f
  • 11,293
  • 8
  • 57
  • 96
4
votes
1 answer

How can I stop a Fiber?

Let's say we have a Fiber with a long running loop and with the given timeout channel when we receive message we want to stop the fiber and restart it(or do something else). Here is the pseudo code: # we have a timeout channel fiber = spawn do #…
Oguz Bilgic
  • 3,392
  • 5
  • 36
  • 59
4
votes
1 answer

A Library For Web Scraping In Crystal

I have been using Scrapy with Python for web scraping, is there anything similar for Crytal? I tried to make a Google search but I found nothing.
Caffeinatedwolf
  • 1,217
  • 3
  • 20
  • 26
4
votes
2 answers

How to generate a random number in Crystal?

In Crystal, how can I generate a random number? Using Python, I can simply do the following to generate a random integer between 0 and 10: from random import randint nb = randint(0, 10)
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
4
votes
1 answer

Error in writing Python functions in Crystal-lang

I am trying to write some python function in crystal-lang through the C Python API. My code follows: METH_VARARGS = 0x0001 @[Link("python3.5m")] lib Python alias PyObject = Void* struct PyMethodDef name : UInt8* func : Void* …
Max
  • 145
  • 9
4
votes
2 answers

Crystal get from n line to n line from a file

How can I get specific lines in a file and add it to array? For example: I want to get lines 200-300 and put them inside an array. And while at that count the total line in the file. The file can be quite big.
Eman
  • 43
  • 3
4
votes
1 answer

How do I install an older version of Crystal Lang?

The documentation explains how to install the newest version, but I need a specific (older) version. Could someone help me with the best way to accomplish that?
MRyno
  • 61
  • 2
4
votes
1 answer

Crystal-lang no overload matches 'HTTP::Server.new' with type Int32 http server

I'm a newbie to Crystal-lang. I'm trying the Http Server example given in the crystal-lang docs. require "http/server" server = HTTP::Server.new(8080) do |context| context.response.content_type = "text/plain" context.response.print "Hello…
girish946
  • 745
  • 9
  • 24
4
votes
1 answer

How to restrict type of a function argument

Let's say I have a following function: def encode(obj) case obj when Int32 "i#{obj}e" when String "#{obj.size}:#{obj}" when Symbol encode(obj.to_s) when Array obj.reduce "a" {|acc, i| acc + encode(i)} + "e" else raise…
Anon
  • 97
  • 6
4
votes
1 answer

Declaring union types with nil in Crystal

I've been following the official documentation of Crystal but I couldn't find any details on this. The regular syntax when declaring a union type is String | Int32. However, I've noticed a difference regarding the Nil type. The regular way of…
kajetons
  • 4,481
  • 4
  • 26
  • 37