CS 390 Software Engineering Lecture 6 Ruby References: Ruby in Twenty Minutes, available at https://www.ruby-lang.org/en/documentation/quickstart/.
Outline Clone Ruby example git repository $ git clone ssh://[user@]csserver.evansville.edu/usr/local/git/ruby-examples.git Install RVM (Ruby Version Manager). Use to install consist version of Ruby and Rails See rvm-installation file Ruby 2
Ruby Ruby is a scripting language, not unlike Python or PHP. Ruby can be run interactively using the irb (Interactive Ruby) interpreter. It evaluates expressions and prints out results. (puts is a command that outputs a string.) irb(main):001:0> "Hello World" => "Hello World" irb(main):002:0> puts "Hello World" Hello World irb(main):003:0> 3+2 => 5 irb(main):004:0> Math.sqrt(9) => 3.0 3
Functions Ruby functions are defined using reserved word def and end with reserved word end. irb(main):010:0> def hi irb(main):011:1> puts "Hello World!" irb(main):012:1> end => :hi Functions may be called with or without parentheses. E.g., using hi or hi(). 4
Parameters Ruby parameters are listed in parentheses. They are referenced using #{<param>} irb(main):015:0> def hi(name) irb(main):016:1> puts "Hello #{name}!" irb(main):017:1> end => :hi irb(main):018:0> hi("matz") Hello Matz! 5
Parameters Ruby parameters can have default values. Functions with parameters may be called without parentheses as long as it is obvious what is being done. irb(main):019:0> def hi(name = "World") irb(main):020:1> puts "Hello #{name.capitalize}!" irb(main):021:1> end => :hi irb(main):022:0> hi "chris" Hello Chris! irb(main):023:0> hi Hello World! 6
Classes Object types are defined using reserved word class and end with reserve word end. Data attribute names are prefixed with @ and are private. irb(main):024:0> class Greeter irb(main):025:1> irb(main):026:2> irb(main):027:2> irb(main):028:1> irb(main):029:2> irb(main):030:2> irb(main):031:1> irb(main):032:2> irb(main):033:2> irb(main):034:1> end => :say_bye def initialize(name = "World") end @name = name def say_hi end puts "Hi #{@name}!" def say_bye end puts "Bye #{@name}, come back soon." 7
Instantiation Ruby objects are instantiated using reserved method new that is a class method that calls the class' initialize method. irb(main):035:0> greeter = Greeter.new("Pat") => #<Greeter:0x16cac @name="pat"> irb(main):036:0> greeter.say_hi Hi Pat! irb(main):037:0> greeter.say_bye Bye Pat, come back soon. 8
Attribute accessors Attributes can be made accessible using attr_accessor. Ruby classes can be opened after definition and new methods and accessors can be added. irb(main):044:0> class Greeter irb(main):045:1> irb(main):046:1> end => Nil attr_accessor :name irb(main):047:0> greeter = Greeter.new("Andy") => #<Greeter:0x3c9b0 @name="andy"> irb(main):050:0> greeter.say_hi Hi Andy! irb(main):051:0> greeter.name="betty" => "Betty" irb(main):053:0> greeter.name => "Betty" irb(main):054:0> greeter.say_hi Hi Betty! 9
Ruby scripts Look at ri20min.rb. First line is #!/usr/bin/env ruby Comments start with # to the end of the line Start of "main program" is if FILE == $0 to the reserved word end. FILE is an environment variable set to the name of this file. $0 is the first command-line argument (i.e. argv[0]). If they are the same, then this file is being run as a program. Otherwise it is being used as a library and this code is ignored. Run script using: ruby ri20min.rb 10
Ruby objects A Ruby object has Smalltalk-like behavior and can be asked if it responds to a particular method name. if @names.respond_to?("each") E.g., this can be used to distinguish between single objects and collection objects. Collections have many methods such as each and join. 11
Arrays Arrays can be defined explicitly giving a list in [ ]. mg.names = ["Albert", "Brenda", "Charles", "Dave", "Engelbert"] 12