iyi

Playground · Methods and traits · 22 of 33

methods

Methods: functions that belong to a type.

samples/tour/methods.iyi · methods.wasm · 156 KB
# Methods: functions that belong to a type.

struct Rectangle
  getter width : Float64
  getter height : Float64

  def initialize(@width : Float64, @height : Float64)
  end

  def area : Float64
    width * height
  end

  def square? : Bool
    width == height
  end

  # A method can take arguments like any function.
  def scale(factor : Float64) : Rectangle
    Rectangle.new(width * factor, height * factor)
  end
end

r = Rectangle.new(3.0, 4.0)
puts r.area
puts r.square?
puts r.scale(2.0).area

# Methods on the built-in types work the same way.
puts "iyi".upcase
puts 3.7.round
puts [1, 2, 3].sum
output
Press Run.

recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19

You can type in the pane: nothing here compiles, so what you type cannot run, and the page says which bytes it is running the moment the text stops being the recorded file. There is no standard input, no filesystem and no threads on this target, and one sample fails on purpose for exactly that reason.

Run executes methods.wasm, the module the compiler built from this file, in your browser. How this works

Try it on your machine

Copy the program into methods.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.

sh, after installing
$ iyi run methods.iyi