Share
Embed
Don't let the Cocoa Api Crush your Ruby Code - Inspect2014
Alex Rothenberg
May 29, 2014
Transcript
67 slides
-
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 -
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 -
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! -
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! -
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! -
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! -
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! -
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 -
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! -
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! -
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! -
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 -
def add_name(frame)! subview = RowView.alloc.initWithFrame(frame)! subview.caption = 'Name'! subview.text =
'Alex Rothenberg'! view.addSubview subview! end -
class RowView < UIView! attr_reader :caption_label, :text_label! ! def initWithFrame(frame)!
super! addSubview caption_label! addSubview text_label! self! end! ! end! -
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! -
def add_name(frame)! subview = RowView.alloc.initWithFrame(frame)! subview.caption = 'Name'! subview.text =
'Alex Rothenberg'! view.addSubview subview! end -
view.frame = [ [x, y], [width, height] ] view.frame =
[! [x, y], ! [width, height]! ]! -
class UIView! def left=(new_left)! self.frame = [[new_left, top], ! !
! ! ! ! ! ! ! ! frame.size]! end! end! -
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!