Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby things I done did learn about and such

Avatar for Skip Gibson Skip Gibson
October 11, 2019

Ruby things I done did learn about and such

I gave a short presentation in work on some interesting things I've learnt about in Ruby recently.

There was a whole lot more I could've spoken about on these topics but this was a nice overview.

Avatar for Skip Gibson

Skip Gibson

October 11, 2019

Other Decks in Education

Transcript

  1. What we’ll chat about • Ampersand operator and blocks of

    code • Enumerators and the Enumerable module • Ruby Object Model: singleton methods and the like
  2. Ampersand operator and blocks When & is prepended to the

    last parameter in a method definition and the method is called with a block then the block is saved in this variable.
  3. :block_given? You can use :block_given? to check if a block

    has been supplied to stop nasty errors.
  4. Passing Procs and Lambdas to methods If you have a

    Proc or a Lambda saved in a variable then you can use & to turn it into a code block and pass it to a method.
  5. The super magical wonder syntax Many people who are not

    familiar with Ruby are often mystified by this syntax.
  6. & == :to_proc Appending an ‘&’ to an object will

    call :to_proc on that object. So what does :to_proc do?
  7. You can define your own version of :to_proc and use

    ‘&’ until the cows come home. Whatever makes you happy really.
  8. But first, Enumerable! If you define a method called :each

    in your class and include the Enumerable module you get all sorts of methods for free. Enumerable uses :each in your class as the basis for its lovely methods.
  9. There are methods defined in the Enumerable module which when

    called without a block will return an enumerator. • :map • :each • :select • :cycle There are plenty of others.
  10. An enumerator will use the code of whichever method it’s

    based on as its :each method. 1st line — :each returns an enumerator that uses Array’s :each method code for its :each method, so a call to :map after works as normal. 2nd line — :map returns an enumerator that uses Array’s :map method code for its :each, so the call to :each is technically just a call to :map.
  11. Enumerator has some cool tricks. You can step through your

    values and :rewind back to the start. You can :peek at upcoming values without moving your internal iterator.
  12. Use :cycle for that :cycle will iterate to the end

    then start from the beginning again. No errors, no mess, no fuss. Good clean iterative fun for all the family.
  13. me Skip :hello Object BasicObject Calling :hello Right into the

    object’s class then up chain of superclasses. Because :hello is found in Skip that method is then run.
  14. me Skip Object BasicObject :method_missing Calling :shout We go right

    into the object’s class, there is no method called :shout so we move up the chain to Object. We reach BasicObject without having found it, so :method_missing is called on the original receiver (me) and we follow the process again. This means you can override :method_missing in your classes if you so wish.
  15. Singleton methods Ruby lets you add methods directly to an

    object. These methods end up in what’s known as the object’s Singleton Class.
  16. me Skip Object BasicObject Calling :shout singleton method #me :shout

    Right into the object’s singleton class then up into the object’s class, then up into the class’s superclass etc.
  17. Class methods Because a class is, like everything else, just

    an object you can define methods on them too. There are a couple of other ways you can define class methods though...
  18. Or you could do it this way But why repeat

    your class name? that’s going to be a pain if you end up changing it. Keep things DRY™.
  19. Seeing as you already have the class open you have

    access to self. This is the most common way you’ll see class methods defined.
  20. The tastiest way “class << self” will open the singleton

    class and you can define them in there.
  21. me Skip Object BasicObject Calling the Skip :hello class method

    #me #Skip :hello #Object #BasicObject Right from the Skip class into its singleton class, then up into the superclass’s singleton class, and so on. This is a simplified object model without modules, but if you understand this then you’re golden.
  22. In a nutshell “The superclass of the singleton class of

    an object is the object’s class. The superclass of the singleton class of a class is the singleton class of the class’s superclass”
  23. fin