Explain Ruby code

Paste Ruby and Vibe explains the blocks, symbols and mixins that keep it so short.

member.rb

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe25 lines selected

Press Explain it for the sample, or paste your own code first.

What this Ruby sample shows

mixins
include Greeting adds greet to every Member. include Comparable adds comparison methods such as <, >, == and between?, all built on the <=> method the class defines.
blocks
{ |m| m.points > 20 } is a block passed to select, which keeps the members the block returns true for.
symbols
:name and :points are symbols. map(&:name) turns the symbol into a block that calls name on each member.
metaprogramming
attr_reader :name, :points writes the two reader methods for you when the class is defined, so points works without a def points.

Good to know

  • <=> returns -1, 0 or 1. max and sort only need that method, so the script prints Hi, I'm Ben, then Ana, Ben, then ["Cy", "Ana", "Ben"].
  • greet calls name without self., because a method from a module runs on the object it was mixed into.

Watch out: Comparable also defines == from <=>, so two different members with the same points count as equal: Member.new("Al", 5) == Member.new("Bo", 5) is true.

Explain Ruby code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).