Skip to content
Search

Ruby

lastmod 2026-04-27created 2015-06-0734 edits

Ruby is a dynamic, object-oriented scripting programming language developed by Yukihiro Matsumoto (松本行弘). Ruby is a pure object-oriented language. Everything in Ruby, including data types such as integers and strings, is an object.

Features include class definitions, garbage collection, powerful regular expression handling, multithreading, exception handling, iteration, closures, Mixin, operator overloading, and more. The syntax inherits from the ALGOL family while emphasizing readability.

For a long time the language specification was not written down and was considered to follow Matsumoto's implementation, but since June 2010 the language specification has been in the works as RubySpec.

Categories

Syntax

Libraries

  • timetrap - Timetrap is a simple command-line time tracker written in Ruby. It provides an easy-to-use command-line interface for tracking the time you spend.

Variables

Signature

Brief

Example

$

Global variable

$name = 'yundream'

@

Instance variable; simply put, a class member variable

@name = 'yundream'

[a-z_!]

Local variable || name = 'unknown'

[A-Z!]

Constant || NAME = "KIM"

Special variables

  • $!: last error message
  • $@: error location
  • $_: string most recently read by gets
  • $.: line number of the code
  • $&: string last matched by regexp
  • $~: the last regexp match, as an array of subexpressions
  • $n: nth matched string
  • $=: case-insensitivity flag
  • $/: input record separator
  • $\: output record separator
  • $0: name of the running program
  • $*: command-line arguments
  • $$: process ID
  • $?: exit status of the most recently run child process
  • ARGV[n]: nth command-line argument
  • $DEBUG: debugging messages; enabled when -d is turned on
  • $stderr: standard error
  • $stdin: standard input
  • $stdout: standard output
  • $:: names of modules loaded by the script

Comment

Characters after # are treated as a comment. Alternatively, you can comment out a block with =begin and =end.

name = 'test_text' # COMMENT
=begin
COMMENT
=end

Evaluate String

A string enclosed in single quotes (') means its contents make up the string as is. A string enclosed in double quotes (") evaluates its contents.

language = 'Ruby'
puts "hello, #{language}"

Iterators and Blocks

Think of an Iterator as an object for cycling through data. In Ruby, arrays also have an Iterator. Use .each.

presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {|prez| puts prez}
## OR:
presidents.each do |prez|
    puts prez
end

{} vs do/end

{} applies to the result of the Iterator, while do/end applies to the Iterator.

Create Proc

doYouLike = Proc.new do |aGoodThing|
  puts 'I *really* like '+aGoodThing+'!'
end

List of methods

## Total methods.
Greeter.instance_methods
## Ignore parent methods.
Greeter.instance_methods(false)

Methods checker

irb(main):041:0> g.respond_to?("name")
=> false
irb(main):042:0> g.respond_to?("say_hi")
=> true
irb(main):043:0> g.respond_to?("to_s")
=> true

Setter & Getter

irb(main):044:0> class Greeter
irb(main):045:1>   attr_accessor :name
irb(main):046:1> end
irb(main):047:0> g = Greeter.new("Andy")
=> #<Greeter:0x3c9b0 @name="Andy">
irb(main):048:0> g.respond_to?("name")
=> true
irb(main):049:0> g.respond_to?("name=")
=> true
irb(main):051:0> g.name="Betty"
=> "Betty"
irb(main):052:0> g
=> #<Greeter:0x3c9b0 @name="Betty">
irb(main):053:0> g.name
=> "Betty"
irb(main):054:0> g.say_hi
Hi Betty!
=> nil

Frameworks

See also

Favorite site

Basic tutorial

Article

References


  1. Learn_to_Program_-_Ruby.zip ↩

  2. Joinc_Ruby_Tutorial1_-_Basic.pdf ↩

  3. Joinc_Ruby_Tutorial2_-_Class.pdf ↩

author Zer0 sitemap