Ruby: a programming language
Rails: a Ruby framework used for web development
"Ruby on Rails"
Gems: Ruby add-ons (called libraries in other languages)
Ruby files use *.rb extension
Ruby is object-oriented and can also be used for functional programming
single line comments
hash aka sharp aka pound (#) is used to comment out a line
# single line comment
puts 'hi' # more comments
block comments aka multiline comments
=begin
comment line
comment line
=end
=begin
escape character `=being
=end
a statement can be ended with a semicolon (;) or end-line
if a line ends with an operator (+ - \, for example) then the statement will be continued onto the next line
file: hello_world.rb
puts 'Hello world'
Identifiers are names of variables, constants, methods
Identifiers are case sensitive
snake case: my_variable_name, my_variable_name_2
local variable names start with a lowercase letter or _
x + y #the same as x+y (addition)
x +y #the same as x(+y) where x is a method being invoked
declares code to run first in the program
does not have to be first in the file
BEGIN {
#code
}
declares code to run last when the program is closing
END {
#code
}
Verify if Ruby is installed on your system
ruby -v
To run a Ruby file
ruby my_file.rb
irb means Interactive Ruby
run an interactive Ruby shell in command prompt
irb #open the shell
"quit" or "exit" will close the shell
you can run single lines of Ruby from command prompt
ruby -e "puts 'Hello world'"
"-e" means evaluate
Installing Ruby on Windows
[Ruby's documentation]
winget search RubyInstallerTeam.Ruby #this displays the list of available packages
winget install {package-id}
#example
winget install RubyInstallerTeam.RubyWithDevKit.3.2
Then restart your command prompt so the updated environment variables are picked up.
You can now run *.rb files directly without saying "ruby" first.
making a Ruby file an executable
- make the *.rb file executable (chmod +x my_file.rb)
- include the shebang line in the file
#!/usr/bin/ruby
puts 'hello world'
now you can run the file without saying "ruby" first
./my_file.rb
"shebang" refers to the "sharp-bang" symbols (#!) that start the line
this tells unix what program to use to run the file
puts
- outputs text
- includes an implicit end line at the end
puts 'text'
puts "text"
- outputs text
- does not include an implicit end line
print 'text'
print "text"
output a variable
puts @my_instance_variable
puts "label: %d" % [@my_instance_variable]
local variables: defined in a method, scope is limited to the method
instance variables: scope is limited to an instance/object, use prefix @ to use the variable
class variables: shared across all instances of a class, use prefix @@ to use the variable
global variables: scope is the entire program, use prefix $ to use the variable
"data members" include characteristics and methods
class Person
#characteristics
@@lucky_number = 3
@@name = 'jane'
#methods
def say_hello()
print 'hello'
end
end
create an instance of a class (create an object)
class Person
#initialize is run on object creation
def initialize()
end
end
jane = Person.new
the "new" method is automatically available on all classes
method invocations only require parentheses when there are arguments
class Person
def initialize(name, lucky_number)
@name = name
@lucky_number = lucky_number
end
def display()
puts "name: %s, lucky: %d" % [@name, @lucky_number]
end
end
jane = Person.new('jane', 3)
jane.display
all classes inherit from the class "Class"
single quoted strings are literals
- '1\n2' does not include an end line
double quoted strings interpret escaped characters (backslash notation)
- "1\n2" prints 1, then end line, then 2
x = 5
y = "bob"
message = "x: %d, y: %s" % [x, y]
format specifiers:
- %s is for strings
- %d is for whole numbers
- %04d pads the number to four digits, filling in with leading 0s
- %f is for floating point numbers
- %0.2f means two decimal places (rounds)
- %x converts a decimal to hexadecimal
left or right justify strings to a number of characters
a = "apple"
b = a.ljust(10)
c = a.rjust(10)
build a multiline string
specify the custom terminator after "<<"
print <<MYEND
text text
text text
MYEND
number to string
n = 3
s = n.to_s