SlideShare uma empresa Scribd logo
1 de 40
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic OperationsWai Nwe Tun
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worldsChristopher Spring
 
Neuroevolution in Elixir
Neuroevolution in ElixirNeuroevolution in Elixir
Neuroevolution in ElixirJeff Smith
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리용 최
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
EventMachine for RubyFuZa 2012
EventMachine for RubyFuZa   2012EventMachine for RubyFuZa   2012
EventMachine for RubyFuZa 2012Christopher Spring
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutesSidharth Nadhan
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia岳華 杜
 
tictactoe groovy
tictactoe groovytictactoe groovy
tictactoe groovyPaul King
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select TopicsJay Coskey
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 

Mais procurados (20)

Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
Neuroevolution in Elixir
Neuroevolution in ElixirNeuroevolution in Elixir
Neuroevolution in Elixir
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
 
Steady with ruby
Steady with rubySteady with ruby
Steady with ruby
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
EventMachine for RubyFuZa 2012
EventMachine for RubyFuZa   2012EventMachine for RubyFuZa   2012
EventMachine for RubyFuZa 2012
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
 
tictactoe groovy
tictactoe groovytictactoe groovy
tictactoe groovy
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
Groovy!
Groovy!Groovy!
Groovy!
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
 

Semelhante a Ruby basics

Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insAndrew Dupont
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and ProsperKen Kousen
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)tarcieri
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosEdgar Suarez
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introductionGonçalo Silva
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicGraham Dumpleton
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicNew Relic
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptGovind Samleti
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenGraham Royce
 

Semelhante a Ruby basics (20)

Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 

Último

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

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