1 / 46

Ruby

Ruby. Reuben Mallaby. Ruby - RMA. http://isic.he-arc.ch/isic-galerie-institut-equipe/reuben-mallaby. http://www.totallysoft.ch. http://stackoverflow.com/users/27060/reuben-mallaby. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol

royce
Télécharger la présentation

Ruby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby Reuben Mallaby

  2. Ruby - RMA http://isic.he-arc.ch/isic-galerie-institut-equipe/reuben-mallaby http://www.totallysoft.ch http://stackoverflow.com/users/27060/reuben-mallaby

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

  4. 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 StartedFebruary24, 1993 1.0 - December25, 1996 1.3 - 1999 -> EN Ruby on Rails 2005 1.9.2 - 2010 Ruby - Introduction

  5. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Redmine http://projets-labinfo.he-arc.ch Ruby - Introduction Login – firstname.lastname Password – changeme!

  6. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Change Password http://projets-labinfo.he-arc.ch/my/password Ruby - Introduction View account http://projets-labinfo.he-arc.ch/my/account View project http://projets-labinfo.he-arc.ch/projects/masradror

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

  8. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Linux Ruby - Installation http://projets-labinfo.he-arc.ch/attachments/download/766/ruby-1.9.2-p180.tar.gz sudoapt-getinstallmysql-server sudoapt-getinstalllibmysqlclient15-dev sudoapt-getinstallbuild-essential zlib1g-dev sudoapt-getinstalllibssl-devlibreadline5-dev sudoapt-getinstall git-core

  9. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Mac OSX Ruby - Installation http://amerine.net/2010/02/24/rvm-rails3-ruby-1-9-2-setup.html http://rvm.beginrescueend.com/

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

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

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

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

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

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

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

  17. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises FixNum 15+256 56/7 2**8 25%4 Ruby - Numbers -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. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises FixNum def fact(i) i <= 1? 1 : i * fact(i-1) end Ruby - Numbers fact(10).class fact(100).class http://www.ruby-doc.org/core/classes/Integer.html

  19. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Float 1.0 -1.25abs 51.25+98.65-9 2.2**5.25 98.0%25.63 98%25.63 Ruby - Numbers 15.63.ceil 15.63.floor 15.63.to_i (-1.0/0.0).infinite? 1.0.class http://www.ruby-doc.org/core/classes/Float.html

  20. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises require ‘bigdecimal’ BigDecimal Ruby - Numbers big = BigDecimal.new(“123.45”) big ** 12 big **= 12 big.frac big.class http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html

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

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

  23. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises unless count = gets.chomp.to_i unless count > 100 puts “Small” end Ruby - Structure 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. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises case count = gets.chomp.to_i text = case count where 0..50 “Small” where 51..100 “Medium” else “Big” end puts text Ruby - Structure

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

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

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

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

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

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

  31. 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”] Ruby - Array to_go = [“Moscow”, “Tokyo”, “Perth”, “Sydney”, “Washington”, “San Francisco”] my_places = been_to + to_go to_go -= [“Tokyo”] http://www.ruby-doc.org/core/classes/Array.html

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

  33. 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 Ruby - Array http://www.ruby-doc.org/core/classes/Array.html

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

  35. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises my_hash.eachdo |key, value| puts"#{key} is #{value}" end Ruby - Hash http://www.ruby-doc.org/core/classes/Hash.html

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

  37. 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 end Ruby - Class MyClass.new.say(“Hello”) a = MyClass.new a.say(“Hello”) a.class

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

  39. 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 end Ruby - Module class MyNewClass include MyModule end a = MyNewClass a.speak “Hi”

  40. 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 end Ruby - Module class AnotherClass extend MyModule end AnotherClass.speak “Hi”

  41. begin1/0p'I should never get executed'rescuep'I am rescuing an exception and can do what I want!'end Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises 1/0 Ruby - Exceptions begin 1/0 p 'I should never get executed' rescue p 'I am rescuing an exception! end

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

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

  44. Introduction Installation Editor Convention IRB Hello World Numbers Structure Array Hash Symbol Class Module Exceptions Blocks Exercises Write a method to check if a given number or string is a palindrome Ruby - 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.

  45. 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 Ruby - Bibliography

  46. Ruby – End!

More Related