12. The good stuff
feel_the_awesome! if can_i_handle_it?
Ruled by
convention
(ignorable) Shiny inline if
No more is_x()
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. 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. 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!”
17. Classics
class Kitten < Moggy
attr_accessor :cuteness_score
def initialize
@cuteness_score = 100000000
end
end
kitty = Kitten.new
kitten.cuteness_score #=> 100000000
18. Classics
class Kitten < Moggy
attr_accessor :cuteness_score
def initialize
@cuteness_score = 99E99
end
end
kitty = Kitten.new
kitten.cuteness_score #=> 99E99
19. Classics
class Kitten < Moggy
attr_accessor :cuteness_score
def initialize
@cuteness_score = “WTF”
end
end
kitty = Kitten.new
kitten.cuteness_score #=> “WTF”
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. 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
23. Hate your fellow developer
class String
def to_s
reverse
end
end
“Fishy”.to_s #=> “yhsiF”
24. Spread the (un)love
module Evil
def to_s
super.reverse
end
end
class Object ; include Evil ; end
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
“I'm 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. 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!
28. Q&A
Show us some ruby in IRB!
irb(main):003:0* puts "Hello Nrug"
Hello Nrug
=> nil
More examples of class / object, and accessors
irb(main):004:0> class Question
irb(main):005:1> attr_accessor :title
irb(main):006:1> end
=> nil
irb(main):007:0> q = Question.new
=> #<Question:0x007fd44a947160>
irb(main):008:0> q.title
=> nil
irb(main):009:0> q.title = "Why won't my baby stop drinking rum?"
=> "Why won't my baby stop drinking rum?"
irb(main):010:0> q.title
=> "Why won't my baby stop drinking rum?"
irb(main):011:0> q.title=("Why won't my baby stop drinking rum?")
=> "Why won't my baby stop drinking rum?"
irb(main):012:0> q.title=("Why won't my baby stop drinking rum?").reverse
=> "?mur gniknird pots ybab ym t'now yhW"
irb(main):013:0> q.title=("Why won't my baby stop drinking rum?").split('
').reverse.join(' ')
=> "rum? drinking stop baby my won't Why"
29. More Q&A
How would you do a traditional rescue?
# This is the full syntax
irb(main):014:0> def funny
irb(main):015:1> begin
irb(main):016:2* 10 / 0
irb(main):017:2> rescue
irb(main):018:2> "You FOOL!"
irb(main):019:2> end
irb(main):020:1> end
=> nil
irb(main):021:0> funny
=> "You FOOL!"
# But if your methods are small enough,
# begin...end can be implied
irb(main):022:0> def funny
irb(main):023:1> 10 / 0
irb(main):024:1> rescue
irb(main):025:1> "You FOOL!"
irb(main):026:1> end
=> nil
irb(main):027:0> funny
=> "You FOOL!"
30. Extra Awesome Q&A
Here I try to show that you can add methods on objects, not just on classes
irb(main):044:0* a = "adsihudsbhjdsghjasjh"
irb(main):051:0> def a.find_j
irb(main):052:1> self.scan(/j/).count
irb(main):053:1> end
=> nil
irb(main):054:0> a.find_j
=> 3
irb(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_j
NoMethodError: 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. Q&A For Fun And Profit
Question: 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 a
convention for very short methods and descriptive method names:
irb(main):028:0> def destroy_the_world_with_my_massive_laser
irb(main):029:1> start_laser
irb(main):030:1> destroy_world
irb(main):031:1> have_cocktails
irb(main):032:1> end
When code looks like this, the only reason to add comments is to document
your gem for people who can't 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. Rocking that Q&A
Now I show that it's not just methods that return their final value – classes do too...
irb(main):062:0* class Kitty
irb(main):063:1> self
irb(main):064:1> end
=> Kitty
irb(main):065:0> class Kitty
irb(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 Kitty
irb(main):069:1> def self.leg_count
irb(main):070:2> 4
irb(main):071:2> end
irb(main):072:1> end
=> nil
irb(main):073:0> class Kitty
irb(main):074:1> def self.leg_count
irb(main):075:2> @leg_count # Class-level variables!
irb(main):076:2> end
irb(main):077:1> end
=> nil
# @@varname has some odd side effects. It is rarely needed.
33. And now for the finale
Finally, 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
=> 4000
Final notes:
Unlike certain compiled languages, class definitions are essentially just code. They
get evaluated line by line, so a “puts” statement in a class gets called as the class is
being set up.
If anyone has any questions, or bits of code that they can't get to work for love nor
money, 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!