Intro To Advanced Ruby

Programmer and writer.
Apr. 11, 2011
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
Intro To Advanced Ruby
1 of 48

More Related Content

What's hot

Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaEdureka!
scope of pythonscope of python
scope of pythonDwarak Besant
Python by RjPython by Rj
Python by RjShree M.L.Kakadiya MCA mahila college, Amreli
Flutter presentation.pptxFlutter presentation.pptx
Flutter presentation.pptxFalgunSorathiya
Chapter 4 number systemChapter 4 number system
Chapter 4 number systempraveenjigajinni
Python_in_DetailPython_in_Detail
Python_in_DetailMAHALAKSHMI P

More from Brian Hogan

Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoBrian Hogan
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleBrian Hogan
Create Development and Production Environments with VagrantCreate Development and Production Environments with Vagrant
Create Development and Production Environments with VagrantBrian Hogan
DockerDocker
DockerBrian Hogan
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open SourceBrian Hogan
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With ElmBrian Hogan

Recently uploaded

Mule Meetup Calgary- API Governance & Conformance.pdfMule Meetup Calgary- API Governance & Conformance.pdf
Mule Meetup Calgary- API Governance & Conformance.pdfNithaJoseph4
GIT AND GITHUB (1).pptxGIT AND GITHUB (1).pptx
GIT AND GITHUB (1).pptxGDSCCVRGUPoweredbyGo
Framing Few Shot Knowledge Graph Completion with Large Language ModelsFraming Few Shot Knowledge Graph Completion with Large Language Models
Framing Few Shot Knowledge Graph Completion with Large Language ModelsMODUL Technology GmbH
"Stateful app as an efficient way to build dispatching for riders and drivers..."Stateful app as an efficient way to build dispatching for riders and drivers...
"Stateful app as an efficient way to build dispatching for riders and drivers...Fwdays
How is AI changing journalism? Strategic considerations for publishers and ne...How is AI changing journalism? Strategic considerations for publishers and ne...
How is AI changing journalism? Strategic considerations for publishers and ne...Damian Radcliffe
How resolve Gem dependencies in your code?How resolve Gem dependencies in your code?
How resolve Gem dependencies in your code?Hiroshi SHIBATA

Recently uploaded(20)

Intro To Advanced Ruby

Editor's Notes

  1. Brian P. Hogan\n
  2. Ruby is a language that we can bend to our will\n
  3. It can be easy to read\n
  4. We can call methods as strings, etc\n
  5. \n
  6. \n
  7. \n
  8. \n
  9. Require loads in another Ruby file. It’s like import or include in other languages. \n
  10. We want to call the ‘today’ method on the Date class to get the current date by using the system time. This method isn’t actually loaded for us by default. We require the ‘date’ library which adds those features to the language.\n
  11. Unit testing in Ruby is built in. All we have to do is include the testing library.\n
  12. To use tests in Ruby, we only need to require the testing library and define our methdos with a special naming convention. Let’s use a test to drive the development of a function that will tell us if a person is allowed to drive. We don’t write the code. We have no idea how the code will work, but we do know that if a person is under 16 then they cannot drive. So in our test, we create a new Person, set the person’s age to 15, and then we call a method called “can_drive?” which we assert should not be true. If the assert statement returns true, the test passes.\n
  13. Our person class isn’t defined, so when we run this test, it fails. That’s ok. This is how we do test-driven development in Ruby. We writea simple test, then we write the class to make the test pass.\n
  14. So here’s the class. We use attr_accessor to create a getter and setter for age, then we write the can_drive? method to ensure that the age is greater than or equal to 16. Remember that in Ruby, the return value of a fuction is implicit - the last evaluated statement is the return value.\n
  15. Now our test passes and we can repeat this process each time we add a new feature.\n
  16. Now let’s talk about classes and objects. In many languages, a class is a blueprint for an object. In Ruby, that’s kind of true, but you should really think of classes AS objects. That means classes can also have methods.\n
  17. Instance methods are defined on the instance scope. To call them, we need to create an instance of the class to cal the method. This is the most common method. Class methods are methods we define on the class object itself. We can use the self. prefix to attach the method to the class object instead of the object instance. This is how we define the equivalent of a static method.\n
  18. But the idea of methods is something of a Java thing. Under the hood, Ruby is really a message passing language. Method calls are transated into messages, and are basically a conveience layer.\n
  19. We can use that to our advantage when writing more complex programs. send lets us call methods as a string. We can use respond_to? to ask if the method exists!\n
  20. \n
  21. \n
  22. We can use respond_to? to ask if the method exists!\n
  23. Lambdas let us execute code later. Sometimes we can't evaluate code right at runtime. Instead we need to context of other code instead.\n
  24. \n
  25. We use blocks all the time when we iterate\n
  26. We can also use blocks to wrap other code. Let's build a navigation bar\n
  27. We can also use blocks to wrap other code. Let's build a navigation bar\n
  28. \n
  29. The lambdas get evaluated when we fire the .call method. So here we declare a lambda and we take in one variable called “phrase”. We timestamp it. We pass the variables we want to the .call method and the time is displayed with a different value each time. We’re not actually running the code in the lambda until later.\n
  30. We can reopen classes\n
  31. In Ruby, we can reopen any class we want, even ones in the standard library. We can then make changes to the methods there. \n
  32. This is the surest way to shoot yourself, and your team, in the face. \n
  33. We still need to modify core classes at times. The Rails framework couldn’t exist without some patches like this, and modifying classes and objects is the way we write modular code.\n
  34. Modules let us extend objects without inheritance.\n
  35. A ninja is a person. Just because a person is now a ninja does not mean they are not a person anymore.\n
  36. We use include to add the module's methods to the instance. In this case, every Person is now a Ninja. \n
  37. We use the extend keyword to add the module's methods as class methods. Remember, classes are objects too. When we extend, we add the methods to the object.\n
  38. \n
  39. An instance is also an object. So instead of adding the Ninja module to the Person class, we can add it to just specific instances using extend, which is just a method that adds methods to the object.\n
  40. Now each instance is independant and we can add our modules to each one without affecting the others.\n
  41. This callback lets us hook into the class that includes the module. So getting back to our “blank” callback, let’s say we wanted all of our objects in our application to be able to have our “blank” method on it. Now we have a completely self-contained plugin that does not open any classes. This is how we cleanly modify Ruby code.\n
  42. One really nice feature of self.included is that we can call any class methods of the class that includes the module. So we can make behaviors. This lets us separate our concerns.\n
  43. Modules are inserted into the object's inheritence chain. When\nwe call a method, Ruby looks first in the object. If the method\nisn't found there, it looks at the parent object, and then it looks\nat the included modules. \n\n
  44. class_eval lets us declare methods on the class. We can use it inside of self.included to safely override existing methods.\n
  45. But these weird concepts let us organize our code in ways that don’t paint us into a corner later. We can make plugins that, just by adding a file to our app, we seamlessly prevent records from being deleted and instead just mark them as updated. \n
  46. We’ll use all of these concepts to drive the development of an extension that lets us declare callbacks that should be run before we save an object. Assume the save method saves the data to some data store somewhere.\n
  47. \n
  48. \n