Ruby
Creating Desktop Applications with Ruby on Mac
Summarized using AI

Creating Desktop Applications with Ruby on Mac

by Brian Cooke

The video titled "Creating Desktop Applications with Ruby on Mac" features a presentation by Brian Cooke at the LoneStarRuby Conf 2008. In this talk, Cooke introduces the concept of developing desktop applications using Ruby and Cocoa on macOS.

Key Points Discussed:

  • Introduction to Brian Cooke: Cooke shares his background as a software developer, detailing his experiences with applications like Ruid and R Switch, which he developed while working at Equal Logic. He emphasizes his transition to independent contracting and his love for coding on macOS.
  • Understanding Cocoa: Cooke explains that Cocoa refers to Apple’s frameworks built in Objective-C, crucial for macOS app development. He notes the history of Cocoa stemming from Apple’s acquisition of NeXT and touches on the language's syntax and structure.
  • Development Environment: An overview of Xcode and Interface Builder is provided, highlighting the tools used to create Cocoa applications. Cooke discusses challenges faced when using Ruby in Xcode, which is primarily optimized for Objective-C.
  • Key Concepts: The talk covers Key-Value Coding (KVC) and Key-Value Observing (KVO), explaining how they facilitate the dynamic management of application state and UI elements.
  • Demo of Ruby Cocoa: Cooke conducts a hands-on demo showing how to create a simple application using the Ruby language with Cocoa components. He demonstrates the setup of a basic application that generates random names and interactively updates the UI using Ruby Cocoa.
  • Future of Ruby Cocoa: The potential of Ruby Cocoa is discussed relative to its simplicity for developers, along with some limitations regarding performance and debugging. Cooke encourages the understanding of Objective-C for leveraging Ruby Cocoa effectively.
  • Learning Resources: Cooke recommends resources such as "Cocoa Programming for Mac OS X" for those interested in deepening their understanding of Cocoa and how it interacts with Ruby.
  • Concerns and Considerations: He addresses possible downsides of using Ruby Cocoa, including performance variances, packaging code for commercial applications, and debugging complexities.
  • Final Thoughts: Brian concludes with insights on the technology's potential, inviting questions and further discussion on the intersection of Ruby and Objective-C development.

Main Takeaways:

  • Ruby Cocoa offers a unique approach to app development on macOS, enabling developers to leverage both Ruby’s simplicity and the power of Cocoa.
  • Understanding Objective-C foundations is crucial for maximizing Ruby Cocoa’s potential.
  • The future of Ruby Cocoa looks promising with tools like Mac Ruby emerging to enhance this development experience.
00:00:06.359 Video equipment rental costs were paid for by Peep Code screencasts.
00:00:19.359 I gave a lightning talk at the Mountain West Ruby Conference on this topic, and I had to do a lot of hand waving. I was trying to finish in five minutes, but I think I managed to stretch it to nine or ten minutes.
00:00:25.279 Although I wasn't planning to give that lightning talk, I noticed many people using Macs at the conference, so I wanted to present on Ruby Cocoa and developing desktop applications on OS X. This is our schedule.
00:00:41.399 I will discuss who I am, what experience I have that allows me to speak, what Cocoa is, explain some examples of applications you may have encountered that are actually Ruby Cocoa applications, and I will also conduct a quick demo. Finally, we will talk about the future of Ruby Cocoa.
00:01:06.240 So, who am I? I'm from Boise, Idaho, and I have spent most of my paid software development career working for EIC. This was the best image I could find because they were purchased by Dell in January and I left the company to become an independent contractor under the name of Ruof.
00:01:19.079 I have also developed some applications. Both of these applications were actually created while I was still at Equal Logic. One application, Ruid, allows you to drop videos into it, and it saves your profiles while resizing.
00:01:38.880 The second application, R Switch, lets you create profiles for any Cocoa application, enabling you to maintain two copies of your address book—one for work and one for home. I was proud to receive a design award from Apple for this application.
00:01:57.200 As I started getting into Ruby on Rails, I initiated a side project called Simply Invoices, which generates invoices. I am currently contracting with a company called Maala Media to work on Rails projects.
00:02:11.920 Catalog Choice.org is nearing its millionth user, and MLK.com is a German band promotion site for concerts. We are also working on a project where students are writing letters to the next president to be published on a map.
00:02:29.760 I am also a father of two children, and they get along pretty well.
00:02:39.600 So, what is Cocoa? In this case, Cocoa is not the chocolate drink. Instead, it's Apple's name for all their Objective-C frameworks, covering aspects such as QuickTime, Address Book, Bonjour, Quartz, Core Graphics, and Core Audio.
00:02:54.000 It stems from NeXT. When Apple acquired NeXT, OS X was originally based on the NeXT operating system, so all the classes created at that time were prefixed with 'NS' for NextStep. This naming convention has persisted, and they have never needed to change it.
00:03:29.400 While working at Equal Logic, I purchased my first Mac on eBay, and I realized I needed to learn how to program on it and develop native applications for a platform I was falling in love with. I quickly found that Cocoa, Objective-C, and the entire platform actually made development quick and efficient.
00:03:54.519 I had a similar experience when I began working with Ruby and Rails; the efficiency compared to PHP was refreshing. Cocoa is built with Objective-C, and while it might sound complex, it is actually quite approachable once you get used to the syntax.
00:04:20.239 For example, you might see syntax like this, where I am just allocating an array and initializing it with two strings. There are some similarities between Objective-C and Ruby, as they both draw elements from Smalltalk.
00:04:30.840 In Objective-C, messages are sent to objects instead of calling functions in the traditional sense. It isn't strongly typed, though it is based on C, and everything essentially falls back to pointers being passed around. Classes in Objective-C are somewhat open, allowing you to use what are called categories to add methods to them, though you cannot add properties as you can in Ruby.
00:05:04.800 With Leopard, Apple shipped Objective-C 2.0, which introduced blocks and garbage collection, and now with Snow Leopard, they are shipping Objective-C 2.1, which adds blocks and other enhancements. When I was researching, I found that Wikipedia has a great entry on Objective-C that discusses its differences from C and C++.
00:05:29.919 When you are developing a Cocoa application with Ruby Cocoa or any other frameworks, you will use Xcode and Interface Builder. You probably have them installed already; they come with the OS X distribution, and they're free. If you want the latest versions, you can register at developer.apple.com.
00:06:10.240 Xcode is where you will spend most of your time. It has a new organize feature introduced in Leopard, allowing you to build and manage projects more efficiently. In the interface, you can see the devices connected to your machine, like iPhones, if you are doing mobile development.
00:06:35.919 The editor in Xcode is great for Objective-C, but not as polished for Ruby programming. For example, if you select a block of code and use the keyboard shortcut to comment it out, it will use C-style comments, which is not ideal for Ruby. While they are improving the Ruby support, it remains primarily an Objective-C editor, with the Ruby features being added over time.
00:07:10.199 Additionally, you can configure Xcode to open Ruby files in your favorite text editor instead. Xcode builds and manages the entire build environment, allowing you to create tasks for your build stage. For example, if you want to include the Git revision number in your About box, you can set up a build task to pull that information and inject it into your app.
00:07:45.280 Xcode features a robust Objective-C debugger based on GDB and uses the GCC compiler. It has a nice frontend, but I haven't found it as effective when working with Ruby. Often, I've reverted to simpler debugging methods like using print statements in my code, which isn't ideal but necessary.
00:08:36.639 The documentation provided by Apple is also well-written and helpful for developers. It offers both reference materials for classes and companion guides walking you through various tasks and features. While Cocoa is not open-source, they have made an effort to provide good documentation, which is usually accurate and comprehensive.
00:09:20.600 Interface Builder is where you will design your application's user interface. It allows you to drag and drop standard controls, set their sizes, and manage the layout visually. In our demo, we will see how to wire up our code to the interface.
00:09:53.360 Key-Value Coding (KVC) and Key-Value Observing (KVO) are important concepts in Cocoa. KVC allows you to set and retrieve values via string keys, while KVO enables observers to be notified when changes occur to properties. We will cover some examples of using KVC and KVO in our applications.
00:10:39.480 With KVC, when you set a value for a key, if a setter method exists, it is called. Similarly, when retrieving a value, the getter method is called if it exists. When binding controls in Interface Builder, this allows for intuitive management of UI elements and ensures updates are reflected automatically in the application state.
00:11:34.760 In our demo of Ruby Cocoa, we will use a simple application that generates random names using the Faker gem. This app will demonstrate how to utilize Cocoa through Ruby to create a graphical interface and interact with it effectively.
00:12:30.200 In Xcode, we will create a new project and select the Cocoa Ruby template. This generates a basic framework for our application, ready for us to customize. Once the application initializes, it allows for setup and further development.
00:13:20.000 Using Interface Builder, we will drag various UI controls, such as labels and buttons, into our application window. We will set constraints and behaviors for our controls so that the interface behaves intuitively when users interact with it.
00:14:15.200 To manage the layout, we will make sure our controls properly resize and rearrange themselves as needed. With Interface Builder's visual capabilities, we can simulate how the application will look and feel while being developed, allowing us to adjust as we go.
00:15:12.480 Once our interface is laid out correctly, we will wire up actions and outlets in our code. This allows buttons to trigger functions and controls to update the state of our application efficiently. We can create a structure that responds dynamically to user input.
00:16:05.400 The primary objective of the demo is to illustrate how Ruby Cocoa can interact with Cocoa's components to provide a smooth user experience. By using Ruby, we maintain a more straightforward development process while taking full advantage of the Cocoa API.
00:16:45.600 As we execute the app, we will see how it generates names and displays them in the interface while maintaining reactive features. This showcases Ruby Cocoa's power in creating user-friendly applications without sacrificing performance.
00:17:32.000 The final implementation will incorporate the ability to persist data using Cocoa frameworks, allowing us to save generated names to the user's address book. This provides a practical use case for Ruby Cocoa's capabilities and reinforces the direct relationship between Ruby and Cocoa programming.
00:18:17.760 Throughout this process, we will maintain awareness of both Ruby and Objective-C's roles in the application. This understanding will help us write more effective code and effectively utilize the features present in both languages.
00:19:04.120 As we wrap up the demonstration, we will look back at the application we built, discuss what Ruby Cocoa offers, and go over some practical examples of how you can apply these skills in your projects. Finally, I'll leave you with resources for further learning and development.
00:20:12.400 Moving on, I will talk about why you would use Ruby Cocoa, as well as some potential reasons not to. It's a language that many of us enjoy working with, and it gives you access to all the available gems. This further increases the practical nature of the applications created.
00:20:40.760 Not only does Ruby Cocoa allow you to package any required gems directly with your applications, but it also lets you explore through IRB: you can interact with Cocoa classes directly if you're set up correctly on your system, making it easier to experiment and build prototypes in Cocoa.
00:22:00.760 Though there are extensive opportunities to create applications that feel correct and familiar, recognize that performance may vary if your tasks are performance-critical. Additionally, the bridging between Ruby and Cocoa does introduce slight overhead in terms of processing.
00:23:14.000 If you’re working on something particularly complex, Objective-C may offer better performance and direct access to Cocoa libraries without the redundancies that the bridge introduces. To effectively use Ruby Cocoa, it's important to understand when these costs come into play.
00:23:57.200 For developers looking to make commercial applications, Ruby Cocoa does present issues related to packaging code. While it’s possible to bundle and protect your Ruby files, do consider that distributing your application freely could lead to unintended access to your source code.
00:24:39.880 Debugging in Ruby Cocoa can sometimes feel cumbersome compared to pure Objective-C applications. This can be a downside, especially for developers used to robust debugging tools in the more traditional Cocoa development environment.
00:25:25.600 I recommend that anyone serious about using Ruby Cocoa for development spend time learning Objective-C first. Understanding how Cocoa and Objective-C interact is critical for effectively leveraging Ruby Cocoa in your projects.
00:26:08.800 Cocoa Programming for Mac OS X is a fantastic resource that many in the Ruby Cocoa community recommend. The book is comprehensive and covers Objective-C extensively, providing a foundational understanding that will benefit your Ruby Cocoa exploration.
00:26:54.160 There is also a new resource focused on Ruby Cocoa from the Pragmatic Programmers, which I have yet to explore fully, but I’ve heard it has valuable insights. With the ongoing development of Mac Ruby, competition in this space is heating up, leading to exciting possibilities in the future.
00:27:29.360 Mac Ruby is poised to provide some substantial advantages over Ruby Cocoa by allowing Ruby to talk directly to Cocoa classes without the bridging issues present in Ruby Cocoa. While Ruby Cocoa has been adopted in Leopard, the future seems bright for both platforms.
00:28:08.480 In terms of interoperability, Ruby and Objective-C can coexist within the same project, allowing for more flexibility in development. This co-working is exemplified in established applications where dependencies may be delivered in either Ruby or Objective-C, demonstrating the potential of both approaches.
00:29:14.400 The integration of different frameworks is possible, enhancing the functionality you can achieve without committing exclusively to one language. Application developers can pick and choose the best elements of both Ruby and Objective-C to build robust Mac applications.
00:30:10.120 To close the discussion, I'll highlight the dynamism of Ruby, allowing for insertion of dynamic aspects into Cocoa applications. Despite a few limitations, the capabilities provided by Ruby Cocoa offer unique advantages worth exploring.
00:30:55.440 Ruby Cocoa remains an exciting option for developers because it has the potential to simplify the process of Cocoa development. There are a few platforms on which Ruby Cocoa and Mac Ruby can compete, and researching these alternatives is worth your time.
00:31:43.880 As we approach the question session, feel free to ask about anything we have covered, whether it's related to development with Ruby Cocoa, the future of Mac Ruby, or general programming practices. I hope this talk has provided valuable insights into Ruby Cocoa.
00:32:29.960 Now, let me take a moment to address any questions you may have. I understand that there can be concerns about performance or merging Ruby with Objective-C in various scenarios, and I will do my best to provide detailed answers to your queries.
00:33:17.400 Thank you for your time, and I appreciate your attention. If you're interested, I'll be around for more discussions throughout the conference, and I'd love to connect over any ideas or projects you may have.
Explore all talks recorded at LoneStarRuby Conf 2008
+18