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 http://isic.he-arc.ch/isic-galerie-institut-equipe/reuben-mallaby http://stackoverflow.com/users/27060/reuben-mallaby http://www.totallysoft.ch 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, 1993 1.0 - December 25, 1996 1.3 - 1999 -> EN Ruby on Rails 2005 1.9.2 - 2010

5 Ruby - Introduction Redmine http://projets-labinfo.he-arc.ch 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 http://projets-labinfo.he-arc.ch/my/password Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises View account http://projets-labinfo.he-arc.ch/my/account View project http://projets-labinfo.he-arc.ch/projects/masradror

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

8 Ruby - Installation LinuxIntroduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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-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 http://amerine.net/2010/02/24/rvm-rails3-ruby-1-9-2- setup.html http://rvm.beginrescueend.com/

10 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Windows http://notepad-plus-plus.org/download http://www.jetbrains.com/ruby/download/

11 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Linux gedit http://www.jetbrains.com/ruby/download/ kate

12 Ruby - Editor Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Mac OSX http://www.barebones.com/products/textwrangler/ http://www.jetbrains.com/ruby/download/ http://macromates.com/

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 15+256 56/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 http://www.ruby-doc.org/core/classes/Integer.html

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 http://www.ruby-doc.org/core/classes/Integer.html 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 1.0 -1.25abs 51.25+98.65-9 2.2**5.25 98.0%25.63 98%25.63 15.63.ceil 15.63.floor 15.63.to_i (-1.0/0.0).infinite? http://www.ruby-doc.org/core/classes/Float.html 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 http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html 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 0..50 “Small” where 51..100 “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 1..10 puts i end for i in 1..10 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 1000. 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 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]

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

32 Ruby - Array Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 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_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 http://www.ruby-doc.org/core/classes/Array.html 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 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”

35 Ruby - Hash Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises http://www.ruby-doc.org/core/classes/Hash.html 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 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

45 Ruby - Bibliography http://www.ruby-doc.org http://www.ruby-lang.org https://secure.wikimedia.org/wikipedia/en/wiki/Yukihiro_Matsum oto https://secure.wikimedia.org/wikipedia/en/wiki/Ruby_%28progra mming_language%29 http://www.cs.clemson.edu/~steve/Papers/GoodProblems.pdf

46 Ruby – End!


Download ppt "Ruby Reuben Mallaby."

Similar presentations


Ads by Google