Converting a Ruby Class to Crystal

If you’re a Ruby developer, converting classes is a place where the differences between the languages are especially visible. Walking through the transition from Ruby to Crystal with explanations of the error messages along the way can make it easier to see how Crystal’s use of types changes the story. (If you’re not a Ruby developer, feel free to skip to the next section.)

Here’s a simple class in Ruby:

 class​ Mineral
  attr_reader ​:name​, ​:hardness​, ​:crystal_struct
 
 def​ ​initialize​(name, hardness, crystal_struct)
  @name = name
  @hardness = hardness
  @crystal_struct = crystal_struct
 end
 end
 
 def​ ​mineral_with_crystal_struct​(crstruct, minerals)
  minerals.​ ...

Get Programming Crystal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.