from Ruby to Objective-C
I’m a Flash guy (≈ 9 years)
I’m a Python guy (≈ 3 years)
I’m a Ruby guy (≈ 5 years)
I’m a iOS app guy (≈ 3 years)
Ruby > Rails
Current Status
80% iOS app, 20% Ruby/Rails
100% Ruby Lover!
Rails Girls Taipei
Rails Girls Taipei
WebConf Taiwan 2014
Today, I’m NOT talking about..
how to use Ruby to write iOS app!
I’m going to talk about..
what I learned in Ruby…
and move to Objective-C
after all, our life, time and
resources are limited
what about Objective-C?
Objective-C …
“it has god dame long method name
and weird parameters!”
NSArray* languages = [NSArray arrayWithObjects:@"Ru...
Objective-C …
“what the hell is the square bracket!”
NSString* myName = @"eddie kao";
NSLog(@"%@", [myName uppercaseString...
Objective-C …
“WTF! my app crashed again!!”
Introduction
Ruby was born on 1993
Objective-C was born on 1983
they have the same ancestor
Smalltalk
photo by Marcin Wichary
Ruby is general-purpose
Objective-C mainly used in
Mac/iOS app development
Ruby != Rails
Objective-C != Cocoa Framework
they have something in common..
both Ruby and Objective-C are
Object-Oriented
both Ruby and Objective-C are
strongly typed language.
Ruby is a dynamic language
Objective-C is a dynamic language
both Ruby and Objective-C are
Dynamic Typing
Type checking..
- (void) makeSomeNoise:(id) sender
{
if ([sender isKindOfClass:[RobberDuck class]])
{
RobberDuck* duck = (...
or you can do this..
- (void) makeSomeNoise:(id) sender
{
if ([sender respondsToSelector:@selector(quack)])
{
[sender quac...
id
Objective-C is superset of C
Objective-C is still C
NSString, NSArray, NSNumber…
NextSTEP
CF… = Core Foundation
CG… = Core Graphic
CL… = Core Location
CA… = Core Animation
UI… = User Interface
OOP
everything in Ruby is an object…
and almost everything in
Objective-C is an objects..
there’re still some primitive data
types in Objective-C
object model
class Animal
end
!

class Dog < Animal
end
object model
dog = Dog.new
!

puts "class of dog is #{dog.class}”
# Dog
puts "superclass of dog is #{dog.class.superclass}...
object model
@interface Animal : NSObject
@end
!

@implementation Animal
@end
!

@interface Dog : Animal
@end
!

@implemen...
object model
Dog* dog = [[Dog alloc] init];
!
NSLog(@"class of dog is %@", [dog class]); # Dog
!
NSLog(@"superclass of dog...
Object Model
reference: http://goo.gl/wYL6gT
method & message
method definition
def say_hello(someone, message)
puts "Hello #{someone}, #{message}"
end

- (void) sayHello:(id)someOne wi...
sending message
dog.walk()

[dog walk];

# or you can omit the parentheses
sending message
fox.say_something "hi, Ruby"

[fox saySomething:@"hi, Ruby"];

# what does the fox say?
sending message
puts 1 + 2

puts 1.+(2)

puts 1.send(:+, 2)
sending message
class Bank
def save(money)
puts "you just saved #{money} dollars"
end
end
!

bank = Bank.new
bank.save 20
...
sending message
@interface Bank : NSObject
- (void) save:(NSNumber *) money;
@end
!

@implementation Bank
- (void)save:(NS...
block
block
p1 = Proc.new { puts "Hello, Proc Block" }
p1.call
!

p2 = lambda { puts "Hello, Lambda Block" }
p2.call
block
^{ };
block
typedef void (^MyBlock)(void);
int age = 18;
MyBlock theBlock = ^{
NSLog(@"Hello, Objective-C Block, your age = %d",...
block
3.times { |i| puts i }

NSArray* list = @[@1, @2, @3];
[list enumerateObjectsUsingBlock:^(NSNumber* num,
NSUInteger ...
iteration
iteration
list = [1, 2, 3, 4, 5]
!

sum = 0
!

list.each { |num|
sum += num
}
!

puts "sum = #{sum}"
iteration
NSArray* list = @[@1, @2, @3, @4, @5];
!

__block int sum = 0;
!

[list enumerateObjectsUsingBlock:^(NSNumber* n...
iteration
class Fox
def say
puts "what does the fox say?"
end
end
!

fox1 = Fox.new
fox2 = Fox.new
fox3 = Fox.new
foxes = ...
iteration
@interface Fox : NSObject
- (void) say;
@end
!

@implementation Fox
- (void) say
{
NSLog(@"what does the fox say...
iteration
Fox* fox1 = [[Fox alloc] init];
Fox* fox2 = [[Fox alloc] init];
Fox* fox3 = [[Fox alloc] init];
!

NSArray* foxe...
add methods at runtime
Open class
class String
def is_awesome?
return true if self == "Ruby Tuesday"
end
end
!

puts "Ruby Tuesday".is_awesome?
Category
@interface NSString(RubyTuesday)
- (BOOL) isAwesome;
@end
!

@implementation NSString(RubyTuesday)
- (BOOL) isAwe...
Category
NSString* meetup = @"Ruby Tuesday";
if ([meetup isAwesome])
{
NSLog(@"AWESOME!");
}
<objc/runtime.h>
Working with Classes
class_getName
class_getSuperclass
class_getInstanceVariable
class_getClassVariable
c...
<objc/runtime.h>
Working with Instances
object_copy
object_dispose
object_setInstanceVariable
object_getInstanceVariable
o...
reflection
-

(BOOL)
(BOOL)
(BOOL)
(BOOL)
..

isKindOfClass:(Class) aClass
isMemberOfClass:(Class) aClass
respondsToSelecto...
ecosystem
open source projects on Github
Ruby : 76,574
Objective-C : 22,959
Ruby : bundler
source 'https://rubygems.org'
!

gem 'rails', '3.2.8'
gem 'mysql2'
!

group :assets do
gem 'sass-rails',
'~...
Objective-C : cocoapods
platform :ios, '6.0'
!

pod
pod
pod
pod
pod
pod

'Facebook-iOS-SDK', '~> 3.5.1’
'JSONKit', '~> 1.5...
IMHO
Objective-C is not really hard to
learn…
the actual difficult part in iOS app
development is Cocoa Framework
Objective-C would be almost
useless without Cocoa Framework
Ruby without Rails?!
design patterns
MVC

observer
notifications singleton
delegation
command
composite
target-action

proxy
C
what else..
photoed by JD Hancock
Flash Display Hierarchy
reference: http://goo.gl/2mzyMY
UIKit and AppKit framework Hierarchy
reference: http://goo.gl/xhS7m7
Views
reference: http://goo.gl/xhS7m7
Views
reference: http://goo.gl/xhS7m7
and I read Ruby source code..
my iOS app dev experience
= Ruby + C + Flash/AS3
=

+
Contacts
⾼高⾒見⻯⿓龍

Website

http://www.eddie.com.tw

Blog

http://blog.eddie.com.tw

Plurk

http://www.plurk.com/aquarianbo...
from Ruby to Objective-C
from Ruby to Objective-C
from Ruby to Objective-C
from Ruby to Objective-C
from Ruby to Objective-C
Upcoming SlideShare
Loading in …5
×

from Ruby to Objective-C

919 views
751 views

Published on

because Ruby and Objective-C has the same ancestor, they have many things in common, and in this talk I mainly talk about what I learned in Ruby, borrow those experiences and move to Objective-C :)

Ruby Tuesday #27 @Taiwan

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
919
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

from Ruby to Objective-C

  1. 1. from Ruby to Objective-C
  2. 2. I’m a Flash guy (≈ 9 years) I’m a Python guy (≈ 3 years) I’m a Ruby guy (≈ 5 years) I’m a iOS app guy (≈ 3 years)
  3. 3. Ruby > Rails
  4. 4. Current Status 80% iOS app, 20% Ruby/Rails
  5. 5. 100% Ruby Lover!
  6. 6. Rails Girls Taipei
  7. 7. Rails Girls Taipei
  8. 8. WebConf Taiwan 2014
  9. 9. Today, I’m NOT talking about.. how to use Ruby to write iOS app!
  10. 10. I’m going to talk about..
  11. 11. what I learned in Ruby…
  12. 12. and move to Objective-C
  13. 13. after all, our life, time and resources are limited
  14. 14. what about Objective-C?
  15. 15. Objective-C … “it has god dame long method name and weird parameters!” NSArray* languages = [NSArray arrayWithObjects:@"Ruby", @"PHP", @"Objective-C", nil]; ! [languages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"language = %@", obj); }];
  16. 16. Objective-C … “what the hell is the square bracket!” NSString* myName = @"eddie kao"; NSLog(@"%@", [myName uppercaseString]);
  17. 17. Objective-C … “WTF! my app crashed again!!”
  18. 18. Introduction
  19. 19. Ruby was born on 1993 Objective-C was born on 1983
  20. 20. they have the same ancestor
  21. 21. Smalltalk photo by Marcin Wichary
  22. 22. Ruby is general-purpose Objective-C mainly used in Mac/iOS app development
  23. 23. Ruby != Rails Objective-C != Cocoa Framework
  24. 24. they have something in common..
  25. 25. both Ruby and Objective-C are Object-Oriented
  26. 26. both Ruby and Objective-C are strongly typed language.
  27. 27. Ruby is a dynamic language Objective-C is a dynamic language
  28. 28. both Ruby and Objective-C are Dynamic Typing
  29. 29. Type checking.. - (void) makeSomeNoise:(id) sender { if ([sender isKindOfClass:[RobberDuck class]]) { RobberDuck* duck = (RobberDuck *) sender; [duck quack]; } }
  30. 30. or you can do this.. - (void) makeSomeNoise:(id) sender { if ([sender respondsToSelector:@selector(quack)]) { [sender quack]; } }
  31. 31. id
  32. 32. Objective-C is superset of C
  33. 33. Objective-C is still C
  34. 34. NSString, NSArray, NSNumber…
  35. 35. NextSTEP
  36. 36. CF… = Core Foundation CG… = Core Graphic CL… = Core Location CA… = Core Animation UI… = User Interface
  37. 37. OOP
  38. 38. everything in Ruby is an object…
  39. 39. and almost everything in Objective-C is an objects..
  40. 40. there’re still some primitive data types in Objective-C
  41. 41. object model class Animal end ! class Dog < Animal end
  42. 42. object model dog = Dog.new ! puts "class of dog is #{dog.class}” # Dog puts "superclass of dog is #{dog.class.superclass}” # Animal puts "super superclass of dog is #{dog.class.superclass.superclass}” # Object puts "super super superclass of dog is #{dog.class.superclass.superclass.superclass}” # BasicObject ! puts puts puts puts "class "class "class "class of Dog is #{Dog.class}” class of Dog is #{Dog.class.class}” of Animal is #{Animal.class}” of Object is #{Object.class}” # # # # Class Class Class Class
  43. 43. object model @interface Animal : NSObject @end ! @implementation Animal @end ! @interface Dog : Animal @end ! @implementation Dog @end
  44. 44. object model Dog* dog = [[Dog alloc] init]; ! NSLog(@"class of dog is %@", [dog class]); # Dog ! NSLog(@"superclass of dog is %@", [dog superclass]); # Animal ! NSLog(@"super superclass of dog is %@", [[dog superclass] superclass]); # NSObject ! NSLog(@"super super superclass of dog is %@", [[[dog superclass] superclass] superclass]); # null
  45. 45. Object Model reference: http://goo.gl/wYL6gT
  46. 46. method & message
  47. 47. method definition def say_hello(someone, message) puts "Hello #{someone}, #{message}" end - (void) sayHello:(id)someOne withMessage:(NSString *)message { NSLog(@"Hello %@, %@", someOne, message); }
  48. 48. sending message dog.walk() [dog walk]; # or you can omit the parentheses
  49. 49. sending message fox.say_something "hi, Ruby" [fox saySomething:@"hi, Ruby"]; # what does the fox say?
  50. 50. sending message puts 1 + 2 puts 1.+(2) puts 1.send(:+, 2)
  51. 51. sending message class Bank def save(money) puts "you just saved #{money} dollars" end end ! bank = Bank.new bank.save 20 bank.send(:save, 20) # you just saved 20 dollars # you just saved 20 dollars
  52. 52. sending message @interface Bank : NSObject - (void) save:(NSNumber *) money; @end ! @implementation Bank - (void)save:(NSNumber *)money { NSLog(@"you just saved %@ dollars", money); } @end ! Bank* bank = [[Bank alloc] init]; [bank save:@20]; [bank performSelector:@selector(save:) withObject:@20];
  53. 53. block
  54. 54. block p1 = Proc.new { puts "Hello, Proc Block" } p1.call ! p2 = lambda { puts "Hello, Lambda Block" } p2.call
  55. 55. block ^{ };
  56. 56. block typedef void (^MyBlock)(void); int age = 18; MyBlock theBlock = ^{ NSLog(@"Hello, Objective-C Block, your age = %d", age); }; ! theBlock(); # Hello, Objective-C Block, your age = 18 ! age = 38; theBlock(); # guess what’s the age?
  57. 57. block 3.times { |i| puts i } NSArray* list = @[@1, @2, @3]; [list enumerateObjectsUsingBlock:^(NSNumber* num, NSUInteger idx, BOOL *stop) { NSLog(@"%@", num); }];
  58. 58. iteration
  59. 59. iteration list = [1, 2, 3, 4, 5] ! sum = 0 ! list.each { |num| sum += num } ! puts "sum = #{sum}"
  60. 60. iteration NSArray* list = @[@1, @2, @3, @4, @5]; ! __block int sum = 0; ! [list enumerateObjectsUsingBlock:^(NSNumber* num, NSUInteger idx, BOOL *stop) { sum += [num intValue]; }]; ! NSLog(@"sum = %d", sum);
  61. 61. iteration class Fox def say puts "what does the fox say?" end end ! fox1 = Fox.new fox2 = Fox.new fox3 = Fox.new foxes = [fox1, fox2, fox3] ! foxes.map { |fox| fox.say } # what does the fox say?
  62. 62. iteration @interface Fox : NSObject - (void) say; @end ! @implementation Fox - (void) say { NSLog(@"what does the fox say?!"); } @end
  63. 63. iteration Fox* fox1 = [[Fox alloc] init]; Fox* fox2 = [[Fox alloc] init]; Fox* fox3 = [[Fox alloc] init]; ! NSArray* foxes = @[fox1, fox2, fox3]; ! [foxes makeObjectsPerformSelector:@selector(say)];
  64. 64. add methods at runtime
  65. 65. Open class class String def is_awesome? return true if self == "Ruby Tuesday" end end ! puts "Ruby Tuesday".is_awesome?
  66. 66. Category @interface NSString(RubyTuesday) - (BOOL) isAwesome; @end ! @implementation NSString(RubyTuesday) - (BOOL) isAwesome { if ([self isEqualToString:@"Ruby Tuesday"]){ return YES; } return NO; } @end
  67. 67. Category NSString* meetup = @"Ruby Tuesday"; if ([meetup isAwesome]) { NSLog(@"AWESOME!"); }
  68. 68. <objc/runtime.h> Working with Classes class_getName class_getSuperclass class_getInstanceVariable class_getClassVariable class_addIvar class_copyIvarList class_addMethod class_getInstanceMethod class_getClassMethod class_replaceMethod class_respondsToSelector .. reference: http://goo.gl/BEikIM
  69. 69. <objc/runtime.h> Working with Instances object_copy object_dispose object_setInstanceVariable object_getInstanceVariable object_getIndexedIvars object_getIvar object_setIvar object_getClassName object_getClass object_setClass .. reference: http://goo.gl/BEikIM
  70. 70. reflection - (BOOL) (BOOL) (BOOL) (BOOL) .. isKindOfClass:(Class) aClass isMemberOfClass:(Class) aClass respondsToSelector:(SEL) aSelector conformsToProtocol:(Protocol *) aProtocol reference: http://goo.gl/fgmJcg
  71. 71. ecosystem
  72. 72. open source projects on Github Ruby : 76,574 Objective-C : 22,959
  73. 73. Ruby : bundler source 'https://rubygems.org' ! gem 'rails', '3.2.8' gem 'mysql2' ! group :assets do gem 'sass-rails', '~> 3.2.3' gem "bootstrap-sass" end ! gem gem gem gem "kaminari" "simple_form" "carrierwave" 'unicorn'
  74. 74. Objective-C : cocoapods platform :ios, '6.0' ! pod pod pod pod pod pod 'Facebook-iOS-SDK', '~> 3.5.1’ 'JSONKit', '~> 1.5pre' 'MagicalRecord', '~> 2.0.7’ 'SSKeychain', '~> 0.1.4’ 'TestFlightSDK', '~> 1.1' 'SMCalloutView', '~> 1.1.2' ! target :UnitTests do link_with 'UnitTests' pod 'OCMock', '~> 2.0.1' pod 'OCHamcrest', '~> 1.9' end
  75. 75. IMHO
  76. 76. Objective-C is not really hard to learn…
  77. 77. the actual difficult part in iOS app development is Cocoa Framework
  78. 78. Objective-C would be almost useless without Cocoa Framework
  79. 79. Ruby without Rails?!
  80. 80. design patterns MVC observer notifications singleton delegation command composite target-action proxy
  81. 81. C
  82. 82. what else..
  83. 83. photoed by JD Hancock
  84. 84. Flash Display Hierarchy reference: http://goo.gl/2mzyMY
  85. 85. UIKit and AppKit framework Hierarchy reference: http://goo.gl/xhS7m7
  86. 86. Views reference: http://goo.gl/xhS7m7
  87. 87. Views reference: http://goo.gl/xhS7m7
  88. 88. and I read Ruby source code..
  89. 89. my iOS app dev experience = Ruby + C + Flash/AS3 = +
  90. 90. Contacts ⾼高⾒見⻯⿓龍 Website http://www.eddie.com.tw Blog http://blog.eddie.com.tw Plurk http://www.plurk.com/aquarianboy Facebook http://www.facebook.com/eddiekao Google Plus http://www.eddie.com.tw/+ Twitter https://twitter.com/#!/eddiekao Email eddie@digik.com.tw Mobile +886-928-617-687 photo by Eddie

×