Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Don't let the Cocoa Api Crush your Ruby Code - Inspect2014

Don't let the Cocoa Api Crush your Ruby Code - Inspect2014

Alex Rothenberg

May 29, 2014
Tweet

More Decks by Alex Rothenberg

Other Decks in Programming

Transcript

  1. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! itemTitle = UILabel.alloc! .initWithFrame [[30,100],! [300,22]]! itemTitle.text = 'Alex Rothenberg'! view.addSubview itemTitle! end! ! end
  2. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! itemTitle = UILabel.alloc! .initWithFrame [[30,100],[300,22]]! itemTitle.text = 'Alex Rothenberg'! view.addSubview itemTitle! ! itemTitle = UILabel.alloc! .initWithFrame [[30,130],[300,22]]! itemTitle.text = 'Rubyist'! view.addSubview itemTitle! end! ! end
  3. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! ! itemTitle = UILabel.alloc! .initWithFrame [[30,100],[300,22]]! itemTitle.text = 'Alex Rothenberg'! view.addSubview itemTitle! ! ! itemTitle = UILabel.alloc! .initWithFrame [[30,130],[300,22]]! itemTitle.text = 'Rubyist'! view.addSubview itemTitle! ! ! itemTitle = UILabel.alloc! .initWithFrame [[30,160],[300,22]]! itemTitle.text = '[email protected]'! view.addSubview itemTitle! end! ! end!
  4. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! # The name! itemTitle = UILabel.alloc! .initWithFrame [[30,100],[300,22]]! itemTitle.text = 'Alex Rothenberg'! view.addSubview itemTitle! ! # The title! itemTitle = UILabel.alloc! .initWithFrame [[30,130],[300,22]]! itemTitle.text = 'Rubyist'! view.addSubview itemTitle! ! # Email! itemTitle = UILabel.alloc! .initWithFrame [[30,160],[300,22]]! itemTitle.text = '[email protected]'! view.addSubview itemTitle! end! ! end!
  5. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! # The name! nameLabel = UILabel.alloc! .initWithFrame [[30,100],[300,22]]! nameLabel.text = 'Alex Rothenberg'! view.addSubview nameLabel! ! # The title! titleLabel = UILabel.alloc! .initWithFrame [[30,130],[300,22]]! titleLabel.text = 'Rubyist'! view.addSubview titleLabel! ! # Email! emailLabel = UILabel.alloc! .initWithFrame [[30,160],[300,22]]! emailLabel.text = '[email protected]'! view.addSubview emailLabel! ! # Phone! phoineLabel = UILabel.alloc! .initWithFrame [[30,190],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! end! ! end!
  6. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! # The name! nameLabel = UILabel.alloc.initWithFrame [[30,100],[300,22]]! nameLabel.text = 'Alex Rothenberg'! view.addSubview nameLabel! ! # The title! titleLabel = UILabel.alloc.initWithFrame [[30,130],[300,22]]! titleLabel.text = 'Rubyist'! view.addSubview titleLabel! ! # Email! emailLabel = UILabel.alloc.initWithFrame [[30,160],[300,22]]! emailLabel.text = '[email protected]'! view.addSubview emailLabel! ! # Mobile Phone! phoineLabel = UILabel.alloc.initWithFrame [[30,190],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! ! # Office Phone! phoineLabel = UILabel.alloc.initWithFrame [[30,220],[300,22]]! phoineLabel.text = '555-7890'! view.addSubview phoineLabel! ! # Office Address! phoineLabel = UILabel.alloc.initWithFrame [[30,250],[300,22]]! phoineLabel.text = '123 Office Drive'! view.addSubview phoineLabel! end! ! end!
  7. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! # The name! caption = UILabel.alloc.initWithFrame [[20,100],[90,22]]! caption.text = 'Name'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! nameLabel = UILabel.alloc.initWithFrame [[110,100],[300,22]]! nameLabel.text = 'Alex Rothenberg'! view.addSubview nameLabel! ! # The title! caption = UILabel.alloc.initWithFrame [[20,130],[90,22]]! caption.text = 'Title'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! titleLabel = UILabel.alloc.initWithFrame [[110,130],[300,22]]! titleLabel.text = 'Rubyist'! view.addSubview titleLabel! ! # Email! caption = UILabel.alloc! .initWithFrame [[20,160],[90,22]]! caption.text = 'Email'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! emailLabel = UILabel.alloc.initWithFrame [[110,160],[300,22]]! emailLabel.text = '[email protected]'! view.addSubview emailLabel! ! # Mobile Phone! caption = UILabel.alloc.initWithFrame [[20,190],[90,22]]! caption.text = 'Mobile'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,190],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! ! # Office Phone! caption = UILabel.alloc.initWithFrame [[20,220],[90,22]]! caption.text = 'Office'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,220],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! ! # Office Address! caption = UILabel.alloc.initWithFrame [[20,250],[90,22]]! caption.text = 'Address'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,250],[300,22]]! phoineLabel.text = '123 Office Drive'! view.addSubview phoineLabel! end! ! end!
  8. 1. Your class can be no longer than 100 lines

    of code.
 2. Your methods can be no longer than five lines of code.
 3. You can pass no more than four parameters and you can’t just make it one big hash. Sandi Metz Rules
  9. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    ! # The name! caption = UILabel.alloc.initWithFrame [[20,100],[90,22]]! caption.text = 'Name'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! nameLabel = UILabel.alloc.initWithFrame [[110,100],[300,22]]! nameLabel.text = 'Alex Rothenberg'! view.addSubview nameLabel! ! # The title! caption = UILabel.alloc.initWithFrame [[20,130],[90,22]]! caption.text = 'Title'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! titleLabel = UILabel.alloc.initWithFrame [[110,130],[300,22]]! titleLabel.text = 'Rubyist'! view.addSubview titleLabel! ! # Email! caption = UILabel.alloc! .initWithFrame [[20,160],[90,22]]! caption.text = 'Email'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! emailLabel = UILabel.alloc.initWithFrame [[110,160],[300,22]]! emailLabel.text = '[email protected]'! view.addSubview emailLabel! ! # Mobile Phone! caption = UILabel.alloc.initWithFrame [[20,190],[90,22]]! caption.text = 'Mobile'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,190],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! ! # Office Phone! caption = UILabel.alloc.initWithFrame [[20,220],[90,22]]! caption.text = 'Office'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,220],[300,22]]! phoineLabel.text = '555-1212'! view.addSubview phoineLabel! ! # Office Address! caption = UILabel.alloc.initWithFrame [[20,250],[90,22]]! caption.text = 'Address'! caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! phoineLabel = UILabel.allocinitWithFrame [[110,250],[300,22]]! phoineLabel.text = '123 Office Drive'! view.addSubview phoineLabel! end! ! end!
  10. class MyViewController < UIViewController! ! def viewDidLoad! view.backgroundColor = UIColor.whiteColor!

    add_name! add_title! add_email! add_mobile_phone! add_office_phone! add_office_address! end! ! end!
  11. def add_name! caption = UILabel.alloc! .initWithFrame [[20,100],[90,22]]! caption.text = 'Name'!

    caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! name_label = UILabel.alloc! .initWithFrame [[110,100],[300,22]]! name_label.text = 'Alex Rothenberg'! view.addSubview name_label! end!
  12. def add_name! caption = UILabel.alloc! .initWithFrame [[20,100],[90,22]]! caption.text = 'Name'!

    caption.font = UIFont.boldSystemFontOfSize(20)! view.addSubview caption! name_label = UILabel.alloc! .initWithFrame [[110,100],[300,22]]! name_label.text = 'Alex Rothenberg'! view.addSubview name_label! end! Text Caption
  13. class RowView < UIView! attr_reader :caption_label, :text_label! ! def initWithFrame(frame)!

    super! addSubview caption_label! addSubview text_label! self! end! ! end!
  14. def caption_label! if @caption_label.nil?! @caption_label = UILabel.alloc! .initWithFrame([[0, 0],[ 90,22]])!

    @caption_label.font = ! UIFont.boldSystemFontOfSize(20)! end! @caption_label! end!
  15. class UIView! def top! def left! def height! def width!

    def bottom! def right! ! def top=(new_top)! def left=(new_left)! def height=(new_height)! def width=(new_width)! end!