Download presentation
Presentation is loading. Please wait.
Published byVeronica McDaniel Modified over 9 years ago
1
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz
2
Agenda Ruby –general information Syntax and semantics
3
Ruby interpreted, dynamically typed programming language created in 1995 by Yukihiro Matsumoto became popular thanks to Ruby on Rails framework
4
Comments Comments start with # „broken” lines have to end with \ :puts\ "brokenline" Commands don’t have to end with semicolon (or any other character)
5
Numbers No limits on number size _ may be used to separate groups of three digits from each other to increase readability: 12_345_678_901 Real numbers – „traditional” or expotential format: 3.141531415.0e-4
6
Strings Strings – in " or ' puts "string in quotes" puts 'string in apostrophes' Or using "%q syntax" puts %q!string! puts %Q{string} puts %
7
Strings Multiline strings use <<TOKEN syntax print <<HERE I am a multiline string HERE
8
Variables A variable is defined by value assignment text= "I am a string" Variable names start with _ or a..z (but there are exceptions), may contain small and capital letters, digits and _. Multi-word variable names use CamelCase: VariableNameInCamelCase
9
Expression interpolation Interpolation of variables in quoted strings: "My name is #{name}" Any expression may be put in #{…}
10
Symbols Symbols are an equivalent for enumeration – after a fashion. Name of a symbol with colon in front: :ISP_address
11
Constants (sort of) Constants names are written in caps: PI = 3.141_592_653 Their values may be redefined during program execution (but we'll get a warning ;) )
12
Arrays Arrays are used and created like in other languages: array= [1,2,3,"I am", "an array"] puts array[0] array[0]=4 Negative array index – starting at the end of the table, counting backwards: puts array[-1]
13
Arrays Processing multiple array elements – provide two values for index, first is the starting point, second– number of elements to be processed: array[3,2]="aaa"
14
Maps Tables indexed using numbers, and/or strings or symbols: Map=/ {"Key"=>40,"Another"=>"value"} puts AsocArray["Key"]
15
Ranges Range is defined using.. and … operators range= 1…4 #1,2,3 range= 1..4 #1,2,3,4 range= "a".."z" Few useful methods: to_a – convert range to an array min – first element max – last element
16
If if(cnd1) … elsif(cnd2) … else … end … if (cnd)
17
Unless unless(cnd) … else … end … unless (cnd)
18
Case case expression when v1 … when v2 … else … end
19
While and Until loops while(cnd) … end until(cnd) … end
20
Iterators upto and downto – for equivalent, step 1 or -1: startingValue.upto(endValue) do |variable| … end 0.upto(array.length-1) do |loop_index| … end
21
Iterators step – for with step different than 1 startValue.step(endValue, step) do \ |variable| … end 4.step(12,2) do |i| … end
22
Iterators times – loop executed a specific number of times: howMany.times do … end 5.times do … end
23
Iterators each–foreach once again collection.each do |variable| … end
24
Loops and iterators Keywords that may be used in loops and iterators: break – ends the loop redo – redo the current iteration next – move on to next iteration retry – re-start the loop
25
Methods method definition: def name(paramsList) … end def join (FirstWord, SecondWord) return FirstWord+" "+SecondWord end
26
Methods Variable number of parameters –last defined argument preceded with * def join(FirstWord, *others) return FirstWord+" "+others.join(" ") end Last parameter is treated like a table! If a table is to be passed as a parameter, its name should be preceded with *
27
Methods Returning multiple values –separate values with commas, method returns an array: def RetArray return 1,2,3 end
28
Blocks Section of code that may be passed to a method like a parameter Blocks are enclosed in curly brackets or between do and end keywords Blocks are invoked with the yield keyword
29
Blocks def greeting() yield end greeting {puts"Hello"}
30
Blocks Blocks may be parametrized def greeting() yield "Hello" end greeting {|param| puts param}
31
BEGIN and END blocks Code put in block marked as BEGIN is executed automatically after a program is loaded Code put in block marked as END is executed automatically after a program finishes execution
32
Classes Simplest class definition: class Name end Class names ALWAYS start with a capital letter
33
Classes initialize method–constructor class Name def initialize(parameters) … end
34
Classes Instance and class attributes: @name – instance attributes @@name – class attributes Attributes are usually initialized by the initialize method. Like variables –they are created by value assignment!
35
Classes Object creation – using the new method, which invokes initialize() instance= Name.new(parameters)
36
Classes Getters/setters class Animal def color() #getter @color end def color=(color) #setter @color=color end
37
Classes Auto-generated getters and setters: class Animal attr_reader:color attr_writer:color end or class Animal attr_accessor:color end
38
Classes Inheritance – operator < classDog < Animal … end If necessary (usually), superclass constructor is invoked with super(parameters)
39
Classes Access levels: public protected private Modifiers are used somewhere in the class body, everything declared after modifier has the new access level
40
Classes class methods are defined by preceding the method name with class name: class Name def Name.method(parameters) … end
41
Modules May be used to group classes and "independent" methods defined using the module keyword: module ModuleName Imported using include 'module.extension' require 'module' (file extension has to be.rb)
42
Modules Can't create module instances, so "independent" methods are usually class(or rather module) methods Classes defined within module (like internal classes) are accessed with ModuleName::ClassName
43
Modules Non-class independent methods may be imported into a class with include ModuleName methods from many modules may be imported into a single class all will be treated as if they were defined within the class
44
Recommended Reading
45
Programming Ruby 1.9 & 2.0 (4th edition): The Pragmatic Programmers' Guide, Dave Thomas, Chad Fowler, Andy Hunt Metaprogramming Ruby 2: Program Like the Ruby Pros, Paolo Perrotta Eloquent Ruby, Russ Olsen, Addison-Wesley 2011
46
Thank you for your attention
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.