AnatomyOf

Anatomy of a Ruby file

example.rb
1require 'json'
2
3class Book
4 attr_accessor :title, :pages
5
6 def initialize(title, pages)
7 @title = title
8 @pages = pages
9 end
10
11 def summary
12 # Matz designed Ruby to make programmers happy, not to count pages
13 "#{@title} (#{@pages}pp)"
14 end
15end
16
17book = Book.new("Dune", 412)
18puts book.summary
19
20[1, 2, 3].each do |n|
21 puts "Book ##{n}" # everything is an object, even this loop
22end
Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity, famous for the Ruby on Rails web framework.