Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby       The Fun Bits            Matthew Bennett              @undecisive          matt@wearepandr.com
And you are...       Audience      Participation
Ruby is...                    Orthogonal                    Functional                    Extensible                    In...
levitating Happycat
Terminology IRB    Interactive Ruby Gems   Libraries / Packages
Terminology Duck typing Object orientation Translation:  Coding like a HUMAN
CS101: Classes / Objects
Classes != objects class Cookie   def sugary_high       “Woot”   end end kitten = Cookie.new kitten.sugary_high    #=> “Wo...
But classes are objectsCookie = Class.new do  def sugary_high      “Woot”  endendkitten = Cookie.newkitten.sugary_high    ...
Traditional stylee if one == :boring_sod   “Old Skewl” else   “Badass” end
What if...
The good stuff feel_the_awesome!    if can_i_handle_it?Ruled byconvention(ignorable)   Shiny inline if                    ...
Failing to not be un-negated             use_ruby unless self.mad?  (Note: If you ever show me code with unless...else, I ...
True Object Orientation 0.9.round                   #=> 1 “Groinal attachment”.reverse #=> "tnemhcatta laniorG" 100.times ...
Rescue me! def something.fail   raise “The kitten is falling out of the tree”   something_that_will_never_happen end somet...
Gratuitous Cat
Classics class Kitten < Moggy   attr_accessor :cuteness_score   def initialize       @cuteness_score = 100000000   end end...
Classics class Kitten < Moggy   attr_accessor :cuteness_score   def initialize       @cuteness_score = 99E99   end end kit...
Classics class Kitten < Moggy   attr_accessor :cuteness_score   def initialize       @cuteness_score = “WTF”   end end kit...
Classics class Kitten < Moggy   attr_accessor :cuteness_score end kitty = Kitten.new kitty.cuteness_score = :cuter_than_10...
Classics                              at class Kitten < Moggy            tr_                                     re       ...
I can comez wiv?
Hate your fellow developer class String   def to_s       reverse   end end “Fishy”.to_s    #=> “yhsiF”
Spread the (un)love module Evil   def to_s       super.reverse   end end class Object ; include Evil ; end
More funky [ Object, String, Fixnum, Array ].each do | klass |   EEK = klass          # class reopened below must be a con...
Because you are worth it●   You deserve:    –   A language that obeys your whims    –   A language that allows you to do t...
Questions?
Q&AShow us some ruby in IRB! irb(main):003:0* puts "Hello Nrug" Hello Nrug => nilMore examples of class / object, and acce...
More Q&AHow would you do a traditional rescue?   # This is the full syntaxirb(main):014:0> def funnyirb(main):015:1>      ...
Extra Awesome Q&A  Here I try to show that you can add methods on objects, not just on classesirb(main):044:0*   a = "adsi...
Q&A For Fun And ProfitQuestion: I hear programmers complaining a lot about badly commented code,and keeping comments up to...
Rocking that Q&ANow I show that its not just methods that return their final value – classes do too...irb(main):062:0* cla...
And now for the finaleFinally, using a handy trick involving reopening classes, and the value of “self”,we can create gett...
Upcoming SlideShare
Loading in …5
×

Ruby The Fun Bits

523 views

Published on

An introduction to all the things that make Ruby a fun programming language to learn.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ruby The Fun Bits

  1. 1. Ruby The Fun Bits Matthew Bennett @undecisive matt@wearepandr.com
  2. 2. And you are... Audience Participation
  3. 3. Ruby is... Orthogonal Functional Extensible Intuitive Sugary Fun Simple Explorable Expressive Performant Documented Object Oriented Duck Typed Open Source Interpreted Are you actually Reading this Cross Platform Japaneasey Interesting Web scale Readable Utter tosh Unicorns Garbage collected Caring Social Code Succinct Flexible Expressive Semicolon-free (if you wish)
  4. 4. levitating Happycat
  5. 5. Terminology IRB Interactive Ruby Gems Libraries / Packages
  6. 6. Terminology Duck typing Object orientation Translation: Coding like a HUMAN
  7. 7. CS101: Classes / Objects
  8. 8. Classes != objects class Cookie def sugary_high “Woot” end end kitten = Cookie.new kitten.sugary_high #=> “Woot”
  9. 9. But classes are objectsCookie = Class.new do def sugary_high “Woot” endendkitten = Cookie.newkitten.sugary_high #=> “Woot”
  10. 10. Traditional stylee if one == :boring_sod “Old Skewl” else “Badass” end
  11. 11. What if...
  12. 12. The good stuff feel_the_awesome! if can_i_handle_it?Ruled byconvention(ignorable) Shiny inline if No more is_x()
  13. 13. Failing to not be un-negated use_ruby unless self.mad? (Note: If you ever show me code with unless...else, I will kill you.)
  14. 14. True Object Orientation 0.9.round #=> 1 “Groinal attachment”.reverse #=> "tnemhcatta laniorG" 100.times do puts “I am a fish” end (P.S: Sorry if you are missing Red Dwarf for this rubbish presentation)
  15. 15. Rescue me! def something.fail raise “The kitten is falling out of the tree” something_that_will_never_happen end something.fail rescue “My hero!”
  16. 16. Gratuitous Cat
  17. 17. Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = 100000000 end end kitty = Kitten.new kitten.cuteness_score #=> 100000000
  18. 18. Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = 99E99 end end kitty = Kitten.new kitten.cuteness_score #=> 99E99
  19. 19. Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = “WTF” end end kitty = Kitten.new kitten.cuteness_score #=> “WTF”
  20. 20. Classics class Kitten < Moggy attr_accessor :cuteness_score end kitty = Kitten.new kitty.cuteness_score = :cuter_than_10_kitties kitten.cuteness_score=(:cuter_than_10_kitties)
  21. 21. Classics at class Kitten < Moggy tr_ re ad def cuteness_score er :c u @cuteness_score te n es s_ end sc or e def cuteness_score=(mein_score) e or sc @cuteness_score = mein_score s_ es n te end u :c end e r rit _ w tr at
  22. 22. I can comez wiv?
  23. 23. Hate your fellow developer class String def to_s reverse end end “Fishy”.to_s #=> “yhsiF”
  24. 24. Spread the (un)love module Evil def to_s super.reverse end end class Object ; include Evil ; end
  25. 25. More funky [ Object, String, Fixnum, Array ].each do | klass | EEK = klass # class reopened below must be a constants, class EEK # but constants do not have to be constant. def to_s “Im gonna eat you little fishy” end end end (irb):81: warning: already initialized constant EEK (irb):81: warning: already initialized constant EEK (irb):81: warning: already initialized constant EEK
  26. 26. Because you are worth it● You deserve: – A language that obeys your whims – A language that allows you to do the logically impossible – A language that tries desperately to be your friend VOTE RUBY!
  27. 27. Questions?
  28. 28. Q&AShow us some ruby in IRB! irb(main):003:0* puts "Hello Nrug" Hello Nrug => nilMore examples of class / object, and accessorsirb(main):004:0> class Questionirb(main):005:1> attr_accessor :titleirb(main):006:1> end=> nilirb(main):007:0> q = Question.new=> #<Question:0x007fd44a947160>irb(main):008:0> q.title=> nilirb(main):009:0> q.title = "Why wont my baby stop drinking rum?"=> "Why wont my baby stop drinking rum?"irb(main):010:0> q.title=> "Why wont my baby stop drinking rum?"irb(main):011:0> q.title=("Why wont my baby stop drinking rum?")=> "Why wont my baby stop drinking rum?"irb(main):012:0> q.title=("Why wont my baby stop drinking rum?").reverse=> "?mur gniknird pots ybab ym tnow yhW"irb(main):013:0> q.title=("Why wont my baby stop drinking rum?").split().reverse.join( )=> "rum? drinking stop baby my wont Why"
  29. 29. More Q&AHow would you do a traditional rescue? # This is the full syntaxirb(main):014:0> def funnyirb(main):015:1> beginirb(main):016:2* 10 / 0irb(main):017:2> rescueirb(main):018:2> "You FOOL!"irb(main):019:2> endirb(main):020:1> end=> nilirb(main):021:0> funny=> "You FOOL!" # But if your methods are small enough, # begin...end can be impliedirb(main):022:0> def funnyirb(main):023:1> 10 / 0irb(main):024:1> rescueirb(main):025:1> "You FOOL!"irb(main):026:1> end=> nilirb(main):027:0> funny=> "You FOOL!"
  30. 30. Extra Awesome Q&A Here I try to show that you can add methods on objects, not just on classesirb(main):044:0* a = "adsihudsbhjdsghjasjh"irb(main):051:0> def a.find_jirb(main):052:1> self.scan(/j/).countirb(main):053:1> end=> nilirb(main):054:0> a.find_j=> 3irb(main):055:0> a # Here is a different String. It will not have the method.=> "adsihudsbhjdsghjasjh"irb(main):056:0> "ahgakwdhabhmsbd"=> "ahgakwdhabhmsbd"irb(main):057:0> "ahgakwdhabhmsbd".find_jNoMethodError: undefined method `find_j for "ahgakwdhabhmsbd":String from (irb):57 from /Users/matthew/.rbenv/versions/1.9.3-p194/bin/irb:12:in`<main>
  31. 31. Q&A For Fun And ProfitQuestion: I hear programmers complaining a lot about badly commented code,and keeping comments up to date. How does Ruby mitigate this?Answer: The ruby community drives certain standards. One of these is aconvention for very short methods and descriptive method names:irb(main):028:0> def destroy_the_world_with_my_massive_laserirb(main):029:1> start_laserirb(main):030:1> destroy_worldirb(main):031:1> have_cocktailsirb(main):032:1> end When code looks like this, the only reason to add comments is to document your gem for people who cant be bothered to look through the code. Another question asked: What tools can you use in ruby to ensure conventions are followed? Answer: I have a large bamboo stick that works really well. For a serious answer, there is ruby support in IDEs such as Netbeans and Eclipse, but these are rarely faultless. Peer pressure is by far the easiest and most reliable tool.
  32. 32. Rocking that Q&ANow I show that its not just methods that return their final value – classes do too...irb(main):062:0* class Kittyirb(main):063:1> selfirb(main):064:1> end=> Kittyirb(main):065:0> class Kittyirb(main):066:1> "hi"irb(main):067:1> end=> "hi" # Question: When do I use @@varname – Answer: Never! # We can make use of “self” and the fact that you can create # methods on objects, and that classes are also objects...irb(main):068:0> class Kittyirb(main):069:1> def self.leg_countirb(main):070:2> 4irb(main):071:2> endirb(main):072:1> end=> nilirb(main):073:0> class Kittyirb(main):074:1> def self.leg_countirb(main):075:2> @leg_count # Class-level variables!irb(main):076:2> endirb(main):077:1> end=> nil # @@varname has some odd side effects. It is rarely needed.
  33. 33. And now for the finaleFinally, using a handy trick involving reopening classes, and the value of “self”,we can create getters and setters on the class level irb(main):090:0> class Kitty irb(main):091:1> class << self irb(main):092:2> attr_accessor :leg_count irb(main):093:2> end irb(main):094:1> end => nil irb(main):095:0> Kitty.leg_count = 4000 => 4000Final notes:Unlike certain compiled languages, class definitions are essentially just code. Theyget evaluated line by line, so a “puts” statement in a class gets called as the class isbeing set up.If anyone has any questions, or bits of code that they cant get to work for love normoney, by all means send me a gist. My twitter username is @undecisive.Ruby is a wonderful language, and you have to try quite hard to find its limits.http://www.confreaks.com/ Have hundreds of awesome presentations on ruby for free.Check them out!

×