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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de Gautam Rege

Mais de 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
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

GCRC 2014 - The Dark Side of Ruby