Talks
Learning Ruby Made Me A Better Objective C Programmer
Summarized using AI

Learning Ruby Made Me A Better Objective C Programmer

by Michael Harper

In the video titled "Learning Ruby Made Me A Better Objective C Programmer," Michael Harper shares his journey of transitioning from traditional programming languages like C++ and Java to incorporating Ruby on Rails and eventually enhancing his understanding of Objective-C. The session reflects on how adopting Ruby influenced his approach to Objective-C, revealing commonalities and unique features between the two languages.

Key Points Discussed:

- Background and Evolution: Michael’s introduction details his programming journey beginning in the early 90s, where he initially worked with languages such as FORTRAN and C++, eventually encountering Objective-C during a project on NeXT.

- Initial Impressions of Objective-C: He expresses his early skepticism regarding Objective-C, describing it as a ‘goofy’ language compared to the more established C++ and Java, especially due to its unique syntax and method calling conventions.

- Discovering Ruby: After years of working with Java, he discovered Ruby on Rails via a podcast, which he found much more enjoyable compared to Java EE, leading him to work on multiple Ruby projects.

- Commonalities Between Ruby and Objective-C:

- Dynamic Method Handling: Ruby allows modification of classes and methods at runtime, a concept he initially struggled with in Objective-C, but grew to appreciate.

- Sending Methods to Objects: In Ruby, it is possible to send a method to an object, which contrasts with the traditional method calling in Objective-C that he previously found cumbersome.

- Blocks of Code: He discusses the usefulness of blocks in both languages, noting how Ruby's syntax for passing blocks is more straightforward compared to Objective-C.

- Runtime Method Addition: Michael demonstrates adding methods at runtime in Ruby and compares it with how Objective-C implements similar functionality through categories, emphasizing the practicality and flexibility Ruby provides.

- Takeaways:

- Michael reflects on the insights gained from Ruby that improved his understanding of Objective-C’s design principles.

- He concludes that both languages offer unique features that can enhance the programming experience, particularly for mobile development.

Overall, the session underscores the importance of exploring different programming paradigms and how they can lead to a richer understanding of familiar languages. The talk ends with positive remarks about building a community around these experiences and encourages testing and experimentation in coding practices.

00:00:06.080 So, yeah, I'm Michael Harper. I am a Sun River resident. Sun River is down or up the road depending upon how you define down or up, about 15 miles.
00:00:21.439 So, everybody, if you are working on more than one project right now that really requires your attention and you probably shouldn't be here anyway, raise your hand.
00:00:32.640 Okay, now keeping your hands up, how many of you also had to remove eight inches of snow from your driveway this morning? Yeah, exactly.
00:00:58.719 Okay, so I am, like I said, I live up in Sun River and it turns out I'm 50% of the Sun River iOS developer community. There's another gentleman by the name of Dick Lucky and we found each other on Twitter because I think we're probably the only two people in Sun River on Twitter.
00:01:12.479 Anyway, really, about 2500 people live out there. Anyway, so this is awkward.
00:01:29.680 Okay, I'm going to move this so it's a little bit less awkward. I appreciate the guys who came in front of me and set the bar incredibly high for keynote presentations. I'm assuming that they're even here.
00:01:47.439 I'm pleased to see all the MacBooks in the audience. There was a time when that would have been... what.
00:02:03.119 So, just briefly, my road to Objective-C. Why am I talking to you about Objective-C? Good question. Okay, mobile developers, how many mobile developers do we have out there?
00:02:15.599 Okay, so what's interesting is where I got started, right? Does anyone remember a language called FORTRAN? Oh, yeah. Does anybody know about something called a Prime mini-computer? One? Alright, yeah, that’s about how important that is in my life.
00:02:41.599 My experience actually goes back before this 1980 starting point, but that was about yellow spools of paper and keywords on an 110 baud, not really applicable to what's going on today.
00:02:58.879 But what I did learn when I was at Cal Poly in the 80s was I took an elective class called C.
00:03:08.080 Then from there, after I graduated, I went to work in San Francisco at an investment banking company and was doing development on Macs that actually weren't all-in-one.
00:03:20.480 Right? They actually had a separate keyboard, monitor, and base, and they had color. It was exciting! So, I taught myself C++ kind of on the C continuum here.
00:03:37.519 Then I encountered this goofy language on a project that I started working on Next, and this goofy language is called Objective-C.
00:03:55.599 So, you know, going from C to C++, it's a good thing that I taught myself C++.
00:04:09.680 I ended up working on this NextStep project in the mid-90s and encountered Objective-C.
00:04:23.680 Then about 10 years later, I ended up porting that same project from Next to run on Mac OS X, which I thought was really cool.
00:04:35.759 I was able to see the evolution of what it started on Next, but I still thought Objective-C was relatively goofy.
00:04:47.759 Then we get the iPhone SDK and we're doing mobile development on iPhones and iPod Touch with Objective-C.
00:04:59.680 I couldn't resist the allure of writing iPhone apps, just too sexy and too cool to be able to pull your phone out and go, 'I made this!'
00:05:13.759 So, I started doing Objective-C programming. I don’t see any Ruby or anything else on there.
00:05:25.440 Oh, right, I must have filled in with something else. Oh yeah, Java. Yeah, so in the mid-90s, let's see, that would be right about when my daughter was being born.
00:05:40.000 I already had a son living in Marin. Yeah, I had to do something to make some dough; that was Java Enterprise development.
00:05:56.479 So, I was working for Sun and Stanford and various other companies on this crazy platform called Tenga.
00:06:05.360 Does anybody remember Tenga, the precursor to WebLogic, which is now part of Oracle?
00:06:18.720 Yeah, so Oracle is like, there goes Sun, there goes WebLogic, there goes ATG. Does anybody remember ATG Dynamo?
00:06:30.880 Yeah, it's Oracle. It's like this black hole.
00:06:44.320 So, I did a lot of enterprise working jobs and I did that for quite a while.
00:06:59.360 On the Java Posse podcast, if you don't listen to that, you should because it's really funny and interesting.
00:07:12.960 A lot of Android stuff is on there as well.
00:07:24.080 Anyway, I heard about Ruby on Rails on the Java Posse podcast, and I thought, 'Why not check it out?'
00:07:36.560 Then I ended up being asked to work on a Rails project, and from there, ended up working on a number of Rails projects.
00:07:48.640 So, it's not a complete disconnect that I'm standing up here talking to you about Objective-C.
00:08:07.120 So, remember I was saying in the early 90s, I encountered this goofy Objective-C language.
00:08:22.560 You know, that's with the goofy method calling syntax you've got square brackets around everything.
00:08:38.720 You've got method names and parameter names that are a mile long.
00:08:50.080 What's with this method selector? I mean would everyone use a method selector?
00:09:02.080 I mean why wouldn't you just call the method on the object? Come on.
00:09:12.000 Also, remember, this is 20 years ago. I'm just out of college, I'm in my mid-20s, and I know everything!
00:09:30.720 So, you know, what is this class method, category, and protocol stuff? I don't need to know this.
00:09:46.000 I mean, really, seriously, why would you want to use this? We've got C++ with two plus plus rules.
00:10:00.080 But let me briefly point out that I worked on a C project a couple of years ago, which was definitely my last one ever.
00:10:14.560 I have sworn it off completely, and it ain't gonna ever happen again.
00:10:29.200 So comparatively speaking, when I found out about Ruby on Rails, I went to an advanced Rails studio taught by Chad Fowler.
00:10:40.560 Then I had the moment of zen with Frank, David about Ruby, which is that everything is a method calling an object.
00:10:56.160 Whoa! Everything! Whoa!
00:11:06.240 Coming from a statically typed world of the 80s and 90s with Java and C++, that’s like, whoa.
00:11:18.720 So, what were my initial thoughts about Ruby? Well, you know, of course, unicorns and rainbows.
00:11:27.760 Double rainbow! You can't really see it very well, but that is looking towards Lanai from the Maui office.
00:11:46.000 Right? So it was great. I was working on a project over the summer and I had planned this trip for a couple of weeks to Maui.
00:12:02.080 I just told them that I was working from the Maui office, which I was. I mean, seriously, I was!
00:12:17.680 But that was the view from the Maui office.
00:12:29.760 So initial Ruby thoughts: unicorns and rainbows.
00:12:45.919 I took this outside, the actual Maui office. Yes, they have a roof!
00:12:59.600 In fact, they had another condominium upstairs.
00:13:12.640 So, you know, way, way the two different lists here.
00:13:22.320 I’ve got Objective-C initial thoughts and I've got Ruby initial thoughts. Consider there's 15 years of experience, life experience separating the two.
00:13:39.360 But I mean, it really was a moment comparing my initial thoughts about the two.
00:13:54.080 As I was working through Ruby and also working on mobile at the same time, I realized there's stuff going on in Ruby that I do all the time.
00:14:08.640 But I couldn't do in Objective-C, and I didn't even clue into it until I started working with Ruby.
00:14:20.320 So let's talk about the commonalities, not the Karl Marx variety, but what the two have in common.
00:14:34.239 So there are a ton of things that... Sorry, okay, that's not correct. There are a number of common things between Ruby and Objective-C.
00:14:47.120 I'm going to talk about three of them that were important to me.
00:14:56.960 Okay, there are differences as well. One of them you compile things and one you don't; I get that.
00:15:09.440 But these are the three things that I found important to me that Ruby helped me to understand in Objective-C.
00:15:22.240 Okay, so one of them is the ability to modify classes and methods at runtime.
00:15:30.080 I did this yesterday in Ruby on a Rails project.
00:15:41.680 I've been working mostly in mobile for the last year, but I've been helping out in my copious free time.
00:15:51.120 Yeah, we all have copious free time, don't we? And we all work in it, don’t we?
00:16:09.440 So yesterday, I was actually using this capability in Ruby to add a method at runtime, and that's pretty cool.
00:16:23.360 Try that in C++. If you do, tell me how you did it.
00:16:32.760 You can send a method to an object. I phrase this carefully; I didn't say you could call a method on an object.
00:16:44.720 Because duh, you can call a method on an object, and we do that all day long.
00:16:57.919 But the concept of separating the method from the object is kind of an interesting twist.
00:17:09.680 Again, in C, we didn't have objects, right?
00:17:21.440 But in C++, you couldn't really have a method live without an object.
00:17:31.680 I mean, you had a state and you had behavior, those were all part of the class and part of the object.
00:17:42.639 So the concept of just saying, 'Oh, I've got this method. I'll wait until an object comes along, and then I will send the method to the object, and the object will respond'.
00:17:56.720 You know, I guess messaging is the equivalent of what I'm trying to say here is a cool concept.
00:18:06.320 And finally, of course, blocks of code. Blocks of code that aren't contained in a method; basically blocks of code that you can pass around in Ruby.
00:18:19.040 Blocks of code that you can write at runtime; that's really cool.
00:18:30.080 I did look up metaprogramming for Objective-C and got nowhere.
00:18:41.360 Realized well, that's because all your code has to be compiled.
00:18:53.120 So, if somebody can figure out how to do metaprogramming in Objective-C, tell me about it after the recording.
00:19:03.840 So let’s get to an example of this: adding a method at runtime to a class in Ruby.
00:19:15.600 Right? So this is what happens when I have this string.
00:19:25.840 In Ruby, I call the 'ferment' method on it, right? It says there's no 'ferment' method on the string.
00:19:39.680 So, I can solve this problem simply by opening up the class and defining the 'ferment' method.
00:19:51.520 Of course, it changes all instances of sugar alcohol.
00:19:59.760 Then when we run that at the bottom, you can see that it works.
00:20:10.159 So, what does the same kind of thing look like in Objective-C?
00:20:23.520 Well, there's the error, and it's a different kind of error than you get in the Ruby equivalent.
00:20:36.720 It's this thing I alluded to earlier called a compiler.
00:20:44.560 You can’t even compile this code because the 'ferment' method doesn’t exist.
00:20:55.280 So you have to create it in the right place.
00:21:05.600 So, at runtime... I have highlighted a bunch of code, so that doesn't really matter.
00:21:15.520 And I’ve also gone too far ahead, haven’t I?
00:21:27.760 Let’s rewind here. We're talking about adding a method at runtime in Objective-C.
00:21:41.599 What you end up doing is, in the interface definition for the class, you create this thing called a category.
00:21:52.360 That's what putting the parentheses and some name after the class there.
00:22:02.959 You see interface and NSString, and NSString being the string class in iOS and macOS, and that's why there's the NS.
00:22:16.959 Okay, raise your hand if you do that. Come on, yeah!
00:22:29.040 So I create this category, I add the format method that returns a string, and then I implement it.
00:22:42.320 So basically, the Objective-C equivalent of opening up a class and adding a method.
00:22:53.920 Then when I run it, I get the result that you want to see.
00:23:04.640 Okay, compile and run.
00:23:14.560 So when have I actually used this? I've used this particularly on a string to add base64 encoding.
00:23:24.080 It's a real natural thing; in fact, I'm pretty sure I stole it off the web somewhere.
00:23:35.360 But the ability to do that in Objective-C is great, and I never would have thought of that if I hadn't been exposed to Ruby.
00:23:43.680 They do it everywhere else, right? Just a little bit of active support. It’s pretty much that.
00:23:55.840 So, sending a method at runtime—this concept of separating the method itself from the object that it's going to be executed on—it was another 'oh wow, that's kind of cool' thing for me.
00:24:08.560 So again, going back to our fermenting a string, I didn’t get any love for changing sugar to alcohol in the comments section.
00:24:17.440 You guys already had a couple of beers, so you can see towards the bottom.
00:24:29.600 I’m sending an s.send and then I pass in the ferment symbol, right?
00:24:38.920 So, I’m sending that message to that object, and it’s the equivalent of calling the method.
00:24:49.440 I’m sure there are subtle differences, but that’s essentially what's going on for the purposes of my example.
00:24:58.720 How do you do the same thing in Objective-C? Well, you have this thing called the selector.
00:25:05.120 Right? This is the thing that I didn’t understand 20 years ago. Why would you create a method selector?
00:25:15.920 You see this in all caps—SEL—that's the type of variable that you can assign a selector to.
00:25:27.440 Then you can call on an object performSelector and then pass in the selector.
00:25:35.440 Now, going back to Michael 20 years ago, well, why wouldn’t you just call the method directly?
00:25:47.600 Let me tell you why: in iOS 10, there are certain methods that you need to always call on the main thread.
00:25:58.560 You may not be on the main thread for one reason or another.
00:26:08.720 So, you can actually call performSelector on the main thread.
00:26:19.120 Here, you would pass in the text that you want to actually set in the current time view.
00:26:28.320 This is from a boxing timer app that I've used for about four years as an example to continue to learn.
00:26:41.200 Here’s another one: you're going to schedule a timer with a time interval of one second.
00:26:54.400 The target is self; in this case, it's a model.
00:27:04.000 Then here’s the selector that I call.
00:27:14.080 So two real good instances in iOS where you need to have that method separated from the object.
00:27:27.360 Kind of cool! Also, there’s this class called NSInvocation.
00:27:40.960 I looked at that because I’ve never used it, but I took a look because it is apparently the blanket.
00:27:52.080 Totally flexible and abundantly complex way of doing the separation of target, method, and parameters.
00:28:04.800 The amount of code required to do something as simple as one of these two is pretty impressive.
00:28:16.640 But that's kind of the pure way of calling a method on an object, if you really want to get adventurous.
00:28:26.320 So the last bit I have for you here is blocks.
00:28:42.080 This is a fairly contrived example, but I added a yield to the 'ferment' method that I used in the example.
00:28:54.160 That way, if there is a block that's passed into the ferment call, it will yield to that to get the value that we're going to replace sugar with in this string.
00:29:08.480 Right? So I have two calls to ferment: one without any block and one that does simple string concatenation.
00:29:21.360 Do you have any beer? Beer, beer?
00:29:31.039 So that's a simple example of blocks in Ruby, and you all know that we use blocks like crazy in Ruby.
00:29:42.160 So how does that relate to Objective-C?
00:29:51.280 Well, here's the equivalent in Objective-C.
00:30:02.240 You have to write a little more obtuse code, basically.
00:30:13.040 Yeah, but you can do it; it’s really not that bad.
00:30:24.160 You know, just figuring out what that definition of the block is the hardest part.
00:30:36.799 Always involves a caret.
00:30:46.239 But if you look down near the bottom, you can see I'm making the ferment call with nil.
00:30:56.160 That’s kind of the equivalent of not passing in a block in Ruby.
00:31:05.760 Then I’m passing in a block that returns beer times three.
00:31:21.040 So that’s the equivalent of the beer times three in Ruby.
00:31:33.120 Again, this is a compact example of passing blocks around, but it makes your life easier.
00:31:47.840 Specifically for me in dealing with animations in iOS.
00:31:58.040 Because you can pass in, for example, 'when the animation is done, run this block of code.'
00:32:10.080 I don’t have to create another class or delegate method to get called afterwards.
00:32:21.199 Now I just pass in this block of code. That's great.
00:32:31.360 Does anyone notice anything about this?
00:32:40.240 Is there a times method on the string?
00:32:48.160 No, but I needed one. So, you know, I did that thing that I was talking about earlier.
00:32:57.600 Where I added a method at runtime so that I could have a times method.
00:33:11.040 I'm not saying that Objective-C isn't great.
00:33:19.920 We can do this stuff in Objective-C just as easily as we can in Ruby.
00:33:30.080 I'm saying it's neat; we can actually do this.
00:33:40.240 Okay, now, I've got to get this stuff in place.
00:33:47.360 Alright, so just a real quick example here of making array elements do stuff.
00:33:58.560 I was talking about this a little bit earlier, about how nice the syntax is in Ruby.
00:34:09.440 They’ve given us some helper methods for things like NSArray so that we can make objects performSelector.
00:34:23.760 Remember that thing I was talking about having the method separate from the actual object?
00:34:35.760 Yeah, cool!
00:34:45.760 You can also do the same thing with blocks.
00:34:56.239 You can test some code and have the object passed in each element in the array passed into the block.
00:35:07.040 So, what have I learned other than, man, this is a really cool room.
00:35:19.040 We had a buddy's 50th birthday party next door, so I knew about that.
00:35:31.360 I didn't know about this room, but I think it's really cool.
00:35:44.080 Anyway, I've learned that I love Ruby. I mean, I just really do.
00:35:58.720 I've been doing this for a long time, and I remember when Java came along, and I thought, 'This is great!'
00:36:10.720 It's taking all the rough edges away from C++. I don't have to worry about memory management anymore.
00:36:22.440 Java, thank you very much! Then I spent seven or eight years doing enterprise development.
00:36:35.760 It's like, okay, well, we've replaced the sharp edges of C++ with all this XML configuration.
00:36:46.560 Right? The whole thing called accidental violence.
00:36:57.680 And then Ruby comes along; it's like, 'Oh, thank you, thank you!'
00:37:09.840 And then what do I end up doing? I end up doing Android development.
00:37:20.560 It’s not that I hate Java, it's that I hate Java enterprise.
00:37:30.560 Nothing wrong with me; I really love Ruby.
00:37:39.600 What else have I learned? I learned that I understand Objective-C a whole lot better than I did 20 years ago.
00:37:51.680 I don't know, maybe it’s because of the passage of time.
00:38:02.160 Maybe it’s because I realized that I don't know everything anymore.
00:38:12.880 That probably took a while for me to understand.
00:38:20.720 And wouldn’t it be awesome if Ruby were sanctioned for iOS development?
00:38:30.720 Mac Ruby!
00:38:43.040 They're starting to do iOS better with Mac Ruby.
00:38:54.720 Cool! Can you circle back with me and tell me more about that?
00:39:06.720 Okay.
00:39:14.560 So, Ruby, you know, I love about it that it’s so expressive and efficient.
00:39:25.280 And then the moment of zen with Dave and metaprogramming.
00:39:34.640 You know, double-edged sword. Certainly, but isn’t it cool?
00:39:43.440 And Objective-C, you know, my problem, my initial experience with Objective-C was really tainted.
00:39:53.840 By the fact that I came from such a statically typed world.
00:40:03.840 Right? I mean, that’s all I was taught in college.
00:40:13.760 I have this crazy computer science degree, and we learned about lots of particularly typed stuff.
00:40:27.040 You know, only dynamic stuff happened in this one class where we wrote one program in 12 different languages.
00:40:40.480 So, I think that Objective-C was tainted for me by this heavily strictly typed history.
00:40:53.040 And by going to Ruby and the freedom that I was allowed there, I really started to understand Objective-C.
00:41:05.520 Back to it today, to some degree, shame with self-promotion.
00:41:17.440 There seems to be some of that in the previous presentation, so I'll go ahead and do that myself.
00:41:28.800 I told you that I live in Sun River, and if you haven't been there, it is a destination resort area.
00:41:41.760 It has 35 miles of bike paths that lots of tourists get on their bikes.
00:41:53.040 They take their kids in the trailer and head off, and they don’t know where they are.
00:42:06.240 There are, I think we’re up to 11 traffic circles.
00:42:16.960 Interestingly, in the upper right-hand corner, there was supposed to be another traffic circle that is missing.
00:42:27.600 Actually, there is no circle eight in Sun River; it goes from seven straight to nine.
00:42:39.120 The joke is that tourists are being particularly special, as tourists are want to be.
00:42:53.760 As in, 'Make me a latte, and make it a good one,' we send them to circle eight.
00:43:07.360 And tell them to take the first one.
00:43:17.440 Anyway, go check that out at dot-dash-circle-link.net.
00:43:29.760 And here’s the shameless self-promotion: here’s where you can find me on the interwebs.
00:43:41.680 I’m on Twitter; that’s because I have a 40-year-old BMW Bavaria.
00:43:52.080 No, it’s not a 2002; it’s got two more doors.
00:44:02.080 I’m part of a group of people that keeps those cars alive, so that is a Germanization of my.
00:44:14.240 Statelessstandalonecode.com; that’s me!
00:44:27.360 On GitHub, I'm mharper; sorry it's kind of weak.
00:44:40.960 With that, I am going to ask somebody to go get me a...
00:44:54.040 What do you want? IPA? And my final admonition to you is to go write a test.
00:45:06.640 Thank you to Matt and Mark and Josh and Mike and Miss somebody else—the guy who’s getting me here.
00:45:20.800 Thank you for having me, guys. I appreciate it.
00:45:35.760 Thanks, Chris, for coming out all the way from BC. I appreciate that.
00:45:45.520 I hope you learned something.
00:46:00.800 Any questions? Sure, does anybody have a question for me? If I don’t know the answer, I’ll make it up.
00:46:14.960 That’s a good question! Make it up! The 80s was where I discovered Genesis.
00:46:27.920 Constant doesn’t matter. We pretty much had parity between the US dollar and the British pound.
00:46:42.560 Which was great because I was in abundance in three months.
00:46:54.880 But was it cooler?
Explore all talks recorded at Ruby on Ales 2012
+4