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

Possible way to do `~{do_something}`?

I'm making a DSL in crystal, and I'm trying to get a syntax like ~{do_something} I tried something like this: def ~(&block) block.call end def my_func puts "hello" end ~ { my_func } But all I get is this error: Error in line 11: undefined…
bew
  • 483
  • 4
  • 9
0
votes
1 answer

Crystal: how to copy String::Builder s to each other

I want to write contents on one String::Builder to another, like: str1 = String::Builder.new str2 = String::Builder.new str1 << "foo" str2 << "bar" str1.copy_somehow_another_builder(str2) #=> "foobar" currently I just str1 << str2.to_s. How to do…
ClassyPimp
  • 715
  • 7
  • 20
0
votes
1 answer

Get instance property instead of undefined method

class Place @description = "Default place" def initialize(x : Int32, y : Int32, description : String) @x = x @y = y @description = description puts "Description of this place is: #{description}" end end require…
idchlife
  • 554
  • 1
  • 6
  • 16
0
votes
2 answers

How to limit a type to the class of a interface and not the instance of that interface?

Given the following Interface in a module: module Action abstract def perform end I would like to use it to instantiate different classes that implement it: class Run include Action def perform puts "run!" end end class Jump include…
Luis Lavena
  • 10,348
  • 1
  • 37
  • 39
0
votes
1 answer

Define Class object validations

Is there a native way to define validations for a Crystal object ? Let's consider this class: class Person def initialize(@age : Int32) end end How could I add a simple validation if age < 18 ? Ex: Person.new(10) >> Error: attibute 'age'…
Graham Slick
  • 6,692
  • 9
  • 51
  • 87
0
votes
1 answer

Application server for Crystal web app / Kemal

As Rails developer I'm used to Nginx + Unicorn in our servers. Are there similar solutions for Crystal web apps / Kemal ? I'm not a Nginx expert but I suppose I could use proxy_pass directly to the Crystal HTTP server or Kemal or fast-http-server...…
Mat
  • 2,156
  • 2
  • 16
  • 29
0
votes
2 answers

Unable to compile after upgrade from crystal 0.19.4 to 0.20

The shards install went well but compilation gave this error. Checked with icr that able to connect to local postgres database successfully. Any help is greatly appreciated! Thank you. [aranin@acbc328b9a5b:kemal-react-pg-chat-master]$ crystal…
0
votes
1 answer

Crystal convert string to hash from response.body

I am going some HTTP gets and the response body is this structure: response.body = "{\"temp\": \"val_one\", \"temp2\": \"val_two\"}" How do I convert this to a Hash, I want to do this: response.body.to_hash response.body["temp"] # =>…
shell
  • 1,867
  • 4
  • 23
  • 39
0
votes
1 answer

Array of Union-type of Class.class and Struct.class

I'm trying to get a list of classes of other classes and structs. I currently have the following code (reduced to minimum) struct Foo end class Bar end alias Baz = Foo.class | Bar.class types = [ Foo, Bar, Foo, Foo, Bar, Baz ] of Baz This…
Leonard Schütz
  • 517
  • 1
  • 7
  • 16
0
votes
1 answer

Crystal with Sidekiq I've got an error. What I'm doing wrong?

I'm trying to run an Sidekiq example in Crystal, but I've got an error in Sidekiq log: 2016-11-03T13:18:22.430Z 19329 TID-uvtuk WARN: {"queue"=>"default", "jid"=>"84853f6ac0bf8d434ec0beee", "class"=>"Sample::MyWorker", "args"=>["world", 3],…
nobilik
  • 736
  • 9
  • 29
0
votes
2 answers

Wrap a crystal object into a custom root object in JSON

I have a class like the following class Foo JSON.mapping( bar: String, baz: String, ) end I know that I can wrap single attributes in JSON objects by specifying {root: "name of node"} inside of JSON.mapping. But is there any way to do…
Kilian
  • 2,122
  • 2
  • 24
  • 42
0
votes
1 answer

Avoid repeating variable name in assignation

I think I have seen a way to reduce this: long_variable_name = long_variable_name.squeeze('i') To something like this: long_variable_name = &:squeeze('i') But it doesn't work and I must be confusing. Just wondering if there is actually a way to do…
Aurelien
  • 339
  • 3
  • 12
0
votes
1 answer

Sharing variables between different macros

How to access vars in macro that are set in another macro e.g. macro foo(arg) {% SHARED_VAR = arg%} p {{arg}} end macro baz p {{ SHARED_VAR }} end foo("foo") baz #=> prints "foo"
ClassyPimp
  • 715
  • 7
  • 20
0
votes
1 answer

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. Is it expected behavior?
ClassyPimp
  • 715
  • 7
  • 20
0
votes
1 answer

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.content_type = "text/plain" context.response.print…
1 2 3
43
44