Ruby on Ales 2011

Summarized using AI

GUI Programming with Mac Ruby

Erik Michaels-Ober • April 07, 2011 • Earth

The video titled "GUI Programming with Mac Ruby" features Erik Michaels-Ober discussing the capabilities and advantages of using MacRuby for GUI programming on macOS. MacRuby is presented as a powerful tool that allows developers familiar with Ruby to create GUI applications, bridging Ruby's elegance and the performance of Objective-C.

Key Points Discussed:
- Who is Erik Michaels-Ober:
- An open-source software developer and fellow at Code for America.
- Currently developing a MacRuby application called Hubcap, a social GitHub client.

  • Introduction to MacRuby:

    • MacRuby is a complete implementation of Ruby 1.9, running on macOS core technologies instead of a Java Virtual Machine (JVM).
    • It utilizes the Objective-C runtime and offers features such as a superior garbage collector and Just-In-Time (JIT) compilation.
  • Comparison with Other Languages:

    • Unlike AppleScript, which is considered limited, MacRuby provides a full programming experience with object orientation, blocks, and advanced features like regular expressions.
    • It also aims to provide better performance compared to Objective-C due to its integration with LLVM.
  • Advantages of Using MacRuby:

    • Allows Ruby developers to use existing Ruby libraries and leverage Ruby 1.9 features.
    • Eliminates the global interpreter lock issue, enhancing performance in multi-threaded scenarios.
  • GUI Programming with MacRuby:

    • Two main uses: controlling existing GUI applications and creating new GUI applications.
    • MacRuby applications maintain the native look and feel of macOS applications, in contrast to Java apps.
  • Future of MacRuby:

    • Version 0.10 was released recently, and version 1.0 is on the horizon.
    • Acknowledged the advantage of MacRuby being potentially included as a public framework in macOS, reducing application sizes significantly.
  • Resources Available:

    • Recommendations include a book co-authored by Matt Aimonetti titled 'Ruby: The Definitive Guide' and another resource 'MacRuby in Action'.

Conclusion:
MacRuby emerges as a compelling framework for Ruby developers to build native GUI applications on macOS due to its integration with existing Ruby libraries and the advantages of Objective-C performance. With the growing opportunities in the Mac App Store and the continuous development of MacRuby, it represents a viable path for Ruby enthusiasts looking to enter the realm of desktop application development.

GUI Programming with Mac Ruby
Erik Michaels-Ober • April 07, 2011 • Earth

Ruby is a great language for building web applications and manipulating text but it's also the best language to interact with your favorite Mac apps or even build a new app to sell on the Mac App Store. I will demonstrate how to build a simple GUI app in MacRuby and discuss the benefits and drawbacks of doing so versus using RubyCocoa, Objective-C, Objective-J, or Java. I will also discuss the roadmap for MacRuby 1.0, scheduled to be released later this year.

Help us caption & translate this video!

http://amara.org/v/GZCe/

Ruby on Ales 2011

00:00:22.080 Next up is Erik Michaels-Ober. He is the second most handsome Ruby programmer in the world.
00:00:28.560 He is going to talk to us about GUI programming with MacRuby.
00:00:33.760 GUI programming is a sticky situation, so I hope that you provide some clarity.
00:00:40.160 And now, Erik!
00:00:51.680 GUI programming with MacRuby. Hey everybody.
00:00:58.640 So, my mother is a journalist, and when I was very young, she instilled in me this idea that there are only a handful of words in the English language that you can use to ask a question. If you answer all those, then there are no questions left to be answered. I’m basically going to run through all these points on MacRuby, and if there are any questions at the end, I’ll just take that as a personal affront to my mother. These are the only questions.
00:01:34.240 So first, who am I? This is my given name, but you might know me on the internet as sf_eric—Twitter, GitHub, Gmail, IRC, etc. I’m currently in the middle of a one-year fellowship at a non-profit organization called Code for America. Yeah, Code for America! We're working to make government more efficient, transparent, and open. It’s basically the best thing ever. I get paid to write open-source software all day, using Ruby. You can too!
00:01:48.079 We just opened applications for our 2012 fellowship, so if anyone's interested in that, please come up and talk to me at some point during the conference. I can give you more information and even a t-shirt. We have great t-shirts!
00:02:29.760 I also write a lot of open-source code, not necessarily related to fixing government. You probably use at least one line of code that I’ve written at some point—so you’re welcome.
00:02:46.879 And just to be clear, I’m not a MacRuby expert. I’ve never submitted a patch to MacRuby, and I’m not on the core team. However, I am currently developing a MacRuby app called Hubcap.
00:02:57.840 Basically, it’s a social GitHub client, so think of it as a third-party Twitter desktop client, but it's just for your friends who write code so you can keep tabs on them and see what code they’re writing.
00:03:11.200 These are the real heroes for MacRuby. You should follow them. Also, the last thing on the list is the MacRuby Develop mailing list, which is really active and a great place to start if you’re interested in diving in and getting questions answered.
00:03:37.599 Now, the what: what is MacRuby? MacRuby is a few things, but first and foremost, it’s a complete implementation of Ruby 1.9. You can think of it sort of like JRuby, except instead of running on the Java Virtual Machine, it runs right on top of macOS core technologies.
00:03:45.040 It runs on the Objective-C runtime and uses the Objective-C garbage collector, which turns out to be vastly superior to the Ruby garbage collector, even in Ruby 1.9, primarily because it’s multi-threaded. Garbage collection can happen on a separate background thread, so it never interferes with your main thread.
00:03:57.200 There is no global interpreter lock in Objective-C, so it has great performance just like JRuby. MacRuby also uses LLVM, which is more like a compiler infrastructure than a virtual machine.
00:04:06.000 It compiles code into Objective-C bytecode through a Just-In-Time (JIT) compilation process. You can use IRB the same way you do now, and it will work great. There’s also ahead-of-time compilation, so you can bundle your applications and ship them out as packages.
00:04:24.800 All Ruby apps should run in the MacRuby interpreter. Not everything you write for MacRuby will run in every other Ruby interpreter.
00:04:36.400 I just want to give a really quick minimalistic demo. It’s not getting into any of the GUI features at all, but it’s just proving that MacRuby is, as I say, a real Ruby.
00:05:47.039 Let me see if I can get this to work. Can you all see that?
00:05:52.479 Let me see if I can make that bigger. Let’s try another one.
00:06:22.319 Can everyone see this? Those are basically side by side, similar size, and big enough font.
00:07:03.039 So I’m using RVM, and on this side is Ruby 1.9.2, and on this side, I’ll make it MacRuby, which is the latest version of Ruby 0.10.
00:07:10.720 I’m just going to open up IRB on both of these. Looks about the same.
00:07:24.319 If you have a string in Ruby 1.9 and you have a string in MacRuby, they are totally the same.
00:07:30.639 So great, if I ask for that string’s class in Ruby 1.9, anyone want to venture a guess as to what it’s going to say? So that actually changed, I think, between MacRuby 0.5 and 0.6. It’s actually going to say String. I just blew your mind, I know.
00:07:47.759 But what do you think is going to happen if I say MacRuby.class over here? The superclass of that in Ruby is Object. And for this side, some of you said NSObject or NSString; you’re both wrong. correct. It’s an NSMutableString.
00:08:02.160 So the superclass of a Ruby string in that Ruby is NSMutableString. Anyone know if I super one more time what I’m going to get? So you get an NSString. If I go up one more time, great.
00:09:00.399 So everything is an object, or everything is an NSObject as the case may be. Basically, anything you can do in Ruby, you can do in MacRuby.
00:09:06.160 Let’s say I want to capitalize that string, so I can say food.upcase. Good. I can do the same thing over here.
00:09:19.120 So if I want to see what methods are on String in Ruby 1.9, I need to say foo.methods. You're going to see almost exactly the same list. It’s not 100% identical, but it’s basically the same list. If I sort them, you can actually do a one-to-one comparison. There seems to be a few more methods on the MacRuby side, but they’re basically the same. What’s really cool in MacRuby is you can do this: I can say methods true true.
00:09:55.760 So the first true indicates whether to include all the methods in the superclasses as well, and by default, that’s true. You’re going to get the same number of methods, right? But if I say false, then nothing. If I say true here, I get the same methods.
00:10:12.960 But if I say true true, and if I give it the second true, you can't do this on this side. It will say 'wrong number of arguments.' But on this side, it’ll give me all of the Ruby methods that you can call on String, and also all of the Objective-C methods that you can call on an NSString.
00:10:26.480 If I say methods.size for foo, it’s going to give me 168. If I say methods true true, size is going to give me a lot more. Okay, let’s take a look at some of those.
00:10:39.680 So they’re these ugly Objective-C things, but what’s cool is that you can actually call them. Just like I can say foo.upcase, I can say foo.uppercaseString.
00:10:47.360 It does the same thing. It totally works! You can invoke methods in the normal Ruby method invocation way. That might not seem awesome, but it's awesome because you have all the power of Ruby. You can call any Ruby method, and you can also call any Objective-C method and mix and match. You can have classes that inherit from NSObject or NSMutableString or anything like that and extend those using Ruby. I think that’s really cool.
00:12:00.720 But that’s not really the fun stuff. The fun stuff is GUI programming, which is what this talk is all about.
00:12:06.720 What’s the keyboard shortcut to just...Oh, there we go. Is that playing? Okay, great!
00:12:16.720 This is probably all complicated by the fact that I’m running the Lion Developer Preview, which I’m not allowed to say anything about. But you can take from that what you will.
00:12:33.920 Great! So MacRuby, in addition to being a complete implementation of Ruby 1.9, also lets you do two other things.
00:12:39.120 One is that you can write scripts to control existing GUI applications. If you want to write a script that updates your status in iChat or does anything really, you can use it in the same way you would use Automator on the Mac but with the full power of a real programming language like Ruby.
00:13:01.440 Then you can also create new GUI applications. That seems like an exciting thing you might want to do, but why should you care? There’s already a language existing to script Mac applications, called AppleScript, and there’s already a language for creating new Mac desktop applications called Objective-C.
00:13:18.480 So what do you need Ruby for?
00:13:28.320 I’m going to tell you a secret. I need everyone just to lean in because I can’t say it on the mic. Listen, here’s the secret: Apple’s not very good at making programming languages.
00:13:39.680 Who is good at making programming languages? I think this guy is. So why do I say that? What’s not so good about AppleScript? Well, AppleScript is really a toy language. It’s not object-oriented. It doesn’t have blocks and lacks most of the features you’d want in a real programming language, like regular expressions, which you just take for granted in Ruby.
00:14:01.520 So I think it’s fair to say that given equal availability, if you had to choose fairly between AppleScript and writing an application in Ruby with MacRuby, you would choose MacRuby.
00:14:09.840 MacRuby has a great scripting bridge framework that allows you to easily hook into any application and write scripts to your heart’s desire.
00:14:19.520 Maybe you’re thinking, you know, I don’t really care about writing scripts for other people’s GUI applications. I want to create my own. For that, Apple has a language for you called Objective-C.
00:14:36.480 Objective-C is not a toy language. You can tell that by looking at the name and seeing the word C in it. So you say, "Okay, this must be a robust and powerful language because it’s C," and that’s mostly true. But I would make the case that it’s hampered by being constrained by the syntax of C.
00:14:49.440 What’s nice is that because MacRuby uses LLVM and compiles your applications down into Objective-C bytecode, you get the performance benefits of Objective-C without having to write C code.
00:15:05.440 Let me make the case that Ruby is actually better than Objective-C; it's just a nicer language.
00:15:13.600 Let’s look at method invocation. This is how you invoke a method in Ruby, which should look familiar to everyone in the room. Can anyone suggest how you can make this simpler?
00:15:25.440 So you could get rid of the dots. But how would you know that you put a colon after a method?
00:15:35.520 You could make it simpler by getting rid of the dot, but you still need some syntax to differentiate parameters.
00:15:41.760 This is how you do method invocation in Objective-C. I think this is kind of gross, but maybe it’s not so bad. Can anyone think of a way to make this simpler?
00:15:54.720 You could use parentheses and get rid of the dot. You could lose the semicolon too.
00:16:02.560 In Objective-C, because it is bound by C syntax, everything must be syntactically valid C.
00:16:08.720 This is Ruby, how you initialize and sign an array. This should look pretty simple, right? Hard to imagine too much simpler.
00:16:19.520 So this is how you do exactly the same in Objective-C.
00:16:28.640 If you’re okay with doing this, then definitely Objective-C is the language for you. If you’re a Ruby developer, like I was, it seems out of reach.
00:16:36.580 For me personally, the thing that attracted me to MacRuby was being able to code in Ruby and generate something beautiful like a desktop application.
00:16:50.360 RubyCocoa was Apple’s first attempt at supporting Ruby. They built this Ruby to Objective-C bridge.
00:17:01.760 But according to Apple, RubyCocoa is not viable anymore because they’re investing a lot into MacRuby development, paying developers full time.
00:17:15.440 There are many ways to write a client for the Mac; you don’t have to use Objective-C at all.
00:17:24.640 I’m not going to spend a lot of time convincing you not to write Java apps for desktop clients. You probably already know why.
00:17:36.640 The promise of Java is that you write the code once and it runs anywhere. But that always seemed a bit wrong about Java client apps!
00:17:48.320 You don't get that problem with MacRuby. MacRuby apps look and feel exactly the same as apps written in Objective-C because they are native Cocoa applications.
00:18:01.920 So, do not write Java apps. Java wasn't the solution to write once, run anywhere; the solution is the web.
00:18:15.760 Maybe you're thinking, okay, I could use one of these fancy new JavaScript frameworks for doing rich apps. I'm not going to tell you not to write web apps; they are terrific.
00:18:27.440 Not all apps have to be web apps, and certain functionalities are just not possible with web apps.
00:18:35.440 For example, if I have a Bluetooth device and want to write a Mac app that communicates with it, it’s tough to do that in a web app.
00:18:47.120 So, this is sort of the what. Moving on to the when. This is exciting! Just yesterday, MacRuby 0.10 was released.
00:19:05.600 It’s a great release! It’s stable! 1.0 is coming really soon. Features have basically been frozen, and the core team is polishing up the APIs.
00:19:16.920 They’re going to do a couple of small releases just to improve stability and performance leading up to the target of 1.0.
00:19:26.080 That’s awesome! MacRuby, whatever version it may be at the time, will hopefully be included in Mac OS 10. It’s already included in the developer previews.
00:19:39.520 The only problem is that currently, it’s a private framework.
00:19:48.960 If you want to use MacRuby in your app, you have to bundle all of MacRuby into your app. You can’t just assume it’s there in the operating system.
00:20:02.480 If Lion becomes a public framework, you would be able to assume it was there, so that would be great.
00:20:14.080 If you have to bundle MacRuby into your application, you start off with a 50 Megabyte footprint of your application before writing any code just to include Ruby.
00:20:28.320 If it were a public framework and you could assume it was there, then every MacRuby application would be 50 Megabytes smaller!
00:20:44.080 So it seems worthwhile to advocate for that.
00:20:54.160 So why? I think I already gave you some reasons from a programmer's perspective.
00:21:03.840 From a programming perspective, I hope I’ve convinced you that the syntax is nice, and it’s a good way to go if you want to build a GUI application!
00:21:14.240 Here’s a number of reasons why.
00:21:22.719 One is you already know Ruby. If you're sitting in this room, there’s a strong chance you know more Ruby than any of the other languages mentioned.
00:21:31.760 Number two: you can use any existing Ruby libraries. The libraries that you’ve already written, there are vast gems on rubygems.org.
00:21:39.280 MacRuby is also all Ruby 1.9. Especially if you’re bundling the application, you can use the cool new hash syntax without worrying about breaking code.
00:21:45.920 It allows you to use all the new syntactic features in the latest version of Ruby.
00:21:54.720 There’s also no global interpreter lock, so it has the potential to be really fast.
00:22:03.600 It has a much better garbage collector than even the latest version of Ruby!
00:22:10.720 Here are some benchmarks. The x-axis is confusing—there are just a bunch of different benchmarks, and I couldn’t figure out how to rotate them.
00:22:29.600 Each bar represents a benchmark relative to Ruby 1.9. If you see scores like 4, 5, or 6, it means it’s six times faster than Ruby 1.9.
00:22:42.080 Not everything is faster, but it makes it clear that MacRuby outperforms Ruby 1.9 in most scenarios.
00:22:59.280 You aren’t taking a performance hit by writing MacRuby applications; many times, it's a performance gain!
00:23:16.720 A lot of people would be well served to run their existing non-MacRuby applications on MacRuby because it’s such a fast Ruby.
00:23:29.760 The Mac App Store is a tremendous new opportunity for app developers.
00:23:39.680 I think Pixelmator reported making a million dollars in the first 20 days of sales on the App Store—that’s an amazing statistic!
00:23:51.040 You might think, oh, maybe I should do an iPhone app or maybe an iPad app; those are much bigger markets.
00:23:58.320 That's true; more people have iPhones and iPads than Macs, but that’s offset by the fact that Mac users are willing to pay more for apps.
00:24:07.600 You can do greater things without being limited by the screen real estate of a phone, making the design constraint interesting.
00:24:16.080 There’s a great opportunity to capitalize on this wave of apps, and there’s also a lot less competition!
00:24:26.560 Before all these barriers for writing Mac desktop apps existed, now they’re sort of going down.
00:24:37.600 Now the opportunity is increasing!
00:24:44.240 So, how do you do it? You can go to macruby.com and download it.
00:24:55.120 It’s just a package, like any other Mac DMG or maybe a ZIP file. You go through an installer, and it works.
00:25:06.960 If you’re running RVM, make sure you have the latest version of RVM, and then just say 'rvm install macruby'!
00:25:15.680 I’ve been assured that there’s a 20 gig broadband pipe, so if you all want to put that to the test now, just type those two commands.
00:25:23.040 One of the dependencies you might need is LLVM, which you can install with Homebrew or MacPorts.
00:25:38.080 You can also build it from source if that’s your thing.
00:25:45.920 You also need Xcode tools, which you probably already have.
00:25:52.080 So, if you don’t have that information yet, get it!
00:26:03.600 What are some other resources you should know about? There’s a great book that Matt Aimonetti is working on called 'Ruby: The Definitive Guide.' It’s an O’Reilly book.
00:26:10.720 He’s writing it live on the internet, so you can see every time he updates it. It’s totally free, so just go check it out.
00:26:20.960 The first nine chapters are mostly fleshed out, and he’s updating as new releases of MacRuby come out.
00:26:31.920 That book will presumably be published shortly after MacRuby 1.0 goes live.
00:26:41.440 There’s another book called 'MacRuby in Action,' which you can also get early. It hasn’t been published yet, but you can pay 20 bucks and get a PDF of whatever state it's currently in.
00:26:52.160 Despite what I said about Objective-C earlier, having knowledge of Cocoa will definitely help you.
00:27:01.120 You can call any of the Cocoa methods in Ruby in MacRuby. Having a fundamental understanding of what's happening under the hood helps, and this is an awesome book!
00:27:09.520 If you have any feedback about this talk, feel free to send me an email, tweet, or something like that.
00:27:19.520 You can follow me on GitHub. If you don't have a computer, you can call me. My telephone number is 415-312-2382.
00:27:27.440 Just kidding! Don’t call me.
00:27:31.600 That’s it! I think I have exactly one minute for questions. Maybe one or two?
00:27:59.520 Yes, okay, sorry, real fast. First one: ruby gems that are not compatible with MacRuby?
00:28:03.840 Stuff that has native extensions; we are working on that.
00:28:06.720 The second question is about distributing an app that is using, you know, not mine.
00:28:20.640 Okay, I’ll review the questions for those who can hear it. The first question was... If gems aren’t compatible, that’s above Ruby. In theory, MacRuby should be able to handle those.
00:28:30.040 So if you find a gem that’s not compatible with MacRuby and it won’t install, you should file that as a bug.
00:28:47.520 The second question was about the bundling. If somebody doesn't have Lion, Apple may decide to make MacRuby a public framework.
00:28:58.960 So if they expose some interface that allows you to include it, you’d then have all of the code stack; you wouldn’t have to bundle it.
00:29:10.720 What you would have to do for earlier releases of Mac OS X, hopefully, they’d also release point version updates for Snow Leopard.
00:29:23.520 But if not, you just have two separate binaries.
00:29:30.080 You could distribute a package for Lion and a very lightweight package for pre-Lion.
00:29:38.320 Great! I think I’m out of time, is that right?
00:29:44.240 Yes? Okay, great! Thanks, everybody!
00:38:17.760 (foreign)
Explore all talks recorded at Ruby on Ales 2011
+8