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

How to you convert an Int32 into a string (base 16) with a leading 0x?

On the shell, I convert and format a number (eg 29360134) into a string, "0x1c00006": printf "%#08x" 29360134 # -> 0x1c00006 But, in Crystal I get a different result using the same format string: puts("%#08x" % 29360134).inspect # ->…
dgo.a
  • 2,634
  • 23
  • 35
0
votes
1 answer

What's the proper way to get a local time when TZ=":/etc/localtime"?

My environment variable TZ is set to :/etc/localtime. File /etc/localtime is a symbolic link to file /usr/share/zoneinfo/America/Chicago. So far I am using this to get a local time object: Time.local( …
dgo.a
  • 2,634
  • 23
  • 35
0
votes
1 answer

App compilations in centos crystal obtain error

I have the following code: require "kemal" get "/" do "Hello Kemal!" end Kemal.run But when I'm going to launch the application in centos it tells me the following: crystal run app /usr/bin/ld: it can not be found -lz /usr/bin/ld: it can not…
zeros
  • 101
  • 1
  • 4
0
votes
1 answer

type narrowing not work if type is specified?

class Foo def initialize(@foo : String | Nil) end def foo @foo end end a = Foo.new "213" if !a.foo.nil? puts a.foo, typeof(a.foo) end get output of 213 (String | Nil) but shouldn't type of a.foo be narrowed to String? Is this…
Austaras
  • 901
  • 8
  • 24
0
votes
2 answers

How to force return value to be same type as argument value in Crystal?

If I have a function which accepts an argument of multiple types, how can I enforce that the return must match the value of the input? This comes up particularly when I want a method to work with any children of a parent type. For demonstration,…
KCE
  • 1,159
  • 8
  • 25
0
votes
1 answer

Seeking dynamic method dispatch via automated construction of Hash

I build stochastic simulation models. The state transitions are handled by event methods, but the order of occurrence varies depending on the randomness of the events so I need some form of dynamic dispatch. This has worked beautifully for me for…
pjs
  • 18,696
  • 4
  • 27
  • 56
0
votes
1 answer

crystal class with macro not compiling

for class Y def initialize(@a : String) end getter a end class X macro test(name) @{{name}} = y.{{name}} end @a : String def initialize(y : Y) test a end end I got instance variable '@a' of X was not initialized directly in…
Austaras
  • 901
  • 8
  • 24
0
votes
0 answers

Crystal how to require implementing class operate on self, instead of all siblings

Let's say I want my method to accept anything that is "number like" i.e. knows how to negate, add, subtract, multiply and divide. It needs to do these with itself and with numbers (Int32 and Float64 for my purposes) abstract struct Numberlike …
KCE
  • 1,159
  • 8
  • 25
0
votes
1 answer

Array filtering does not change the array in Crystal

I got a Array of strings where i want to make a statement if something is true filter away this string in my array. The code module CrystalTest files = ["path/to/file1.ext", "path/to/file2.ext", "path/to/file3.ext"] files.reject do |file| …
Nopzen
  • 566
  • 2
  • 6
  • 19
0
votes
2 answers

Recursive type not unifying with itself

The following code fails to compile with error: type must be Tuple(Thing::Ish, Slice(UInt8)), not Tuple(Array(Array(Thing::Ish) | UInt8) | UInt8, Slice(UInt8)) These two types seem equivalent to me... and adding an .as(Ish) in the right place…
singpolyma
  • 10,999
  • 5
  • 47
  • 71
0
votes
1 answer

Is it possible to compile a Crystal script so it can run on any Linux machine without the user having to download Crystal

There is a Crystal equivalent of OCRA (One-Click Ruby Application Builder) but it's only for Windows and I use Linux.
tequila
  • 69
  • 4
0
votes
1 answer

instance var of CategoryController must be (Array(Array(Array(Int64 | String) | Array(String))) | Nil)

unable to store data contain in abc. it's showing an error instance variable @ar of CategoryController must be (Array(Array(Array(Int64 | String) | Array(String))) | Nil), not Array(Array(Int64 | String) | Array(String))) code sample: abc = [["",…
0
votes
1 answer

How to get File's atime and ctime (Crystal-lang)

(Newbie here). Trying to get a file's last access_time and creation_time. I have found how to get modification time thusly. File.info("foo").modification_time but cannot find anything for the other two. (Using latest 0.28) on OSX High Sierra.
rahul
  • 598
  • 1
  • 9
  • 20
0
votes
1 answer

collection dropdown default blank value

how to add default blank value in select field (amber framework)? == select_field(name: "parent_category_id", collection: Category.all.map{|category| [category.id, category.name]}, selected: "", class: "form-control")
0
votes
1 answer

I can’t use mmap to share a Hash between processes

I am implementing a multi-process library that provides a data structure for shared memory. But I am having trouble now, I modified the shared Hash object in the child process, but the parent process still has not read the changed value. Sample…
Hentioe
  • 228
  • 1
  • 9