Ruby Reuben Mallaby

Preview:

Citation preview

Ruby

Reuben Mallaby

http://isic.he-arc.ch/isic-galerie-institut-equipe/reuben-mallaby

http://stackoverflow.com/users/27060/reuben-mallaby

http://www.totallysoft.ch

Ruby - RMA

Ruby

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

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

Ruby - Introduction

Redminehttp://projets-labinfo.he-arc.ch

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

Login – firstname.lastnamePassword – changeme!

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

Ruby - Installation

WindowsIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

http://projets-labinfo.he-arc.ch/attachments/download/764/rubyinstaller-1.9.2-p180.exe

iconv

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

Ruby - Installation

Mac OSXIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

http://amerine.net/2010/02/24/rvm-rails3-ruby-1-9-2-setup.htmlhttp://rvm.beginrescueend.com/

Ruby - Editor

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

Windows

http://notepad-plus-plus.org/download

http://www.jetbrains.com/ruby/download/

Ruby - Editor

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

Linux

gedit

http://www.jetbrains.com/ruby/download/

kate

Ruby - Editor

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

Mac OSX

http://www.barebones.com/products/textwrangler/

http://www.jetbrains.com/ruby/download/

http://macromates.com/

Ruby - Convention

local_variablemethod_namemethod_name(parameter , other)method_name parameter, other

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

$global_variable

class_variableinstance_variable

CONSTANTConstant

Ruby - IRB

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

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}”

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”

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

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

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

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’

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

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

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

Ruby - Structure

caseIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

count = gets.chomp.to_itext = case count where 0..50 “Small” where 51..100 “Medium” else “Big”endputs text

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

Ruby - Structure

timesIntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

10.times { |i| puts i }

upto 1.upto(10) { |i| puts i }

downto 10.downto(1) { |i| puts i }

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

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

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.

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]

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”]

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

Ruby - Array

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

http://www.ruby-doc.org/core/classes/Array.html

Exercises:

https://raveendran.wordpress.com/tag/ruby-exercise

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”

Ruby - Hash

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

http://www.ruby-doc.org/core/classes/Hash.html

my_hash.each do |key, value| puts "#{key} is #{value}" end

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

Ruby - Class

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

class MyClass def say(word) puts word endend

MyClass.new.say(“Hello”)

a = MyClass.newa.say(“Hello”)a.class

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”)

Ruby - Module

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

module MyModule def speak(word) puts word endend

class MyNewClass include MyModuleend

a = MyNewClassa.speak “Hi”

Ruby - Module

IntroductionInstallationEditorConventionIRBHello WorldNumbersStructureArrayHashSymbolClassModuleExceptionsBlocksExercises

module MyModule def speak(word) puts word endend

class AnotherClass extend MyModuleend

AnotherClass.speak “Hi”

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

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

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, " " }

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

Ruby – End!