Ruby The Fun Bits
Upcoming SlideShare
Loading in...5

Ruby The Fun Bits



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

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



Total Views
Slideshare-icon Views on SlideShare
Embed Views



2 Embeds 6 5 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Ruby The Fun Bits Ruby The Fun Bits Presentation Transcript

    • Ruby The Fun Bits Matthew Bennett @undecisive
    • And you are... Audience Participation
    • 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)
    • 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 = kitten.sugary_high #=> “Woot”
    • But classes are objectsCookie = do def sugary_high “Woot” endendkitten = Cookie.newkitten.sugary_high #=> “Woot”
    • 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 No more is_x()
    • 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.)
    • 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)
    • Rescue me! def raise “The kitten is falling out of the tree” something_that_will_never_happen end rescue “My hero!”
    • Gratuitous Cat
    • Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = 100000000 end end kitty = kitten.cuteness_score #=> 100000000
    • Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = 99E99 end end kitty = kitten.cuteness_score #=> 99E99
    • Classics class Kitten < Moggy attr_accessor :cuteness_score def initialize @cuteness_score = “WTF” end end kitty = kitten.cuteness_score #=> “WTF”
    • Classics class Kitten < Moggy attr_accessor :cuteness_score end kitty = kitty.cuteness_score = :cuter_than_10_kitties kitten.cuteness_score=(:cuter_than_10_kitties)
    • 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
    • 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 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
    • 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!
    • Questions?
    • 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: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"
    • 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!"
    • 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>
    • 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.
    • 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.
    • 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. Have hundreds of awesome presentations on ruby for free.Check them out!