learn ruby at ASIT

Click here to load reader

download learn ruby at ASIT

of 12

description

Ruby is fully object oriented; everything is an object.Inheritance is shown by ‘Java: class Student extends PersonRuby: class Student Modules are used to group classesclass Person Modules are like namespaces in html and xml.Access controls are similar to Java: public, protected and private. Each controls everything following it in a class.All variables are accessed by reference.

Transcript of learn ruby at ASIT

The Ruby Programming Language

The Ruby Programming Language

Object OrientationRuby is fully object oriented; everything is an object.Inheritance is shown by blue, :size => 6, :price => 24.95}To retrieve an element, use square brackets@size = order[:size]

Control Structures: Conditionalsif order[:color] == blueelsif order[:size] == 6elseend

Control Structures: Iterationfor, while and untilfor item in order doputs itemIterator eachsum = 0[1..10].each do |count| sum += countendputs sumcount is a parameter to the block and has no value outside of it.

Exceptionsbeginrescuerescueensureendrescue and ensure are the same as catch and finallyRuby also has throw and catch, similar to Java

ConventionsClass names begin with upper case letters.Method and variable names use lower case.For names with more than one word:Class names use camel (or bumpy) caseclass ActiveRecordMethod and variable names separate words with underscores.def show_person@little_girlIn Rails, table names are the plurals of the record namesSingle record is courseTable is called coursesBut the model class is called Course.