Ruby
When switching to Ruby from other languages, the transition should be easy and the programmer should be able to follow the same habits formed by other languages
Ruby can be very personal in the style it is created in, as there are multiple methods to accomplish the same task. For example, to get a subset of a list:
myArray[1,4] == myArray[1..4] == myArray.slice(1..4)
As per standard, lets make our first ruby script ‘hello world’. Ruby files have the extension of .rb, so helloworld.rb would look something like this:
#!/usr/bin/ruby
print “Hello ”
puts “World!”
Looking at the output, we should get a simple “Hello World!\n”. This illustrates the difference between print and put in respect to the automatic insertion of a newline.
Functions are defined in ruby very similarly to python, with a dash of perl:
def MyFunc (arg1, arg2)
do something
return something
end
Arguments are simply identified as a variable name.
Ruby has scoping rules based off of the character content of a variable name
Name Begins With Variable Scope
$ A global variable
Code
$global = 10
def testFunc
puts $global
end
testFunc()
Output
10
[a-z] or _ A local variable
Code
notAGlobal = 10
def testFunc
puts notAGlobal
end
testFunc()
Output
in `testFunc': undefined local variable or method `notAGlobal'
[A-Z] A constant
Code
MyConstant = 10
def testFunc
puts MyConstant
MyConstant = 30
puts MyConstant
end
testFunc()
Output
test.rb:7: dynamic constant assignment: MyConstant = 30
@ An instance variable (traditional class members)
Code
class MyClass
def MyVariable=(value)
@storedVar = value
end
def MyVariable
@storedVar
end
end
inst1 = Myclass.new
inst2 = Myclass.new
inst1.myVariable = 10
inst2.myVariable = 20
puts inst1.myVariable
Output
10
@@ A class variable (static across all instances / singleton)
Code
class MyClass
def MyVariable=(value)
@@storedVar = value
end
def MyVariable
@@storedVar
end
end
inst1 = Myclass.new
inst2 = Myclass.new
inst1.myVariable = 10
inst2.myVariable = 20
puts inst1.myVariable
Output
20
class MyClass
def initialize ()
end
def MyMethod
puts "Test"
end
end
MyClassInstance = Myclass.new
class TestClass < BaseClass
...
end
def AngleRad
@angleRad
end
def AngleRad=( value )
@AngleRad = value
end
def AngleDeg
@angleRad * 180 / Math::Pi
end
def AngleDeg=( value )
@AngleRad = value * Math::PI / 180
end
attr_reader :variable
attr_writer :variable
attr_accessor :variable
myHash = Hash.new()
myHash[‘a’] #=> nil
myHash = Hash.new(“Error”)
myHash[‘a’] #=> “Error”
myHash = { ‘a’ => 1, ‘b’ => 2 }
myHash[‘c’] = 3
#!/usr/bin/ruby
if ARGV[0] == nil
would be checking if the user supplied at least one.
for line in file
puts line
end
At first glance some of the nuance in this is missed. The for loop iterates over lines, removing the newline constant at the end of each. This is why we use puts instead of print, as puts will put the \n back onto the line when it prints it back out.
Dir.foreach(dir) { |file| puts file }
The {brackets} declare the scope of this iterator.
The |abs.val| signs declare the iterator name
In english, this line could read:
For each file in dir, print the file with a newline at the end.
/[a-z]+/ would match lowercase words
regex = /pattern/
regex = Regexp.new(pattern)
regex = /#{ARGV[0]}/
is a way to make the variable ARGV[0] the regex string, instead of the variable name as the expression.
if line =~ regex
regex.match(line)