Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ruby Reuben Mallaby.

Similar presentations


Presentation on theme: "Ruby Reuben Mallaby."— Presentation transcript:

1 Ruby Reuben Mallaby

2 Ruby - RMA

3 Ruby Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises

4 Ruby - Introduction Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Yukihiro "Matz" Matsumoto Started February 24, December 25, > EN Ruby on Rails

5 Ruby - Introduction Redmine Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Login – firstname.lastname Password – changeme!

6 Ruby - Introduction Change Password Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises View account View project

7 Ruby - Installation WindowsIntroduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises arc.ch/attachments/download/764/rubyinstaller p180.exe iconv

8 Ruby - Installation LinuxIntroduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises arc.ch/attachments/download/766/ruby p180.tar.gz sudo apt-get install mysql-server sudo apt-get install libmysqlclient15-dev sudo apt-get install build-essential zlib1g-dev sudo apt-get install libssl-dev libreadline5-dev sudo apt-get install git-core

9 Ruby - Installation Mac OSXIntroduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises setup.html

10 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Windows

11 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Linux gedit kate

12 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Mac OSX

13 Ruby - Convention local_variable method_name method_name(parameter, other) method_name parameter, other Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises $global_variable class_variable instance_variable CONSTANT Constant

14 Ruby - IRB Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises

15 Ruby – Hello World Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Console IRB Text file puts “Hello world” number = gets.chomp ruby my_file.rb p “Hello world” name=gets.chomp puts “Hello #{name}”

16 Ruby – Hello World Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Strings number = gets.chomp p ‘Hello world’ p “Hello world” p `Hello world` p ‘#{var}’ p “#{var}” p `#{var}` var = “dir”

17 Ruby - Numbers FixNum Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises /7 2**8 25%4 -1.abs 65.chr 34.even? 71.odd? 24.gcd(32) 24.lcm(32) 3.next -5.pred 1.class

18 Ruby - Numbers FixNum Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises def fact(i) i <= 1? 1 : i * fact(i-1) end fact(10).class fact(100).class

19 Ruby - Numbers Float Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises abs ** % % ceil floor to_i (-1.0/0.0).infinite? 1.0.class

20 Ruby - Numbers BigDecimal Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises big = BigDecimal.new(“123.45”) big ** 12 big **= 12 big.frac big.class require ‘bigdecimal’

21 Ruby - Structure if Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i if count > 100 puts “Big” end count = gets.chomp.to_i if count > 100 puts “Big” else puts “Small” end

22 Ruby - Structure if Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i if count > 100 puts “Big” elsif count > 50 puts “Medium” else puts “Small” end count = gets.chomp.to_i puts “Big” if count > 100

23 Ruby - Structure unless Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i unless count > 100 puts “Small” end count = gets.chomp.to_i unless count > 100 puts “Small” else puts “Big” end count = gets.chomp.to_i puts “Small” unless count > 100

24 Ruby - Structure case Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i text = case count where “Small” where “Medium” else “Big” end puts text

25 Ruby - Structure for Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises for i in puts i end for i in do puts i end for item in items puts item end items.each do |item| puts item end

26 Ruby - Structure times Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 10.times { |i| puts i } upto 1.upto(10) { |i| puts i } downto 10.downto(1) { |i| puts i }

27 Ruby - Structure while Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i while count > 10 puts count count -= 10 end count = gets.chomp.to_i begin puts count count -= 10 end while count > 10

28 Ruby - Structure until Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises count = gets.chomp.to_i until count > 100 puts count count += 10 end count = gets.chomp.to_i begin puts count count += 10 end until count > 100

29 Ruby - Structure Exercises Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Find the sum of all the multiples of 3 or 5 below Write two methods to convert between Celsius and Fahrenheit.

30 Ruby - Array Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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]

31 Ruby - Array Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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”]

32 Ruby - Array Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises been_to.sort been_to.sort! my_places.clear my_places.empty? my_places = been_to + to_go my_places.sort! my_places.each {|place| puts place} more = my_places + been_to more.uniq! my_places.reverse.each do |place| if been_to.include?(place) puts “I’ve been to #{place}” end

33 Ruby - Array Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Exercises: https://raveendran.wordpress.com/tag/ruby-exercise

34 Ruby - Hash Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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”

35 Ruby - Hash Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises my_hash.each do |key, value| puts "#{key} is #{value}" end

36 Ruby - Symbol Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises my_hash[:one] “one”.object_id :one.object_id

37 Ruby - Class Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises class MyClass def say(word) puts word end MyClass.new.say(“Hello”) a = MyClass.new a.say(“Hello”) a.class

38 Ruby - Class Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises class MyClass def self.speak(word) puts word end MyClass.speak(“Hello”) a = MyClass.new a.say(“Hello”) a = MyClass.new a.speak(“Hello”)

39 Ruby - Module Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises module MyModule def speak(word) puts word end class MyNewClass include MyModule end a = MyNewClass a.speak “Hi”

40 Ruby - Module Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises module MyModule def speak(word) puts word end class AnotherClass extend MyModule end AnotherClass.speak “Hi”

41 Ruby - Exceptions Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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/01/0

42 Ruby - Exceptions Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises begin 1/0 p 'I should never get executed' rescue p 'I am rescuing an exception and can do what I want!' end i = 0 while 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 end

43 Ruby - Blocks Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises def three_times yield end three_times { puts "Hello" } def fibonacci(max) i1, i2 = 1, 1 # parallel assignment while i1 <= max yield i1 i1, i2 = i2, i1+i2 end fibonacci(1000) { |f| print f, " " }

44 Ruby - Exercises Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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 is modulo 10 is 6, and the check digit is in the third position of the final account number, giving Write a method to check if a given number or string is a palindrome

45 Ruby - Bibliography https://secure.wikimedia.org/wikipedia/en/wiki/Yukihiro_Matsum oto https://secure.wikimedia.org/wikipedia/en/wiki/Ruby_%28progra mming_language%29

46 Ruby – End!


Download ppt "Ruby Reuben Mallaby."

Similar presentations


Ads by Google