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

Return Granite validation errors as JSON

I have a Granite User model with some validations. When someone makes a POST request to users/new, I'd like to return the validation errors (if any) as JSON. Currently, I have: if user.errors.size > 0 halt env, status_code: 500, response:…
Hyrum Carlile
  • 107
  • 3
  • 7
0
votes
0 answers

Amber framework : url_for in rails?

Is there something like url_for in rails to implement: url_for controller: CONTROLLER, action: ACTION, arg_1: ARG1, arg_2: ARG2 generates link /CONTROLLER_PATH/ACTION?arg_1:ARG1&arg_2:ARG2?
XQY
  • 552
  • 4
  • 17
0
votes
1 answer

Crystal-lang: How to Find End URL After a Redirect?

I'm just dipping a toe in the water with Crystal at the moment and, as an exercise, trying to port one of my Python scripts across. The script in question downloads the 'latest' PDF from a URL which takes the form:…
vlota
  • 3
  • 3
0
votes
0 answers

Crystal lang : How to get location of macro itself rather than macro argument.

One could get location of a ASTNode via ASTNode#line_number and other methods. However the problem is one can get ASTNode of a macro's arguments but macro itself. Something like self in macro isn't defined. The question may be induced to "How to get…
XQY
  • 552
  • 4
  • 17
0
votes
0 answers

Hello World in Crystal Lang not outputting to terminal

I am using the Linux subsystem with the Ubuntu distribution for Windows 10 to compile crystal code. I have created a directory which contains a text file called Hello.cr and placed inside of it puts "Hello World". I compile and run using crystal…
0
votes
1 answer

How to use content_for in kemal slang

content_for does not work for me. I use Kemal and Slang. Here code: layout.slang == yield_content "js_css" index.slang - content_for "js_css" do javascript: window.allowDigits = function(evt) { alert("msa7"); } In output HTML I do…
0
votes
1 answer

How to initialize an object created by from_json in Crystal

Take a look at this example: https://play.crystal-lang.org/#/r/4hqb Here's the code (don't know how long that link is good for...) require "json" class House JSON.mapping(address: String) getter first_part def initialize @first_part =…
KCE
  • 1,159
  • 8
  • 25
0
votes
1 answer

Crystal lang : Type arithmetic, belongs to a Union?

I'm trying to implement a fucntion: def foo(t : Class) if t in Int::Signed # ... end end But how to implement t in Int::Signed? Where Int::Signed I know is_a?(Int::Signed) but here the parameter is of type Type. Thanks.
XQY
  • 552
  • 4
  • 17
0
votes
1 answer

Crystal installation on WSL fails

I'm following the current crystal installation docs, my installation stops at the first moment and give the error: gpg: connecting dirmngr at '/tmp/apt-key-gpghome.4GKHZljOFL/S.dirmngr' failed: IPC connect call failed gpg: keyserver receive failed:…
Marcelo Barreto
  • 162
  • 1
  • 10
0
votes
1 answer

Crystal-Lang - Cross-Macro Macro-variables

So I'm building a data type where, I would like, optional auto-casting. The last question I asked is related to this also. The code I currently have can be found below: class Test(T) @@auto_cast = false def initialize(var : T) @var = var …
Sancarn
  • 2,575
  • 20
  • 45
0
votes
1 answer

crystal-lang : how to fill an array with all modules defined in files in a specific package of the project?

All in the question, I would like to know if it's possible to do something like this in Crystal (pseudo-code) : modules = Array(Module).new foreach (file in specific_package) do if (file.is_a(crystal_module) do modules.push(file.module) …
Mead
  • 100
  • 10
0
votes
1 answer

How to include large numbers of dependencies

In crystal, is there a better way to include large numbers of dependencies for a script, as opposed to lots of require "whatever" statements at the top? For example, I'm currently creating a web framework where I'm potentially anticipating a large…
ol'bob dole
  • 119
  • 8
0
votes
2 answers

How do I get a word inside a file at given position?

How to get a word inside a file at given position def get_word(file, position) File.each_line(file).with_index do |line, line_number| if (line_number + 1) == position.line_number # How to get a word at position.column_number ? end …
Faustino Aguilar
  • 823
  • 6
  • 15
0
votes
1 answer

Crystal GET request to HTTP fails when run on different platforms with a socket exception

Having a problem I'm bashing my head at - I have a GET request with the Crystal HTTP client, which runs on my machine, and also on the compiling container - crystallang/crystal, gets a good response and does its job. But moving the same binary to…
Omer H
  • 415
  • 5
  • 14
0
votes
1 answer

How to access module version in Crystal Lang?

I see that a frequent convention is to add a file often named version.cr or named after the module it is attached to. Inside of that file, there is a definition of the module with VERSION = "[semver]". How can I access that version?
dkimot
  • 2,239
  • 4
  • 17
  • 25