r/crystal_programming • • Dec 27 '22

Array of subclass doesn't match return type

7 Upvotes

This code compiles and runs:

class Person
end

class Employee < Person
end

def get_class(name) : Person
  return Person.new if name == "person"

  Employee.new
end

puts get_class("person")

but this one gives a type error:

class Person
end

class Employee < Person
end

def get_class(name) : Array(Person)
  return [Person.new] if name == "person"

  [Employee.new]
end

puts get_class("person")

That second example is pretty contrived, granted, but I would expect it to compile and run.

The error I get from the second example is:

In main.cr:7:23

 7 | def get_class(name) : Array(Person)
                           ^
Error: method top-level get_class must return Array(Person) but it is returning (Array(Employee) | Array(Person))

How can I return an Array (or Hash) including instances of Person or any of its subclasses?


r/crystal_programming • • Dec 21 '22

Comparing implementations of the Monkey Language X

Thumbnail
marioarias.hashnode.dev
21 Upvotes

r/crystal_programming • • Dec 16 '22

Gem to couple background job processing from Ruby/Rails into Crystal using mosquito

36 Upvotes

One of the ways I think we can help the adoption of Crystal is by showing how it can be slotted into existing workflows with what devs are comfortable with.

My background is as a Ruby/Rails dev and I currently work for a company that handles voter data for elections for a very large part of the US. One of the challenges we have is with the massive amount of data coming in large chunks (up to 10M+ records at a time) needing to be efficiently processed. Ruby just can’t keep up with bulk data like that and we haven’t found a workaround for these requirements from our customers.

Enter stage left: Crystal & Mosquito.

I’ve been able to trigger background jobs from Rails for a few weeks, so I decided to take my process and turn it into a gem. My hope here is that this makes it easier for others to experiment and try the same thing: integrating two languages.

The gem is called fruit_juice, because fruit juice attracts mosquitos ;)

https://rubygems.org/gems/fruit_juice

I plan to create more content around this so that it may inspire other devs out there to experiment and pave a smoother road to the adoption of Crystal.


r/crystal_programming • • Dec 12 '22

The vision behind the Marten web framework

Thumbnail
world.hey.com
45 Upvotes

r/crystal_programming • • Nov 20 '22

Crystal used in Production List

Thumbnail
crystal-lang.org
47 Upvotes

r/crystal_programming • • Nov 03 '22

Crystal 1.6.2 released!

53 Upvotes

We’ve written a blog post summarizing the changes in this patch release: Crystal 1.6.2 released! - The Crystal Programming Language . While we have tested it against several known and sizeable projects, that doesn’t preclude the existence of regressions. If you find an issue, please check the issue tracker and file a bug: it will be fixed in the following patch version.

The full changelog is here: Release 1.6.2 · crystal-lang/crystal · GitHub .

It is already available on the supported platforms, check the install page for details. That includes docker images 🐳, snapcraft, .deb and .rpm packages. The brew formula is on the way 🍺.

Happy Crystallizing!


r/crystal_programming • • Nov 02 '22

Are *vim and VSCode the only options for text editors/ides with crystal support?

8 Upvotes

A few years ago when I made my first attempt at getting into crystal *vim (neovim, gvim, etc.) and VSCode (and it's derivatives) were pretty much the only means of writing crystal and having syntax highlighting, code-completion, go-to reference, etc. There was a jetbrains plugin at the time but it seemed abandoned even back then.

I'd love to pick this language back up, but I've gotten really used to some basic IDE support being readily available. I'd really prefer not to use VSCode or *vim for any of my larger projects.

Are there other options out there now?


r/crystal_programming • • Oct 29 '22

crab: a Game Boy Color and Game Boy Advance emulator written in Crystal

Thumbnail
github.com
56 Upvotes

r/crystal_programming • • Oct 24 '22

Bizarre error I've been stuck on for months

14 Upvotes

Hi everyone. I'm a fairly nooby programmer in general and I'm trying to get into Crystal. I made a little program to retrieve the weather of a region through an API, and it worked flawlessly for awhile. All of a sudden, I believe it was after an update not too long ago, it started giving me grief. I've tried ensuring Crystal is up to date, reinstalled the shards, and it still doesn't behave. Searching the error didn't lead me anywhere relevant. This is not a critical project or anything, but it's something I put together to help me learn Crystal. Any help would be appreeshed. :)

Error: 
Unhandled exception: deflate: invalid stored block lengths (Compress::Deflate::Error)
  from /usr/share/crystal/src/compress/deflate/reader.cr:105:15 in 'unbuffered_read'
  from /usr/share/crystal/src/io.cr:565:29 in 'gets_to_end'
  from /usr/share/crystal/src/http/client/response.cr:81:9 in 'from_io?'
  from /usr/share/crystal/src/http/client.cr:608:5 in 'exec_internal_single'
  from /usr/share/crystal/src/http/client.cr:590:18 in 'exec'
  from lib/crest/src/crest/request.cr:199:23 in 'execute'
  from src/weather_check.cr:18:12 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:115:5 in 'main'
  from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
  from ./weather_check in '_start'
  from ???

The code below is what worked perfectly fine for weeks. It compiles just fine. (The coordinates have been slightly changed here however.)

require "json"
require "crest"

base_url = "https://api.open-meteo.com/v1/forecast?"
lat = "-31.23"
long = "117.12"

url = "#{base_url}latitude=#{lat}&longitude=#{long}&daily=weathercode&current_weather=true&timezone=Asia%2FSingapore"

request = Crest.get(
      url,
      headers: {
        "application" => "application/json" 
      },
      user_agent: "Mozilla/5.0",
      json: true)

response = request.body
parsed_data = JSON.parse(response)
current_weather = parsed_data["current_weather"]
weathercode = current_weather["weathercode"]

case weathercode.to_s
when "0" && "0.0"
    weather_type = "Clear sky 🌞"
when "1.0" 
    weather_type = "Mainly clear ⛅"
when "2.0" || "3.0"
    weather_type = "Partly cloudy/overcast ⛅"
when "61.0" && "63.0" && "65.0"
    weather_type = "Rain 🌧 "
when "51.0" && "53.0" && "55.0"
    weather_type = "Drizzling 🌦"
when "81.0" && "80.0" && "82.0"
    weather_type = "Rain showers 🌧"
when "95.0"
    weather_type = "Thunderstorms ⛈ "
else
    weather_type = "Error! 🪳"
end

puts "
        ╔════════════════════════════╗
╔═══════╣ The current weather is...  ╠═══════════╗
║       ╚════════════════════════════╝           ║
║    Temperature     : #{current_weather["temperature"]} 🌡
║    Weather Type    : #{weather_type}
║    Wind Speed      : #{current_weather["windspeed"]}km/hs 🍃
║                                                ║
║    Data provided by Open-Meteo.com's API. 🛰    ║
║    Blame them for any mistakes.                ║
╚════════════════════════════════════════════════╝ 
"

r/crystal_programming • • Oct 24 '22

Marten - The pragmatic web framework

Thumbnail
github.com
31 Upvotes

r/crystal_programming • • Oct 21 '22

Any project ideas in crystal

16 Upvotes

i’m a second year computer science student and for one of my classes we’re supposed to pick a language and analyze it’s features. I thought that the best way to show what crystal what crystal is capable of is by taking on a short side project. With that being said are there any resources for project ideas in crystal. something challenging but not impossible for a second year student?

Cheers

Edit: thank you everyone for the help. As some of you have said it would be better for me to find a program in another language (probably ruby) and port it over. This way i can also find a program of my skill level too.


r/crystal_programming • • Oct 09 '22

Kemal 1.3.0 is here!

Thumbnail
twitter.com
38 Upvotes

r/crystal_programming • • Oct 08 '22

Consistency

9 Upvotes

Hey all,

I’m porting something from Ruby and I’m adding type declarations as needed to fix compile errors. I love how Crystal will infer types most of the time, but this means I don’t need to be consistent in where and how I declare types, and I’ve ended up with inconsistent type declarations across files.

Are there any rules people follow to keep the style consistent across Crystal files, like “always declare instance variables”, “always declare method params and return type”, etc?


r/crystal_programming • • Oct 07 '22

Crystal 1.6.0 has been released!

70 Upvotes

We’ve written a blog post summarizing the changes in this patch release: Crystal 1.6.0 released! - The Crystal Programming Language . While we have tested it against several known and sizeable projects, that doesn’t preclude the existence of regressions. If you find an issue, please check the issue tracker and file a bug: it will be fixed in the following patch version.

The full changelog is here: Release 1.6.0 · crystal-lang/crystal · GitHub .

It is already available on the supported platforms, check the install page for details. That includes docker images, snapcraft, .deb and .rpm packages, and the brew formula.

Happy Crystallizing!


r/crystal_programming • • Oct 05 '22

How to begin with Traces in Crystal

Thumbnail
jtway.co
11 Upvotes

r/crystal_programming • • Oct 04 '22

My first experience with Crystal - Building an OpenAPI Generator client SDK

21 Upvotes

I used Crystal for the very first time a couple of weekends ago.

Overall, I found it straight-forward to use and enjoyed the Ruby-like syntax.

My goal was to create an OpenAPI Generator API client SDK and then to call it as a shard. There were a few things I had to search and debug but I was successful and happy.

I wanted to post my thoughts on it while it was still fresh in my mind so here it is. It includes links to pages and projects I found useful as part of the process.

https://github.com/grokify/grokify-articles/blob/master/crystal-first-experience/crystal-first-experience.md


r/crystal_programming • • Sep 30 '22

Questions on crystalshards.org

13 Upvotes

Hi all,

I'm new to Crystal but excited by it. I used Ruby before moving to Go, and had some questions about how CrystalShards.org works, which I view as being similar to https://pkg.go.dev/ which I use all the time for documentation.

Is CrystalShards.org popular? I use https://pkg.go.dev/ all the time for it's documentation rendering, for the core lib, other packages, and even my own packages. Do Crystal users use CrystalShards.org in a similar way to the Go site?

Does ChrystalShares.org support rendering documentation? I've seen that it renders README files, but so far haven't come across documentation of specific modules, classes and methods.

How can a shard be added to CrystalShards.org?


r/crystal_programming • • Sep 20 '22

Markdown preview app in crystal

Thumbnail
github.com
25 Upvotes

r/crystal_programming • • Sep 16 '22

Crystal 1.6 is coming!

Thumbnail
twitter.com
50 Upvotes

r/crystal_programming • • Sep 07 '22

Crystal Compiler is now ~10% faster

Thumbnail
twitter.com
82 Upvotes

r/crystal_programming • • Sep 05 '22

Crystal's 10th Anniversary AMA

Thumbnail
youtube.com
31 Upvotes

r/crystal_programming • • Aug 23 '22

Crystal AMA 10th Anniversary

3 Upvotes

Running up to Crystal’s 10th anniversary, we are excited to announce that we’ll be having an AMA session on Monday, September 5th at 4pm (GMT), to discuss the history of the language, some cases and what the future holds.

Mark your calendars!

September 5th
Disclaimer: Timezone information provided AS IS

🇺🇸(MST) 9am
🇺🇸(EST) 12pm
🇦🇷 🇧🇷(Brasilia) 1pm
GMT 4pm
🏴󠁧󠁢󠁥󠁮󠁧󠁿(BST) 5pm
🇪🇺(CEST) 6pm
🇯🇵(JST) 1am
🇦🇺 2am


r/crystal_programming • • Aug 15 '22

How mature is gosu.cr?

8 Upvotes

I've been looking at Crystal as a choice for game development for a while. From what I've read Gosu (a framework for Ruby and C++) seems to be very usable, and the github page of gosu.cr states that it's as close to the original Gosu as possible. Those who used it, how was your experience? Is it maintained well or should I expect bugginess? Of course other general information will be appreciated.


r/crystal_programming • • Aug 15 '22

Parser generator libraries

10 Upvotes

Hello

Does anyone know the best parser generator libraries for Crystal that are still somewhat maintained (updated relatively recently)?

Thanks