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

Crystal method return multiple values

Can Crystal-lang method return multiple values with specific type? I know that it can be implemented in this way: def my_method(arg_1 : Int, arg_2 : String) : Tuple return arg_1, arg_2 end res_1, res_2 = my_method(1, "1") but it also work if I…
Oleh Sobchuk
  • 3,612
  • 2
  • 25
  • 41
4
votes
2 answers

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 text[i, k] end return kmers end How do I check if…
Wen-Bin Luo
  • 147
  • 1
  • 15
4
votes
1 answer

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 via Crystal ~$ crystal ~/test.cr -o…
Some Dinosaur
  • 154
  • 1
  • 14
3
votes
2 answers

Why does array "each" do not work anymore in Crystal 1.3.0? What should I use instead?

The code below does not print the elements of the array. Why is that? array = [1, 2, 3] array.each do |x| puts x end array.each { |x| puts x } Crystal Play Output It used to work in previous versions. I know Crystal is not Ruby, but the code…
3
votes
1 answer

Scope variables/guards

Is it possible to have a variable that is guaranteed to be finalized on the scope exit. Specifically, I want a guard: something that calls a specific function on initialization, and calls another specific function on scope exit.
Hrisip
  • 900
  • 4
  • 13
3
votes
1 answer

How do I make generic memoization in Crystal?

I want to define a generic memoizing wrapper in Crystal. I have the following crystal code: module Scalar(T) abstract def value: T end class ScSticky(T) include Scalar(T) def initialize(sc : Scalar(T)) @sc = sc @val =…
monomonedula
  • 606
  • 4
  • 17
3
votes
2 answers

Purpose of no-codegen option on crystal build?

What is purpose of no-codegen option when building crystal project ? I have quite large codebase and when building without this option, it can take up to 20 seconds to build. When I use no-codegen option, it reduces build time to 6 seconds. But I…
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
3
votes
1 answer

Why can't Crystal resolve the type of the assignment of 1 + 1?

What's the reason why Crystal can't/won't resolve the type of this? (I see that the documentation does not mention that the compiler could infer instance method calls, but what's the rationale behind it, especially when only stdlib functions are…
3
votes
4 answers

How to read a certain number of characters (as opposed to bytes) in Crystal?

In Crystal, if I have a string (or a file), how do I read a certain number of characters at a time? Using functions like IO#read, IO#gets, IO#read_string, and IO#read_utf8, one can specify a certain number of bytes to read, but not a certain number…
obskyr
  • 1,380
  • 1
  • 9
  • 25
3
votes
1 answer

How do I check which Operating System (OS) is used in Crystal?

Is there something similar to Ruby's OS gem which allows me to check if I am running on Mac, Linux or Windows?
3
votes
2 answers

Created Expected created to be (Time | Nil) but got (Float64 | String)

I am new to this amber framework,crystal lang and object oriented programming in general . I was following the tutorials and tried to create a simple form using this scaffold amber g scaffold item name:string path:string type:string size:float…
gurrurin
  • 41
  • 2
3
votes
1 answer

How do I call instance method from string?

Let's say I have a class class MyClass def sayMyName() puts "I am unknown" end end and I have stored this method name in a variable: methodName = "saymyName" I want to call this method by using above variable, something like this: instance…
Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32
3
votes
2 answers

How to pass method to block in Crystal

How to pass plus into calculate method? def calculate(&block : (Float64, Float64) -> Float64) block.call(1.1, 2.2) end def plus(a, b) a + b end calculate{|a, b| plus a, b} This won't work calculate ->plus calculate &plus P.S. Another…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
2 answers

Is it possible to do one-line loops in Crystal?

I'm just learning Crystal and was reading a lot about the language. Coming from Ruby, I'm trying out some things that I was used to before, e.g., one-line loops: x += 1 until x === 5 However, this is seemingly not supported by Crystal, is it? In…
q9f
  • 11,293
  • 8
  • 57
  • 96
3
votes
2 answers

Can Hash be sorted by keys or values?

Is it possible to sort Hash by key or by value in following code: myhash.each_key do |key| print myhash[key], "\t:\t", key, "\n" end
rnso
  • 23,686
  • 25
  • 112
  • 234