TRAINING 1RUBYPavel Tsiukhtsiayeugithub.com/paveltyk
Ruby :: introduction• Ruby is a object oriented language• Truly OBJECT ORIENTED language !!!• Designed and developed by Yu...
Ruby :: introduction                • Yukihiro “Matz” Matsumoto                  • まつもとゆきひろ 松本行弘                • Born 14 ...
Ruby :: introduction• Ruby is a object oriented language• Truly OBJECT ORIENTED language !!!• Designed and developed by Yu...
Ruby :: variables and constants• Sigils denote scopes• Sigil - symbol attached to variable name• $ - global ($global_varia...
Ruby :: methods, naming convention• object.find_by_name - preferable snake case• object.dangerous! - Bang!!! method• objec...
Ruby :: truly object oriented• Everything is an object. Everything!• 1.class => Fixnum• “hello”.class => String• [1, „hell...
Ruby :: nil is an object• nil.to_s => “”• nil.to_i => 0• nil.nil? => true• nil.blank? => true (Rails extension)• In statem...
Ruby :: syntax• Almost everything can be written in different ways  • Easy things stay clean and easy  • Difficult things ...
Ruby :: syntax sugar• Did you ever do?  • puts “Hello, World!”• puts - is it a language construct?  • It‟s a method define...
Ruby :: classes• Ready to know the truth? class MyClass                     MyClass = Class.new do  def name              ...
Ruby :: open classes• Classes in Ruby are always open• Change them any time (avoid monkey patching core classes) nil.dance...
Ruby :: inheritance• Ruby has NO multiple inheritance• Ruby has single inheritance + MIXINS• By default all classes inheri...
Ruby :: inheritance, message dispatching • kate.dance                                      BasicObject • kate.send :dance ...
Ruby :: self & method missing• self = current object• Every method has 3 parts: object.do_the_thing(10)  • object - receiv...
Ruby :: modules• A namespace for constants (such as classes)• A place to define methods (can be later used by a class or  ...
Ruby :: MIXINS• Mixins are used widely in Ruby• Please behave like Enumerable: include Enumerableclass Band               ...
Ruby :: MIXINS• What happens when you include a module?• class Band;end• Band.ancestors => [Band, Object, Kernel, BasicObj...
Ruby :: MIXIN• Try to solve thismodule A                           Object def name; “I‟m A”; endendmodule B               ...
Ruby :: MIXINS & eigenclasses• You can extend individual object with module‟s methodsmodule Dancer def dance  “Swing” ende...
Ruby :: MIXINS & eigenclasses• You can define methods on individual objectsclass Catendjane = Cat.newdef jane.dance  “Craw...
Ruby :: Blocks• Blocks are heavily used in Ruby• Callbacks  • after_create lambda { |u| u.send_message }  • before_validat...
Ruby :: alternative syntax• Array  • [„cat‟, „dog‟]  • %w(cat dog) - parentheses may vary: %w{..} %w[..] %w!..!  • %W(cat ...
Ruby :: Q&A• Q&A?
Ruby :: what do I do next?• http://tryruby.org• http://koans.heroku.com
Upcoming SlideShare
Loading in …5
×

Ruby :: Training 1

827 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Range - Интервал
  • Template functions = Функции шаблоны
  • Ruby :: Training 1

    1. 1. TRAINING 1RUBYPavel Tsiukhtsiayeugithub.com/paveltyk
    2. 2. Ruby :: introduction• Ruby is a object oriented language• Truly OBJECT ORIENTED language !!!• Designed and developed by Yukihiro “Matz” Matsumoto
    3. 3. Ruby :: introduction • Yukihiro “Matz” Matsumoto • まつもとゆきひろ 松本行弘 • Born 14 Apr 1965 • As of 2012, Matsumoto is the Chief Architect of Ruby at Heroku • https://github.com/matz • https://twitter.com/yukihiro_matz
    4. 4. Ruby :: introduction• Ruby is a object oriented language• Truly OBJECT ORIENTED language !!!• Designed and developed by Yukihiro “Matz” Matsumoto• Ruby written for humans, not machines • Often … computer engineers … focus on the machines. But in fact we need to focus on humans… We are the masters. They are the slaves.• Rich yet Flexible Syntax • 3.downto(0) { |i| puts "Countdown: #{i}..."} • user = User.find_by_name(„Matz‟) and user.say_hi• Ruby makes developers HAPPY
    5. 5. Ruby :: variables and constants• Sigils denote scopes• Sigil - symbol attached to variable name• $ - global ($global_variable)• @ - instance variable (@instance_variable)• @@ - class variable (@@class_variable)• [A-Z] - constants • CONSTANT - all upper case • MyClass - titleized• [_a-z] - local variables (local_variable)• You never see variable declarations• But you always know what you are dealing with• Clean and Readable code
    6. 6. Ruby :: methods, naming convention• object.find_by_name - preferable snake case• object.dangerous! - Bang!!! method• object.boolean_predicate? - returns true or false def Name # do not do this “I‟m a method” end Name = “I‟m a constant” Name #=> “I‟m a constant” Name() #=> “I‟m a method”
    7. 7. Ruby :: truly object oriented• Everything is an object. Everything!• 1.class => Fixnum• “hello”.class => String• [1, „hello‟, nil].class => Array• nil.class => NilClass• true.class => TrueClass• false.class => FalseClass• (1..10).class => Range• /hello/.class => Regexp• Fixnum.class => Class• Class.class => Class %)) WHAT!!
    8. 8. Ruby :: nil is an object• nil.to_s => “”• nil.to_i => 0• nil.nil? => true• nil.blank? => true (Rails extension)• In statements (if, case, etc.) only nil and false evaluated as FALSE
    9. 9. Ruby :: syntax• Almost everything can be written in different ways • Easy things stay clean and easy • Difficult things maintain readability• Parentheses are optional • find_by_name Matz • find_by_name(Matz)• Blocks • { |a| #do something with a } • do |a| • #do something with a • end• 1..5 eq Range.new(1,5) and /^Hi/ eq Regexp.new(“^Hi”)
    10. 10. Ruby :: syntax sugar• Did you ever do? • puts “Hello, World!”• puts - is it a language construct? • It‟s a method defined in Kernel :)• public, private, protected - language constructs? • No! They are class methods!• +, -, *, ==, etc. are methods too :) • 1 + 2 equivalent to 1.+(2) equivalent to 1.send :+, 2• =, .., ..., !, not, &&, and, ||, or, !=, !~ What about these?• These are REAL operators
    11. 11. Ruby :: classes• Ready to know the truth? class MyClass MyClass = Class.new do def name def name “MyClass” “MyClass” end end end end• Classes are instances of a class Class• Class.ancestors => • [Class, Module, Object, Kernel, BasicObject]
    12. 12. Ruby :: open classes• Classes in Ruby are always open• Change them any time (avoid monkey patching core classes) nil.dance #=> NoMethodError class NilClass def dance “Spin” end end nil.dance #=> “Spin” • Rails adds many handy methods to core Ruby classes: • blank?, present?, presence, etc.
    13. 13. Ruby :: inheritance• Ruby has NO multiple inheritance• Ruby has single inheritance + MIXINS• By default all classes inherit from Object• Object inherit from BasicObject (ruby 1.9)• Object.instance_methods.size => 112• BasicObject.instance_methods.size => 7
    14. 14. Ruby :: inheritance, message dispatching • kate.dance BasicObject • kate.send :dance Object my_object.method_missing my_object.send :dance MyClass• What happens if method not found?• Exception raised: NoMethodError
    15. 15. Ruby :: self & method missing• self = current object• Every method has 3 parts: object.do_the_thing(10) • object - receiver • do_the_thing - message • 10 - arguments • receiver is self, if not explicit receiver given• What method_missing can be useful for • Person.find_by_name(„Matz‟) • Write DSL (XML generators) • Proxy pattern • Method delgation
    16. 16. Ruby :: modules• A namespace for constants (such as classes)• A place to define methods (can be later used by a class or an object)• Unlike a class, can‟t have instances & instance variables• Class (as a class name) inherit from class Module • Class (as a class name) has more functionality (#new for example)• A key element of MIXIN module Dance Dance = Module.new do def do_the_trick def do_the_trick “Back flip” “Back flip” end end end end
    17. 17. Ruby :: MIXINS• Mixins are used widely in Ruby• Please behave like Enumerable: include Enumerableclass Band find | detect include Enumerable find_all | select map | collect attr_accessor :members inject, reject all? any? one? none? def each(&block) min max members.each{ |member| block.call(member) } end AND MOREendmetallica = Band.newmetallica.members = [„James‟, „Lars‟, „Kirk‟, „Robert‟]metallica.find_all { |m| m.size > 4 }#=> [„James‟, „Robert‟]
    18. 18. Ruby :: MIXINS• What happens when you include a module?• class Band;end• Band.ancestors => [Band, Object, Kernel, BasicObject] • class Band • include Enumerable • end• Band.ancestors => • [Band, Enumerable, Object, Kernel, BasicObject]• Ruby creates an anonymous proxy class (“include” class) containing the module‟s methods: Enumerable• Ruby inserts this proxy class in the inheritance chain right above current class: Band < Enumerable < Object …
    19. 19. Ruby :: MIXIN• Try to solve thismodule A Object def name; “I‟m A”; endendmodule B Object A def name; “I‟m B”; endendclass MyClass Object B A include A Include Bend MyClassMyClass.new.name => “I‟m B”
    20. 20. Ruby :: MIXINS & eigenclasses• You can extend individual object with module‟s methodsmodule Dancer def dance “Swing” endendclass Catendjane = Cat.newjane.extend(Dancer)jane.dance #=> “Swing”bob = Cat.newbob.dance #=> ??? NoMethodError• Ruby created eigenclass, and added proxy class • (jane eigenclass) < (Dancer) < Cat
    21. 21. Ruby :: MIXINS & eigenclasses• You can define methods on individual objectsclass Catendjane = Cat.newdef jane.dance “Crawl”endjane.dance #=> “Crawl”bob = Cat.newbob.dance #=> NoMethodError• jane‟s eigenclass now has #dance method
    22. 22. Ruby :: Blocks• Blocks are heavily used in Ruby• Callbacks • after_create lambda { |u| u.send_message } • before_validate lambda { |u| u.clear_name }• Template functions • [1,2,3].map { |a| a * 2 } #=> [2,4,6] • [1,2,3].all? { |a| a > 2 } #=> false• Wrapping logic • File.open(„hello.txt‟, „w‟) { |f| f.puts „Hello‟ }• Lazy evaluation • Rails.cache.fetch(:jobs) { heavy_task }
    23. 23. Ruby :: alternative syntax• Array • [„cat‟, „dog‟] • %w(cat dog) - parentheses may vary: %w{..} %w[..] %w!..! • %W(cat #{var} dog)• Regexp • /^cat/ • %r(^cat) - parentheses may vary• String • „Hello, World‟ and “Hello, Word!” • %q(Hello, World!) - single quoted string • %Q(Hello, World!) - double quoted string (interpolation occurs) • %(Hello, World!) • <<HERE_DOC • Hello, World! • HERE_DOC
    24. 24. Ruby :: Q&A• Q&A?
    25. 25. Ruby :: what do I do next?• http://tryruby.org• http://koans.heroku.com

    ×