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
3
votes
1 answer

Recursive block expansion error when recursively yielding

I was looking to implement Python's os.walk method in Crystal. I was trying to do it recursively, but the compiler was told me to be careful about recursive yielding because it recursively/infinitely would generate code when compiled. This is what I…
mlobl
  • 155
  • 1
  • 11
3
votes
1 answer

What is superclass mismatch in Crystal Lang?

I am trying to implement a rate-limiting handler with Kemal. I have a class, RateLimiter, that inherits the class Kemal::Handler. On compile I get the error: Error in src/rate_limiter.cr:5: superclass mismatch for class RateLimiter (Kemal::Handler…
dkimot
  • 2,239
  • 4
  • 17
  • 25
3
votes
1 answer

What's the idiomatic way to join an array into a string?

Many languages support constructs like the following (Python, in this case): >>> ["One", "two", "three"].join(" and ") "One and two and three" In Crystal, what is the idiomatic way to do this?
obskyr
  • 1,380
  • 1
  • 9
  • 25
3
votes
1 answer

programming and running crystal on windows

I need to work with crystal on windows. Does anyone know about an environment for windows? And how to I run the files I wrote - if for example for now I'm writing in notepad? thank you!
Rachelle
  • 54
  • 3
3
votes
1 answer

Is it possible to get the inferred return type of a method call to be used in a macro?

If the return type of a method is not stated, is it still possible to get the inferred return type to be used in a macro? class Record def explicit : String "name" end def inferred ["a", "b"] end end # The following works: puts…
dgo.a
  • 2,634
  • 23
  • 35
3
votes
1 answer

Why creating StaticArray with size from a variable throws an error?

error when creating StaticArray with size from variable I get this error (see image) but I don't know how to resolve this ? code : t = 3 seps = StaticArray(Int32, t).new{ 2 } seps.each{|i| p i} error : Syntax error in eval:2: expecting token…
Mead
  • 100
  • 10
3
votes
1 answer

Crystal: Class+ is not a class, it's a Class+

While experimenting with the online crystal compiler (which is awesome), I've run into an error which I can't seem to find an explanation for: class Person class Current < self end class Destroyed < self end end Error: Person+ is not a…
John
  • 9,249
  • 5
  • 44
  • 76
3
votes
2 answers

What is the difference between JSON::Any and JSON::Type in Crystal?

In Crystal language, what is the difference between JSON::Any and JSON::Type? What are the use cases of this types?
vtambourine
  • 2,109
  • 3
  • 18
  • 27
3
votes
1 answer

Replace characters in string like in python

In python there's function replace(old, new) which replaces "old" to "new" in some string, is there any function like this or any way to do it in Crystal?
LavX64
  • 105
  • 1
  • 1
  • 5
3
votes
1 answer

Opening a DGRAM socket from within a docker container fails (permission denied)

I'm running an application which builds and sends ICMP ECHO requests to a few different ip addresses. The application is written in Crystal. When attempting to open a socket from within the crystal docker container, Crystal raises an exception:…
voxobscuro
  • 2,132
  • 1
  • 21
  • 45
3
votes
1 answer

Crystal equivalent shard for "pp" (pretty printing)

What is the Crystal equivalent shard for "pp" used in Ruby for pretty printing complex data structures ?
JCLL
  • 5,379
  • 5
  • 44
  • 64
3
votes
2 answers

Crystal: Ensure return value is not Nil

I have a helper class defined as follows: require "toml" module Test class Utils @@config def self.config if @@config.is_a?(Nil) raw_config = File.read("/usr/local/test/config.toml") @@config =…
James Taylor
  • 6,158
  • 8
  • 48
  • 74
3
votes
2 answers

Is it possible to beautify the output of :to_json (in Crystal's standard lib)?

I'm using Crystal's JSON module to update package.json files and all the output is on one line. Is it possible to control the output of :to_json? require "JSON" data = File.read("package.json") data.as_h["version"] = "X.X.X" puts data.to_json
dgo.a
  • 2,634
  • 23
  • 35
3
votes
2 answers

Where to start tracing down an exception?

I'm getting an exception in production which isn't providing and stacktrace information. How do I start debugging where this might be coming from? Oct 25 16:26:17 socket-proxy app/web.1: Exception: RedisError: Disconnected (Redis::DisconnectedError)…
Daniel Westendorf
  • 3,375
  • 18
  • 23
3
votes
1 answer

.to_json on Array() not working in Crystal

I have a class: class User property id : Int32? property email : String? property password : String? def to_json : String JSON.build do |json| json.object do json.field "id", self.id …
twharmon
  • 4,153
  • 5
  • 22
  • 48