Code · 1 file

Sample Ruby File

Ruby is a dynamic language known for Rails and developer-friendly syntax. The Ruby sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.

Ruby · 461 B

Sample Ruby file

An idiomatic Ruby example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.

CC0 · free for any use · Verified by an independent tool · Report a problem

sample.rb — first linesRuby
# Sample Ruby program — samplefiles.com
Customer = Struct.new(:id, :name, :balance) do
  def vip?
    balance >= 10_000
  end
end

def total_balance(customers)
  customers.sum(&:balance)
end

customers = [Customer.new(1, "Ada", 12_500.0), Customer.new(2, "Grace", 830.5), Customer.new(3, "Linus", 10_000.0)]
customers.each do |c|
  puts format("%-6s %10.2f %s", c.name, c.balance, c.vip? ? "VIP" : "")
end
puts format("Total: %.2f", total_balance(customers))

File details

Size
461 B (461 bytes)
MIME type
text/x-ruby
SHA-256
3c03fd4f3c58b75ca10af8f676d39e5c73fb3833000371558dd9dcdbde26f334
MD5
82d8f9f14bf89cc7840b6dfd0acb3acb

Download from the command line

curl -L -o sample.rb https://files.samplefiles.com/code/rb/sample.rb

Or check the download in your browser with the checksum calculator.