ruby reuben mallaby
TRANSCRIPT
![Page 1: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/1.jpg)
Ruby
Reuben Mallaby
![Page 2: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/2.jpg)
http://isic.he-arc.ch/isic-galerie-institut-equipe/reuben-mallaby
http://stackoverflow.com/users/27060/reuben-mallaby
http://www.totallysoft.ch
Ruby - RMA
![Page 3: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/3.jpg)
Ruby
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
![Page 4: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/4.jpg)
Ruby - Introduction
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Yukihiro "Matz" Matsumoto
Started February 24, 1993
1.0 - December 25, 1996
1.3 - 1999 -> EN
Ruby on Rails 2005
1.9.2 - 2010
![Page 5: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/5.jpg)
Ruby - Introduction
Redminehttp://projets-labinfo.he-arc.ch
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Login – firstname.lastnamePassword – changeme!
![Page 6: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/6.jpg)
Ruby - Introduction
Change Passwordhttp://projets-labinfo.he-arc.ch/my/password
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
View accounthttp://projets-labinfo.he-arc.ch/my/account
View projecthttp://projets-labinfo.he-arc.ch/projects/masradror
![Page 7: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/7.jpg)
Ruby - Installation
WindowsIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://projets-labinfo.he-arc.ch/attachments/download/764/rubyinstaller-1.9.2-p180.exe
iconv
![Page 8: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/8.jpg)
Ruby - Installation
LinuxIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://projets-labinfo.he-arc.ch/attachments/download/766/ruby-1.9.2-p180.tar.gz
sudo apt-get install mysql-server sudo apt-get install libmysqlclient15-devsudo apt-get install build-essential zlib1g-dev sudo apt-get install libssl-dev libreadline5-devsudo apt-get install git-core
![Page 9: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/9.jpg)
Ruby - Installation
Mac OSXIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://amerine.net/2010/02/24/rvm-rails3-ruby-1-9-2-setup.htmlhttp://rvm.beginrescueend.com/
![Page 10: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/10.jpg)
Ruby - Editor
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Windows
http://notepad-plus-plus.org/download
http://www.jetbrains.com/ruby/download/
![Page 11: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/11.jpg)
Ruby - Editor
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Linux
gedit
http://www.jetbrains.com/ruby/download/
kate
![Page 12: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/12.jpg)
Ruby - Editor
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Mac OSX
http://www.barebones.com/products/textwrangler/
http://www.jetbrains.com/ruby/download/
http://macromates.com/
![Page 13: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/13.jpg)
Ruby - Convention
local_variablemethod_namemethod_name(parameter , other)method_name parameter, other
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
$global_variable
class_variableinstance_variable
CONSTANTConstant
![Page 14: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/14.jpg)
Ruby - IRB
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
![Page 15: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/15.jpg)
Ruby – Hello World
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Console IRB
Text file
puts “Hello world”
number = gets.chomp
ruby my_file.rb
p “Hello world” name=gets.chompputs “Hello #{name}”
![Page 16: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/16.jpg)
Ruby – Hello World
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Strings
number = gets.chomp
p ‘Hello world’
p “Hello world”
p `Hello world`
p ‘#{var}’
p “#{var}”
p `#{var}`
var = “dir”
![Page 17: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/17.jpg)
Ruby - Numbers
FixNumIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
15+25656/72**825%4
-1.abs65.chr34.even?71.odd?24.gcd(32)24.lcm(32)3.next-5.pred
1.class
http://www.ruby-doc.org/core/classes/Integer.html
![Page 18: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/18.jpg)
Ruby - Numbers
FixNumIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
def fact(i) i <= 1? 1 : i * fact(i-1)end
http://www.ruby-doc.org/core/classes/Integer.html
fact(10).class
fact(100).class
![Page 19: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/19.jpg)
Ruby - Numbers
FloatIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
1.0-1.25abs51.25+98.65-92.2**5.2598.0%25.6398%25.63
15.63.ceil15.63.floor15.63.to_i(-1.0/0.0).infinite?
http://www.ruby-doc.org/core/classes/Float.html
1.0.class
![Page 20: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/20.jpg)
Ruby - Numbers
BigDecimalIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
big = BigDecimal.new(“123.45”)
big ** 12big **= 12big.frac
http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html
big.class
require ‘bigdecimal’
![Page 21: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/21.jpg)
Ruby - Structure
ifIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_iif count > 100 puts “Big”end
count = gets.chomp.to_iif count > 100 puts “Big”else puts “Small”end
![Page 22: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/22.jpg)
Ruby - Structure
ifIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_iif count > 100 puts “Big”elsif count > 50 puts “Medium”else puts “Small”end
count = gets.chomp.to_iputs “Big” if count > 100
![Page 23: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/23.jpg)
Ruby - Structure
unlessIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_iunless count > 100 puts “Small”end
count = gets.chomp.to_iunless count > 100 puts “Small”else puts “Big”end
count = gets.chomp.to_iputs “Small” unless count > 100
![Page 24: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/24.jpg)
Ruby - Structure
caseIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_itext = case count where 0..50 “Small” where 51..100 “Medium” else “Big”endputs text
![Page 25: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/25.jpg)
Ruby - Structure
forIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
for i in 1..10 puts iend
for i in 1..10 do puts i end
for item in items puts itemend
items.each do |item| puts itemend
![Page 26: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/26.jpg)
Ruby - Structure
timesIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
10.times { |i| puts i }
upto 1.upto(10) { |i| puts i }
downto 10.downto(1) { |i| puts i }
![Page 27: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/27.jpg)
Ruby - Structure
whileIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_iwhile count > 10 puts count count -= 10end
count = gets.chomp.to_ibegin puts count count -= 10end while count > 10
![Page 28: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/28.jpg)
Ruby - Structure
untilIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
count = gets.chomp.to_iuntil count > 100 puts count count += 10end
count = gets.chomp.to_ibegin puts count count += 10end until count > 100
![Page 29: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/29.jpg)
Ruby - Structure
ExercisesIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Find the sum of all the multiples of 3 or 5 below 1000.
Write two methods to convert between Celsius and Fahrenheit.
![Page 30: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/30.jpg)
Ruby - Array
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Array.html
my_array = Array.new
my_array = []
my_array = [1,2,3,4,5]
my_array = Array(1..5)
my_array = [”1”,”2”,”3”,”4”,”5”]
my_array = [1,”2”,3,4,”5”,Array.new]
my_array = %w[hello world]
![Page 31: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/31.jpg)
Ruby - Array
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Array.html
been_to = [“London”, “Paris”, “Bern”, “Geneva”, “Berlin”, “New York”]
to_go = [“Moscow”, “Tokyo”, “Perth”, “Sydney”, “Washington”, “San Francisco”]
my_places = been_to + to_go
to_go -= [“Tokyo”]
![Page 32: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/32.jpg)
Ruby - Array
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Array.html
been_to.sort
been_to.sort!
my_places.clear
my_places.empty?
my_places = been_to + to_gomy_places.sort!my_places.each {|place| puts place}
more = my_places + been_tomore.uniq!
my_places.reverse.each do |place| if been_to.include?(place) puts “I’ve been to #{place}” endend
![Page 33: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/33.jpg)
Ruby - Array
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Array.html
Exercises:
https://raveendran.wordpress.com/tag/ruby-exercise
![Page 34: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/34.jpg)
Ruby - Hash
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Hash.html
my_hash = Hash.new
my_hash = {}
my_hash = { “one” => 1, 1 => “one”, :one => 1}
my_hash[“one”]my_hash[1]my_hash[:one]my_hash[2]my_hash[2] = “Hello”
![Page 35: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/35.jpg)
Ruby - Hash
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
http://www.ruby-doc.org/core/classes/Hash.html
my_hash.each do |key, value| puts "#{key} is #{value}" end
![Page 36: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/36.jpg)
Ruby - Symbol
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
my_hash[:one]
“one”.object_id“one”.object_id“one”.object_id
:one.object_id:one.object_id:one.object_id
![Page 37: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/37.jpg)
Ruby - Class
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
class MyClass def say(word) puts word endend
MyClass.new.say(“Hello”)
a = MyClass.newa.say(“Hello”)a.class
![Page 38: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/38.jpg)
Ruby - Class
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
class MyClass def self.speak(word) puts word endend
MyClass.speak(“Hello”)
a = MyClass.newa.say(“Hello”)
a = MyClass.newa.speak(“Hello”)
![Page 39: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/39.jpg)
Ruby - Module
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
module MyModule def speak(word) puts word endend
class MyNewClass include MyModuleend
a = MyNewClassa.speak “Hi”
![Page 40: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/40.jpg)
Ruby - Module
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
module MyModule def speak(word) puts word endend
class AnotherClass extend MyModuleend
AnotherClass.speak “Hi”
![Page 41: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/41.jpg)
Ruby - Exceptions
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
begin 1/0 p 'I should never get executed' rescue p 'I am rescuing an exception and can do what I want!' end
begin 1/0 p 'I should never get executed' rescue p 'I am rescuing an exception!end
1/0
![Page 42: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/42.jpg)
Ruby - Exceptions
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
begin 1/0 p 'I should never get executed' rescue p 'I am rescuing an exception and can do what I want!' end
i = 0while i <= 10 begin p i if i == 0 1/0 end raise "random runtime exception" p 'I should never get executed' rescue ZeroDivisionError p 'I am rescuing only ZeroDivisionErrors!' i += 1 endend
![Page 43: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/43.jpg)
Ruby - Blocks
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
def three_times yield yield yieldendthree_times { puts "Hello" }
def fibonacci(max) i1, i2 = 1, 1 # parallel assignment while i1 <= max yield i1 i1, i2 = i2, i1+i2 endendfibonacci(1000) { |f| print f, " " }
![Page 44: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/44.jpg)
Ruby - Exercises
IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises
Find the sum of the digits in the number 100!
Write a method that outputs a right isosceles triangle of height and width n.
Write a method to check a given account number. An account number consists of 8 numbers and a check digit. The sum of the digits in account 12345678 is 36. 36 modulo 10 is 6, and the check digit is in the third position of the final account number, giving 126345678.
Write a method to check if a given number or string is a palindrome
![Page 45: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/45.jpg)
Ruby - Bibliography
http://www.ruby-doc.org
http://www.ruby-lang.org
https://secure.wikimedia.org/wikipedia/en/wiki/Yukihiro_Matsumoto
https://secure.wikimedia.org/wikipedia/en/wiki/Ruby_%28programming_language%29
http://www.cs.clemson.edu/~steve/Papers/GoodProblems.pdf
![Page 46: Ruby Reuben Mallaby](https://reader037.vdocuments.net/reader037/viewer/2022102906/56649c755503460f94929bd5/html5/thumbnails/46.jpg)
Ruby – End!