Revision RAD – Ruby Part
A/Prof Andy Song andy.song@rmit.edu.au
Ruby
• BEGIN and END statements
• Ruby is purely OO
• Local variables _var / instance variable @var class variable @@var / global variable $var
• Instance variables are always private
• Constants, classes and methods are considered constants in Ruby
• if&unless
• Onlynilandfalsearefalse.‘’,0,0.0etcarealltrue
• case … when …
• while & until
Dr. Andy Song RMIT University 2021
2
Ruby
• forloopinRuby
• Loop control, e.g. break, next, redo, retry …
• Operators (++ –??)
• Assignmentoperators a=b=1+2 a,b=b,a
• Logical operators &&, ||, and, or,
• variable = a || puts “oops” mark ||= 0
• Override operators
• defined?
• :: operator
Dr. Andy Song RMIT University 2021
3
Ruby
• Array creation, element access [2, 5] [2..-3]
• push,pop,shift,unshift,compact,uniq,delete
• Iterate, select, reject using a block
• Public array methods &, |, -, +, *
• Hash creation and element access
• keys,values,delete,clear,empty?,fetch,each_key,each_value
• Selfdefinedmethods,parameters
• Splats my_call(a, *b) my_call(a, **b)
• my_call(a, b = 10)
Dr. Andy Song RMIT University 2021
4
Ruby
• Exceptions begin … rescue … else … ensure … end
• Retry, raise
• throw & catch in Ruby
• BLOCKS,scopeofblocks
• Block as parameter, yield, block using parameters
• &block, .call()
• Lambda and arrow notaion -> para { . . . }
• Mixins
• require & include
• Regular Expression
Dr. Andy Song RMIT University 2021
5