SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
The Dark Side of Ruby

http://www.flickr.com/photos/sashapo/2722924752/sizes/l/
The Dark Side of Ruby

@gautamrege!
@joshsoftware

http://www.flickr.com/photos/sashapo/2722924752/sizes/l/

since 2007
What’s the talk about?
•

Nothing scary

•

Weirdness and Gotcha’s

Ah-ha! Moments
Slides are Tagged

Beginner

Expert
(In)Famous Infinity
http://www.flickr.com/photos/emdot/482622478/sizes/l/
(In)famous Infinity
$ irb> 1/0
=> ZeroDivisionError: divided by 0
$ irb> 1.0/0
=> Infinity
$ irb> Infinity
=> NameError: uninitialized constant
Infinity
Base Jumping

http://www.flickr.com/photos/shahdi/8035647153/sizes/l/
Base Conversions
$ irb> 12345.to_s(8)
=> "30071"
# => Octal
$ irb> 12345.to_s(36)
=> "9ix"
# That is an actual number
$ irb> 1234.to_s(64)
=> ArgumentError: invalid radix 64
The Star - *
Splat Expander
Job = Struct.new(:name, :occupation)
tom = Job.new("Tom", "Developer")
name, occupation = *tom
=> ["Tom", "Developer"]
=> name
# "Tom"
=> occupation
# "Developer"
Splat Expander
Job = Struct.new(:name, :occupation)
tom = Job.new(occupation: "Developer",
name: "Tom")
name, occupation = *tom
=> name # {:occupation=>"Developer", :name=>
"Tom"}
=> occupation
# nil
Hashes and Arrays
a=[1,2,3,4,5,6]!
h=Hash[*a]
=> {1=>2, 3=>4, 5=>6}
[1,2,3] * 3!
=> [1,2,3,1,2,3,1,2,3]
[1,2,3] * "%"!
=> "1%2%3"
Calling out to Stabby
blk = ->(f, *m, sl, l) do
puts sl
end
blk.call(1, 2, 3, 4, 5, 6)
=> 5
blk.(1, 2, 3, 4, 5, 6)
=> 5
call is implied for a stabby
proc or a Proc
The Case Statement
def multiple_of(factor)!
Proc.new {|p| p.modulo(factor).zero?}!
end!
!

number = 9!
case number!
when multiple_of(3)!
puts "Multiple of 3"!
when multiple_of(7)!
puts "Multiple of 7"!
end
Behind every case is a ===
number = 9!
case number !
when multiple_of(3)
Proc.new {|p| p.modulo(3).zero?} === 9

Proc#=== is an alias to Proc#call.
Proc.new { |p| !
p.modulo(3).zero?!
}.call(9)
Override the === method
to customise case
evaluation.
==, ===, eql?, equal?

http://www.flickr.com/photos/gak/2418146934/sizes/o/
==, ===, eql?, equal?
irb> 1 == 1.0
=> true # generic equality
irb> 1 === 1.0
=> true # case equality
irb> 1.eql? 1.0
=> false # equality by value
irb> 1.equal? 1.0
=> false # object identity
irb> 'a'.equal? 'a'
=> false # gotcha!
Proc#curry
http://www.flickr.com/photos/miscdebris/6748016253/sizes/o/
3 Pulls for the Jackpot
jackpot = lambda { |x, y, z|
(x == y) == (x == z)
}
!

# 3 pulls
pull = jackpot.curry[rand(5)]
2.times { pull = pull.curry[rand(5)] }
!

p pull ? "Jackpot" : "Sucker!"
The curry recipe
pull = jackpot.curry[rand(5)]
=> #<Proc:0x007f9eec0990b0 (lambda)>
•

Return lambda till all parameters are passed.

•

Evaluate the block if all parameters are passed.

2.times { pull = pull.curry[rand(5)] }
=> true # or false
So! So you think you can tell…

Protected from Private
Private methods
class Base
private
def foo
puts "inside foo"
end
Private Methods are
end
inherited!
class Child < Base
def bar
foo
end
end
The elusive include
class Base!
include Mongoid::Document!
end

Private method!
Instance method !
Defined the class Module
Protected methods
•

Work with objects not classes.

•

Invoke a protected method on
another object in the same lineage

What the …
class Autobot
def initialize(nick); @nick = nick; end
!

protected
attr_accessor :nick
end
!

prime = Autobot.new("Optimus Prime")
p prime.nick

protected method `nick' called for
#<Autobot:0x007f92ba082330 @nick="Optimus
Prime"> (NoMethodError)
class Autobot
def fights(target)
p "I am #{self.nick}"
p "Kicking #{target.nick}'s ass"
end
protected
attr_accessor :nick
end
!

prime = Autobot.new("Optimus Prime")
megatron = Autobot.new('Megatron')
!

prime.fights megatron
"I am Optimus Prime"
"Kicking Megatron's ass"
Keywords in Ruby?
Keywords - hmm…
class Serious
def true
false
end
def false
true
end
end
die = Serious.new
p "seriously!" if die.false
Cherry pick from Modules
module Megatron!
def power!
p "Megatron's super strength"!
end!
!

def evil!
p 'Evil genius'!
end!
end
Cherry pick from Modules
class Hanuman!
include Megatron!
end
Hanuman.new.power!
# => "Megatron's super strength"!
Hanuman.new.evil !
# => "Evil genius" # Oh no!
Cherry pick from Modules
class Hanuman!
def power!
Megatron.instance_method(:power).!
bind(self).call!
end!
end
Hanuman.new.power!
# => "Megatron's super strength"!
Hanuman.new.evil !
# => undefined method `evil’...>
That’s all Folks!
@gautamrege
@joshsoftware

Weitere ähnliche Inhalte

Andere mochten auch

Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
Srihari Sriraman
 

Andere mochten auch (17)

Extreme sports
Extreme sportsExtreme sports
Extreme sports
 
Base jumping
Base jumpingBase jumping
Base jumping
 
Base jumping
Base jumpingBase jumping
Base jumping
 
Bungee jumping
Bungee jumpingBungee jumping
Bungee jumping
 
Extreme sports + relative clauses
Extreme sports + relative clausesExtreme sports + relative clauses
Extreme sports + relative clauses
 
Tips and tricks for serials
Tips and tricks for serialsTips and tricks for serials
Tips and tricks for serials
 
Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
Making an impact in Rural India with Ruby - Lightning talk at GCRC 2014
 
Ayat General CV
Ayat General  CVAyat General  CV
Ayat General CV
 
Hardware y sonido (Practica 18)
Hardware y sonido (Practica 18)Hardware y sonido (Practica 18)
Hardware y sonido (Practica 18)
 
O guia do SEO : Evoluções & Perspectivas 2014
O guia do SEO : Evoluções & Perspectivas 2014O guia do SEO : Evoluções & Perspectivas 2014
O guia do SEO : Evoluções & Perspectivas 2014
 
Tema 14
Tema 14Tema 14
Tema 14
 
Profiel
ProfielProfiel
Profiel
 
Resumende quiebras
Resumende quiebrasResumende quiebras
Resumende quiebras
 
Jalal Jamil CV
Jalal Jamil CVJalal Jamil CV
Jalal Jamil CV
 
Boletín nº 12 corpus 2015
Boletín nº 12 corpus 2015Boletín nº 12 corpus 2015
Boletín nº 12 corpus 2015
 
Devocionales Hambre de Justicia
Devocionales Hambre de JusticiaDevocionales Hambre de Justicia
Devocionales Hambre de Justicia
 
como hacer una buena búsqueda
como hacer una buena búsqueda como hacer una buena búsqueda
como hacer una buena búsqueda
 

Ähnlich wie GCRC 2014 - The Dark Side of Ruby

Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
Ignacio Martín
 
Beware: Sharp Tools
Beware: Sharp ToolsBeware: Sharp Tools
Beware: Sharp Tools
chrismdp
 
Fluent Refactoring (Cascadia Ruby Conf 2013)
Fluent Refactoring (Cascadia Ruby Conf 2013)Fluent Refactoring (Cascadia Ruby Conf 2013)
Fluent Refactoring (Cascadia Ruby Conf 2013)
Sam Livingston-Gray
 

Ähnlich wie GCRC 2014 - The Dark Side of Ruby (20)

ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
Defly
DeflyDefly
Defly
 
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)
 
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
 
An Introduction To jQuery
An Introduction To jQueryAn Introduction To jQuery
An Introduction To jQuery
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of Security
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
 
Ruby is Awesome
Ruby is AwesomeRuby is Awesome
Ruby is Awesome
 
Ruby Kaigi 2008 LT
Ruby Kaigi 2008 LTRuby Kaigi 2008 LT
Ruby Kaigi 2008 LT
 
Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)Dip Your Toes In The Sea Of Security (PHPNW16)
Dip Your Toes In The Sea Of Security (PHPNW16)
 
Beware: Sharp Tools
Beware: Sharp ToolsBeware: Sharp Tools
Beware: Sharp Tools
 
Ruby is an Acceptable Lisp
Ruby is an Acceptable LispRuby is an Acceptable Lisp
Ruby is an Acceptable Lisp
 
Aprendendo solid com exemplos
Aprendendo solid com exemplosAprendendo solid com exemplos
Aprendendo solid com exemplos
 
Fluent Refactoring (Cascadia Ruby Conf 2013)
Fluent Refactoring (Cascadia Ruby Conf 2013)Fluent Refactoring (Cascadia Ruby Conf 2013)
Fluent Refactoring (Cascadia Ruby Conf 2013)
 
There and Back Again
There and Back AgainThere and Back Again
There and Back Again
 
Ruby Robots
Ruby RobotsRuby Robots
Ruby Robots
 
Learn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik CubeLearn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik Cube
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 

Mehr von Gautam Rege

Mehr von Gautam Rege (13)

RubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneurRubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneur
 
GoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPHGoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPH
 
Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$
 
WIDS - Gamifying Open Source
WIDS - Gamifying Open SourceWIDS - Gamifying Open Source
WIDS - Gamifying Open Source
 
Gamifying Open Source
Gamifying Open SourceGamifying Open Source
Gamifying Open Source
 
Affordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolutionAffordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolution
 
WebSummit 2015 - Gopher it
WebSummit 2015 - Gopher itWebSummit 2015 - Gopher it
WebSummit 2015 - Gopher it
 
Dont test your code
Dont test your codeDont test your code
Dont test your code
 
Art of speaking at tech conferences
Art of speaking at tech conferencesArt of speaking at tech conferences
Art of speaking at tech conferences
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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 New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

GCRC 2014 - The Dark Side of Ruby