SlideShare a Scribd company logo
Presented by-
ADITYA TIWARI
Ruby - Diving Deep
Know what you know...
3 Pillars of ruby
1. Everything is an object
2. Every operation is a method call on some object.
3. Everything is metaprogramming.
1. Everything is an object(Almost)
• Even lowly integers and nil are true objects:
57.to_s
57.methods
57.heinz_varieties
nil.respond_to?(:to_s)
1 + 2 => 1.+(2) or 1.send(:+, 2)
my_array[4] => my_array.[](4)
or my_array.send(:[], 4)
my_array[3] = "foo" => my_array.[]=(3, ‘foo’)
if (x == 3) .... => if (x.==(3))
my_func(z) => self.my_func(z)
or self.send(:my_func, z)
2. Everything is a mETHOD CALL
Example: (Operator overloading)
1 + 2 3
‘a’ + ‘b’ ‘ab’
[1] + [2] [1, 2]
Numeric#+, String#+, Array#+
So, can I have my own version of + method?
Dynamically typed: objects have types; variables don’t
1. 2.class > Integer
2. That’s why variables always need to be initialized.
3. Everything is pass by reference (except primitive data
types like integer, float, symbol because they take very
little space and they are immutable)
MetaProgramming and reflection
1. Reflection lets us ask an object questions about itself
and have it modify itself.
2. Metaprogramming lets us define new code at runtime.
3. How can these make our code DRYer, more concise, or
easier to read? – (or are they just twenty-dollar words
to make me look smart?)
Example: An international bank account - Open
classes
acct.deposit(100) # deposit $100
acct.deposit(euros_to_dollars(20)) # deposit euro
acct.deposit(CurrencyConverter.new( :euros, 20))
acct.deposit(100) # deposit $100
acct.deposit(20.euros) # about $25
● No problem with open classes....
class Numeric
def euros ; self * 1.292 ; end
end
● But what about
acct.deposit(1.euro)
The power of method_missing
● But suppose we also want to support
acct.deposit(1000.yen)
acct.deposit(3000.rupees)
• Surely there is a DRY way to do this?
https://pastebin.com/agjb5qBF
1. # metaprogramming to the rescue!
2.
3. class Numeric
4. @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
5. def method_missing(method_id, *args, &block) # capture all args in case have to call super
6. singular_currency = method_id.to_s.gsub( /s$/, '')
7. if @@currencies.has_key?(singular_currency)
8. self * @@currencies[singular_currency]
9. else
10. super
11. end
12. end
13. end
Inheritanc( Diving deep)
super
allows us to call methods up the inheritance
hierarchy.
Some facts about super
1. Super automatically forwards the params to it’s parent’s
methods.
2. You can prevent this behaviour by explicitly passing the
desired params or no params.
Example super()
Modules
Important use of modules:
1. mix its methods into a class:
class A ; include MyModule ; end
What if method is defined in multiple places?
– A.foo will search A, then MyModule, then
method_missing in A, then A's ancestor
– sort is actually defined in module Enumerable, which
is mixed into Array by default.
Include vs extend
Include: Add module’s methods as instance methods.
Extend: Add module’s methods as class methods.
A Mix-in is a Contract
Example:
● Enumerable assumes target object responds to each
– ...provides all?, any?, collect, find, include?,
inject, map, partition, ....
• Enumerable also provides sort, which requires elements of
collection (things returned by each) to respond to <=>
• Comparable assumes that target object responds to
<=>(other_thing)
Module for namespacing
module Predator
class Tiger
end
End
Predator::Tiger
When Module? When Class?
• Modules reuse behaviors
– high-level behaviors that could conceptually apply to
many classes – Example: Enumerable, Comparable
– Mechanism: mixin (include Enumerable)
• Classes reuse implementation
– subclass reuses/overrides superclass methods
– Mechanism: inheritance (class A < B)
VS java
ArrayList aList;
Iterator it = aList.iterator();
while (it.hasNext()) {
Object element = it.getNext();
// do some stuff with element
}
• Goal of the code: do stuff with elements of aList
Block?
[1, 2, 3].each do |i|
i.to_s
end
OR
[1, 2, 3].each { |i| i.to_s }
Example
Turning iterators inside-out
• Java:
– You hand me each element of that collection in turn.
– I’ll do some stuff.
– Then I’ll ask you if there’s any more left.
• Ruby:
– Here is some code to apply to every element of the
collection.
– You manage the iteration or data structure traversal.
And give me result.
Blocks are Closures
• A closure is the set of all variable bindings you can “
see ” at a given point in time
– In Scheme, it’s called an environment
• Blocks are closures: they carry their environment around
with them
• Result: blocks can help reuse by separating what to do
from where & when to do it
Advance metaprogramming
Adding methods in the context of an object
1. Is it possible to call private method of an object from
outside the class?
2. Declaring the class methods is the classical example of
adding methods to the objects.
closure
Block carries it’s local environment with it. That’s called
closure.
Yield(advance)
1. Yield with arguments:
def calculation(a, b)
yield(a, b)
end
puts calculation(5, 6) { |a, b| a + b } # addition
puts calculation(5, 6) { |a, b| a - b } # subtraction
Yield is neither an object nor a method
def foo
puts yield
puts method(:foo)
puts method(:yield)
end
foo { "I expect to be heard." }
Is it given
Block_given?
def foo
yield
end
Foo # LocalJumpError
Another way to invoke block
def what_am_i(&block)
block.class
end
puts what_am_i {} #Proc
A block is just a Proc! That being said, what is a Proc?
Procedures, AKA, Procs
class Array
def iterate!(code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end
array_1 = [1, 2, 3, 4]
array_2 = [2, 3, 4, 5]
square = Proc.new do |n|
n ** 2
end
array_1.iterate!(square)
array_2.iterate!(square)
More example
def callbacks(procs)
procs[:starting].call
puts "Still going"
procs[:finishing].call
end
callbacks(:starting => Proc.new { puts "Starting" },
:finishing => Proc.new { puts "Finishing" })
Lambda the anonymous function
class Array
def iterate!(code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end
array = [1, 2, 3, 4]
array.iterate!(lambda { |n| n ** 2 })
puts array.inspect
Lambda is just like procs so what’s the difference?
1. unlike Procs, lambdas check the number of arguments
passed.
def args(code)
one, two = 1, 2
code.call(one, two)
end
args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
2. A Proc return will stop a method and return the value
provided, lambdas will return their value to the method and
let the method continue on, just like any other method.
def proc_return
Proc.new { return "Proc.new"}.call
return "proc_return method finished"
end
def lambda_return
lambda { return "lambda" }.call
return "lambda_return method finished"
end
puts proc_return
puts lambda_return
If lambda are methods so can we
store existing methods and pass them
just like Procs?
def iterate!(code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end
def square(n)
n ** 2
end
array = [1, 2, 3, 4]
array.iterate!(method(:square))
puts array.inspect

More Related Content

PDF
Advanced Python, Part 1
PDF
Python Puzzlers - 2016 Edition
PDF
Porting to Python 3
PDF
concurrency with GPars
ODP
GPars (Groovy Parallel Systems)
PPTX
Introduction to Python and TensorFlow
PDF
Swift for TensorFlow - CoreML Personalization
PDF
Python programming : Threads
Advanced Python, Part 1
Python Puzzlers - 2016 Edition
Porting to Python 3
concurrency with GPars
GPars (Groovy Parallel Systems)
Introduction to Python and TensorFlow
Swift for TensorFlow - CoreML Personalization
Python programming : Threads

What's hot (20)

PDF
Matlab and Python: Basic Operations
PDF
Python Puzzlers
PDF
Object Orientation vs Functional Programming in Python
KEY
jRuby: The best of both worlds
PPTX
Neuroevolution in Elixir
PPTX
Python 표준 라이브러리
PDF
Steady with ruby
PPT
Learning Java 1 – Introduction
KEY
EventMachine for RubyFuZa 2012
KEY
Clojure Intro
PDF
Functional Programming with Groovy
PPTX
Learn python in 20 minutes
PPTX
Metaprogramming in julia
PDF
tictactoe groovy
PDF
Privet Kotlin (Windy City DevFest)
PPTX
SQL Server Select Topics
PPTX
Groovy!
PDF
Learn 90% of Python in 90 Minutes
PDF
Clojure for Java developers - Stockholm
PPTX
Matlab and Python: Basic Operations
Python Puzzlers
Object Orientation vs Functional Programming in Python
jRuby: The best of both worlds
Neuroevolution in Elixir
Python 표준 라이브러리
Steady with ruby
Learning Java 1 – Introduction
EventMachine for RubyFuZa 2012
Clojure Intro
Functional Programming with Groovy
Learn python in 20 minutes
Metaprogramming in julia
tictactoe groovy
Privet Kotlin (Windy City DevFest)
SQL Server Select Topics
Groovy!
Learn 90% of Python in 90 Minutes
Clojure for Java developers - Stockholm
Ad

Similar to Ruby basics (20)

PDF
Everything is Permitted: Extending Built-ins
PDF
Spock: Test Well and Prosper
PDF
Ruby tricks2
KEY
Concurrent programming with Celluloid (MWRC 2012)
KEY
Desarrollando aplicaciones web en minutos
PDF
Ruby — An introduction
PDF
7li7w devcon5
PDF
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
PPTX
The Metasploit Way TO USE IT IN DA PPT.pptx
PPT
Java Tut1
PPT
Java Tutorial
PPT
Java tut1
PPT
Tutorial java
KEY
DjangoCon US 2011 - Monkeying around at New Relic
KEY
Djangocon11: Monkeying around at New Relic
PPT
Java_Tutorial_Introduction_to_Core_java.ppt
PPT
Java Tutorial
PPSX
Java Tutorial
PPT
Groovy unleashed
PPT
Javatut1
Everything is Permitted: Extending Built-ins
Spock: Test Well and Prosper
Ruby tricks2
Concurrent programming with Celluloid (MWRC 2012)
Desarrollando aplicaciones web en minutos
Ruby — An introduction
7li7w devcon5
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
The Metasploit Way TO USE IT IN DA PPT.pptx
Java Tut1
Java Tutorial
Java tut1
Tutorial java
DjangoCon US 2011 - Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
Java_Tutorial_Introduction_to_Core_java.ppt
Java Tutorial
Java Tutorial
Groovy unleashed
Javatut1
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
sap open course for s4hana steps from ECC to s4
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Ruby basics

  • 1. Presented by- ADITYA TIWARI Ruby - Diving Deep Know what you know...
  • 2. 3 Pillars of ruby 1. Everything is an object 2. Every operation is a method call on some object. 3. Everything is metaprogramming.
  • 3. 1. Everything is an object(Almost) • Even lowly integers and nil are true objects: 57.to_s 57.methods 57.heinz_varieties nil.respond_to?(:to_s)
  • 4. 1 + 2 => 1.+(2) or 1.send(:+, 2) my_array[4] => my_array.[](4) or my_array.send(:[], 4) my_array[3] = "foo" => my_array.[]=(3, ‘foo’) if (x == 3) .... => if (x.==(3)) my_func(z) => self.my_func(z) or self.send(:my_func, z) 2. Everything is a mETHOD CALL
  • 5. Example: (Operator overloading) 1 + 2 3 ‘a’ + ‘b’ ‘ab’ [1] + [2] [1, 2] Numeric#+, String#+, Array#+ So, can I have my own version of + method?
  • 6. Dynamically typed: objects have types; variables don’t 1. 2.class > Integer 2. That’s why variables always need to be initialized. 3. Everything is pass by reference (except primitive data types like integer, float, symbol because they take very little space and they are immutable)
  • 7. MetaProgramming and reflection 1. Reflection lets us ask an object questions about itself and have it modify itself. 2. Metaprogramming lets us define new code at runtime. 3. How can these make our code DRYer, more concise, or easier to read? – (or are they just twenty-dollar words to make me look smart?)
  • 8. Example: An international bank account - Open classes acct.deposit(100) # deposit $100 acct.deposit(euros_to_dollars(20)) # deposit euro acct.deposit(CurrencyConverter.new( :euros, 20))
  • 9. acct.deposit(100) # deposit $100 acct.deposit(20.euros) # about $25 ● No problem with open classes.... class Numeric def euros ; self * 1.292 ; end end ● But what about acct.deposit(1.euro)
  • 10. The power of method_missing ● But suppose we also want to support acct.deposit(1000.yen) acct.deposit(3000.rupees) • Surely there is a DRY way to do this? https://pastebin.com/agjb5qBF
  • 11. 1. # metaprogramming to the rescue! 2. 3. class Numeric 4. @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019} 5. def method_missing(method_id, *args, &block) # capture all args in case have to call super 6. singular_currency = method_id.to_s.gsub( /s$/, '') 7. if @@currencies.has_key?(singular_currency) 8. self * @@currencies[singular_currency] 9. else 10. super 11. end 12. end 13. end
  • 13. super
  • 14. allows us to call methods up the inheritance hierarchy.
  • 15. Some facts about super 1. Super automatically forwards the params to it’s parent’s methods. 2. You can prevent this behaviour by explicitly passing the desired params or no params. Example super()
  • 16. Modules Important use of modules: 1. mix its methods into a class: class A ; include MyModule ; end What if method is defined in multiple places?
  • 17. – A.foo will search A, then MyModule, then method_missing in A, then A's ancestor – sort is actually defined in module Enumerable, which is mixed into Array by default.
  • 18. Include vs extend Include: Add module’s methods as instance methods. Extend: Add module’s methods as class methods.
  • 19. A Mix-in is a Contract Example: ● Enumerable assumes target object responds to each – ...provides all?, any?, collect, find, include?, inject, map, partition, .... • Enumerable also provides sort, which requires elements of collection (things returned by each) to respond to <=> • Comparable assumes that target object responds to <=>(other_thing)
  • 20. Module for namespacing module Predator class Tiger end End Predator::Tiger
  • 21. When Module? When Class? • Modules reuse behaviors – high-level behaviors that could conceptually apply to many classes – Example: Enumerable, Comparable – Mechanism: mixin (include Enumerable) • Classes reuse implementation – subclass reuses/overrides superclass methods – Mechanism: inheritance (class A < B)
  • 22. VS java ArrayList aList; Iterator it = aList.iterator(); while (it.hasNext()) { Object element = it.getNext(); // do some stuff with element } • Goal of the code: do stuff with elements of aList
  • 23. Block? [1, 2, 3].each do |i| i.to_s end OR [1, 2, 3].each { |i| i.to_s }
  • 25. Turning iterators inside-out • Java: – You hand me each element of that collection in turn. – I’ll do some stuff. – Then I’ll ask you if there’s any more left. • Ruby: – Here is some code to apply to every element of the collection. – You manage the iteration or data structure traversal. And give me result.
  • 26. Blocks are Closures • A closure is the set of all variable bindings you can “ see ” at a given point in time – In Scheme, it’s called an environment • Blocks are closures: they carry their environment around with them • Result: blocks can help reuse by separating what to do from where & when to do it
  • 28. Adding methods in the context of an object 1. Is it possible to call private method of an object from outside the class? 2. Declaring the class methods is the classical example of adding methods to the objects.
  • 29. closure Block carries it’s local environment with it. That’s called closure.
  • 30. Yield(advance) 1. Yield with arguments: def calculation(a, b) yield(a, b) end puts calculation(5, 6) { |a, b| a + b } # addition puts calculation(5, 6) { |a, b| a - b } # subtraction
  • 31. Yield is neither an object nor a method def foo puts yield puts method(:foo) puts method(:yield) end foo { "I expect to be heard." }
  • 32. Is it given Block_given? def foo yield end Foo # LocalJumpError
  • 33. Another way to invoke block def what_am_i(&block) block.class end puts what_am_i {} #Proc A block is just a Proc! That being said, what is a Proc?
  • 34. Procedures, AKA, Procs class Array def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array_1 = [1, 2, 3, 4] array_2 = [2, 3, 4, 5] square = Proc.new do |n| n ** 2 end array_1.iterate!(square) array_2.iterate!(square)
  • 35. More example def callbacks(procs) procs[:starting].call puts "Still going" procs[:finishing].call end callbacks(:starting => Proc.new { puts "Starting" }, :finishing => Proc.new { puts "Finishing" })
  • 36. Lambda the anonymous function class Array def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array = [1, 2, 3, 4] array.iterate!(lambda { |n| n ** 2 }) puts array.inspect
  • 37. Lambda is just like procs so what’s the difference? 1. unlike Procs, lambdas check the number of arguments passed. def args(code) one, two = 1, 2 code.call(one, two) end args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"}) args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
  • 38. 2. A Proc return will stop a method and return the value provided, lambdas will return their value to the method and let the method continue on, just like any other method. def proc_return Proc.new { return "Proc.new"}.call return "proc_return method finished" end def lambda_return lambda { return "lambda" }.call return "lambda_return method finished" end puts proc_return puts lambda_return
  • 39. If lambda are methods so can we store existing methods and pass them just like Procs?
  • 40. def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end def square(n) n ** 2 end array = [1, 2, 3, 4] array.iterate!(method(:square)) puts array.inspect