Ruby

4,412 views
4,295 views

Published on

Ruby basics, OOP, metaprogramming

Published in: Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,412
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
164
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Ruby

  1. 1. Ruby Vladimir Bystrov
  2. 2. Goals Learn Ruby basics, OOP and metaprogramming. Compare Ruby to other languages. Show Ruby pros and cons.
  3. 3. Contents Overview • Statements • OOP • Metaprogramming •
  4. 4. Ruby – Overview Object oriented • Dynamic • Untyped • Non commercial • Productive •
  5. 5. Ruby – Overview Ruby from other languages: • Java • Perl • PHP
  6. 6. Ruby – Overview Ruby libraries: Ruby/DBI • Rails • Ruby-IRC • google image search • Logging •
  7. 7. Ruby – Overview Advantages: OOP • Dynamic • Garbage collection • Metaprogramming • Exception handling • Libraries (http://rubyforge.org/) •
  8. 8. Ruby – Overview Disadvantages: • You can’t control memory allocation process, unable to define data primitives. • Unable to compile program • Can’t protect sources • Bad performance
  9. 9. Ruby – Statements First programm: puts “Hello World”
  10. 10. Ruby – Statements Variables in Ruby always designate references to objects, not the objects themselves. a = quot;abcdefgquot; => quot;abcdefgquot; b = a => quot;abcdefgquot; b => quot;abcdefgquot; a[3] = 'R' => quot;Rquot; b => quot;abcRefgquot;
  11. 11. Ruby – Statements Data types: number – 23 • string – “hello” • boolean – true | false • array – [“str”, 3] • hash array – {“a”=>4, “b”=>56} • range – 0..5 • symbol – :symb • proc and blocks – {|x| print x} •
  12. 12. Ruby – Statements Numbers: There are 2 types of numbers in Ruby: integers and floats (or decimals). There are 2 classes of integer numbers in order to distinguish between their size. So, numbers between -2^62 and 2^62-1 or -2^30 and 2^30-1 belong to the class Fixnum and are stored internally in binary format. Numbers outside those ranges belong to the Bignum class. A numeric literal with a decimal point and/or an exponent is turned into a Float object, corresponding to the native architecture’s double data type.
  13. 13. Ruby – Statements Numbers examples: 5 # integer number -12 # negative integer number 4.5 # float number 076 # octal number 0b010 # binary number 0x89 # hexadecimal number
  14. 14. Ruby – Statements Strings: str = ‘String’ str = “Another string” str = %q[String] str = %Q[Another string] str = <<EOF Long long long multiline text EOF
  15. 15. Ruby – Statements Boolean type: • true • false Any value evaluate to true, only nil evaluate to false.
  16. 16. Ruby – Statements Arrays: • dynamic • heterogeneous • iterators
  17. 17. Ruby – Statements
  18. 18. Ruby – Statements Arrays examples: a = [1, 3, 5, 7, 9] b = [3.14159, quot;piequot;, 99] s = %w[string array init] r = (1..10).to_a [1,3,5,7,9].each {|i| puts i} [1,3,5,7,9].reverse_each {|i| puts i}
  19. 19. Ruby – Statements Hash arrays: Hashes (sometimes known as associative arrays, maps, or dictionaries) are similar to arrays in that they are indexed collections of object references. However, while you index arrays with integers, you can index a hash with objects of any type: strings, regular expressions, and so on.
  20. 20. Ruby – Statements Hash array example: {”hello” => ”world”, 234 => “mega number!”, ”ruby” => ”rocks”}
  21. 21. Ruby – Statements Ranges: Ranges occur everywhere: January to December, 0 to 9, lines 50 through 67, and so on. Ruby supports ranges and allows us to use ranges in a variety of ways: • Sequences • Conditions • Intervals
  22. 22. Ruby – Statements Ranges as sequences: quot;aquot;..quot;zquot; quot;aquot;...quot;zquot; # equal to quot;aquot;..quot;yquot; 1..100 1...100 # equal to 1..99
  23. 23. Ruby – Statements Ranges as conditions: score = 70 result = case score when 0..40: quot;Failquot; when 41..60: quot;Passquot; when 61..70: quot;Pass with Meritquot; when 71..100: quot;Pass with Distinction“ else quot;Invalid Scorequot; end puts result
  24. 24. Ruby – Statements Ranges as intervals: if ((1..10) === 5) puts quot;5 lies in (1..10)quot; end if (('a'..'j') === 'c') puts quot;c lies in ('a'..'j')quot; end
  25. 25. Ruby – Statements Symbols: A symbol in Ruby is an instance of the class Symbol. A symbol is defined by prefixing a colon with an identifier. :name, :id, :user etc. are examples of symbols. Unlike strings, symbols of the same name are initialized and exist in memory only once during a session of ruby.
  26. 26. Ruby – Statements Symbols usage: • As keys in hashes • In Metaprogramming
  27. 27. Ruby – Statements Proc type: Proc objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.
  28. 28. Ruby – Statements Blocks: A block does not live on its own - it prepares the code for when it will actually become alive, and only when it is bound and converted to a Proc, it starts living.
  29. 29. Ruby – Statements Proc examples: putser = Proc.new {|x| puts x} putser = lambda {|x| puts x} putser.call(“Hello”)
  30. 30. Ruby – Statements Block usage example: def three_times yield yield yield end three_times {puts quot;Helloquot;}
  31. 31. Ruby – Statements Control statements: if/unless • case • while/until/loop/for • times/upto/downto •
  32. 32. Ruby – Statements Operator if example: if num > 0 print “num > 0” elsif num < 0 print “num < 0” else print “num = 0” end
  33. 33. Ruby – Statements Operator unless example: unless num == 0 print “num not equals 0” else print “num equals 0” end
  34. 34. Ruby – Statements Special if/unless usage example: print “a < 2” if a < 2 print “num is positive” unless num < 0
  35. 35. Ruby – Statements Operator case example: case val when 0: print “0” when 1..10: print “from 1 to 10” else print “more than 10” end
  36. 36. Ruby – Statements Operator while example: num = 0 while num < 10 print num num++ end s=2 s = s*s while s < 1000
  37. 37. Ruby – Statements Operator until example: num = 0 until num > 10 print num num++ end
  38. 38. Ruby – Statements Operator loop example: loop do print “Type something:” line = gets break if line =~ /q|Q/ puts line end
  39. 39. Ruby – Statements Operator for: for i in 0..9 print i, “ ” end #=> 0 1 2 3 4 5 6 7 8 9
  40. 40. Ruby – Statements Operators upto, downto and times: 1.upto(5) {|i| print i, “ ”} #=> 1 2 3 4 5 5.downto(1) {|i| print i, “ ”} #=> 5 4 3 2 1 10.times {|i| print i, “ ”} #=> 0 1 2 3 4 5 6 7 8 9
  41. 41. Ruby – Statements Exceptions handling: begin # ... rescue RuntimeError => e # handle concrete error else # handle unexpected error ensure # runs in any case end raise ArgumentError, “Incorrect argumentquot;, caller # caller – returns stack trace
  42. 42. Ruby – OOP Methods • Classes • Singletons • Inheritance • Modules • Encapsulation •
  43. 43. Ruby – OOP Methods: def method print “hello” end In Ruby you can override operators like methods. def +(val) @val + val end
  44. 44. Ruby – OOP Classes: class Test def initialize(val) @val = val end def out_value print @val end end
  45. 45. Ruby – OOP Instance variables: class Test @name = “Easy Jet” def name @name end def name=(val) @name = val end end
  46. 46. Ruby – OOP In Ruby you can create getters and setters using methods: • attr • attr_reader • attr_writer • attr_accessor
  47. 47. Ruby – OOP Method attr: class Test attr :name, true end
  48. 48. Ruby – OOP Method attr_reader: class Test attr_reader :name, :phone end
  49. 49. Ruby – OOP Method attr_writer: class Test attr_writer :name, :phone end
  50. 50. Ruby – OOP Method attr_accessor: class Test attr_accessor :name, :phone end
  51. 51. Ruby – OOP Class variables and methods: class Test @@variable = “something” Test.output print “class method” end end
  52. 52. Ruby – OOP Singleton: Singleton classes, not to be confused with the singleton design pattern. The name itself is confusing, leading people to create alternative names such as: object-specific classes, anonymous classes, and virtual classes. Anonymous classes is one of the better names.
  53. 53. Ruby – OOP Singleton example: class Singleton end s = Singleton.new class << s def s.handle print “singleton method” end end
  54. 54. Ruby – OOP Inheritance: class Base def method print “hello” end end class Child < Base attr_accessor :name end
  55. 55. Ruby – OOP Modules (Mixin): module TestModule def out print “mixin” end end class Test include TestModule end
  56. 56. Ruby – OOP Encapsulation: • private • protected • public class Test private def priv_method // do something end end
  57. 57. Ruby – Metaprogramming Duck typing: Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows: “If it walks like a duck and quacks like a duck, I would call it a duck”.
  58. 58. Ruby – Metaprogramming Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at runtime that would otherwise be done at compile time. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.
  59. 59. Ruby – Metaprogramming Dynamic code interpretation: • eval • class_eval, module_eval • instance_eval Foo = Class.new a = %q[def out() p quot;helloquot; end] Foo.class_eval(a) foo = Foo.new foo.out #=> “hello”
  60. 60. Ruby – Metaprogramming Get and set instance variables: class Tester @name end x = Tester.new p x.instance_variable_get(quot;@namequot;) #=> nil x.instance_variable_set(quot;@namequot;, quot;helloquot;) p x.instance_variable_get(quot;@namequot;) #=> quot;helloquot;
  61. 61. Ruby – Metaprogramming Dynamic method definition: class Tester def new_method(name, &block) self.class.send(:define_method, name, &block) end end x = Tester.new x.new_method(:out) {p quot;helloquot;} x.out #=> “hello”
  62. 62. Ruby – Metaprogramming Remove definitions: • undef_method • remove_method class Array remove_method :size end x = [1,2,3] p x.size #=> Error
  63. 63. Ruby – Metaprogramming Get list of defined objects, class methods: constants • ancestors • class_variables • included_modules • public_instance_methods • private_instance_methods • protected_instance_methods • superclass •
  64. 64. Ruby – Metaprogramming Get list of defined objects, instance methods: instance_variables • methods • public_methods • private_methods • protected_methods • singleton_methods •
  65. 65. Ruby – Metaprogramming Get class information: class • object_id • kind_of? • instance_of? • respond_to? •
  66. 66. Ruby – Metaprogramming View stack of calls: def func1 puts caller[0] end def func2 func1 end func2 #=> prints: somefile.rb:6:in “func2”
  67. 67. Ruby – Metaprogramming Iterate object space: ObjectSpace.each_object do |obj| p obj.class end You can provide parameter for each_object (class or module), to narrow output.
  68. 68. Ruby – Metaprogramming Handle missing methods calls: In ruby, when you call a method that doesn't actually exist on that object, the object always invokes the method_missing method instead. This is one of ruby's cool metaprogramming features: it lets you decide yourself how to handle what would have otherwise been an error.
  69. 69. Ruby – Metaprogramming Handle missing methods calls: class Tester def method_missing(method, *args) system(method.to_s, *args) end end t = Tester.new t.dir
  70. 70. Ruby – Metaprogramming Observe changes in objects or class definitions: •inherited •included •method_added class Test def Test.method_added(method) p quot;new method added [#{method}]quot; end end
  71. 71. Resources http://www.ruby-lang.org/en/
  72. 72. Questions

×