From 71ce6c0c292bac92e8147d23f92613620f782d77 Mon Sep 17 00:00:00 2001 From: Luc Street Date: Wed, 30 Sep 2020 13:30:04 -0700 Subject: [PATCH] Add sheet for ruby classes --- sheets/_ruby/classes | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sheets/_ruby/classes diff --git a/sheets/_ruby/classes b/sheets/_ruby/classes new file mode 100644 index 0000000..588ffb9 --- /dev/null +++ b/sheets/_ruby/classes @@ -0,0 +1,32 @@ +# Simple class with constructor +class Simple + def initialize(val) + // Set instance variable named `some_val` + @some_val = val + end +end + +# Instantiate a class +s = Simple.new('doot') + +# Class with inheritance +class SuperString < String + # Call the same method in the parent class with `super` + def length + # This is equivalent to 100 times String#length + super * 100 + end +end + +# You can re-open *any* existing class for modification +# This is called "monkey patching" and while it is very powerful, it +# should be used sparingly. +class Array + def implode + puts '*poof*' + self.clear + end +end + +# View a class's instance method names +Array.instance_methods