DEV Community

hiko1129
hiko1129

Posted on • Originally published at note.hiko1129.com on

[Ruby][Sorbet] What to do when you can't write types in Ruby files (when meta-programming)

Translate ja to en

メタプログラミングしている(define_methodとか使っている)ときは.rb上では型を付与できない。

ex: hoge.rb

class Hoge
  define_method :hoge do
    puts 'hogehoge'
  end
end

Hoge.new.hoge
Enter fullscreen mode Exit fullscreen mode

この場合には、RBIファイルに型を書く対応をする(or 型生成するコードを書く

ex: hoge.rbi

# typed: strict

class Hoge
  sig { void }
  def hoge; end
end
Enter fullscreen mode Exit fullscreen mode

bin/tapioca gemsなどを実行して型が足りないときも同様にsorbet/rbi/shims以下に型を書く(必要な部分のみで問題ない。)

# typed: strict

class ActiveRecord::Base
  sig { void }
  def hoge; end # <- こんな感じで必要な差分のみ追記で問題ない
end
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay