Ironruby UNLEASHED
Total Page:16
File Type:pdf, Size:1020Kb
Shay Friedman IronRuby UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA IronRuby Unleashed Editor-in-Chief Copyright © 2010 by Pearson Education, Inc. Karen Gettman All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or Executive Editor otherwise, without written permission from the publisher. No patent liability is assumed Neil Rowe with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no Acquisitions Editor responsibility for errors or omissions. Nor is any liability assumed for damages resulting Brook Farling from the use of the information contained herein. Development Editor ISBN-13: 978-0-672-33078-0 ISBN-10: 0-672-33078-4 Mark Renfrow Library of Congress Cataloging-in-Publication Data Managing Editor Friedman, Shay. Kristy Hart IronRuby unleashed / Shay Friedman. p. cm. Project Editor ISBN 978-0-672-33078-0 Andy Beaster 1. IronRuby (Computer program language) 2. Microsoft .NET Framework. 3. Ruby (Computer program language) I. Title. Copy Editor QA76.73.I586F74 2010 Keith Cline 006.7’882—dc22 2009050114 Indexer Printed in the United States of America Word Wise Publishing First Printing: February 2010 Services Trademarks Proofreader All terms mentioned in this book that are known to be trademarks or service marks San Dee Phillips have been appropriately capitalized. Pearson Education, Inc. cannot attest to the accu- racy of this information. Use of a term in this book should not be regarded as affecting Technical Editor the validity of any trademark or service mark. Justin Etheredge Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possi- Publishing ble, but no warranty or fitness is implied. The information provided is on an “as is” Coordinator basis. The author and the publisher shall have neither liability nor responsibility to any Cindy Teeters person or entity with respect to any loss or damages arising from the information contained in this book. Interior Designer Gary Adair Bulk Sales Pearson offers excellent discounts on this book when ordered in quantity for bulk Cover Designer purchases or special sales. For more information, please contact: Gary Adair U.S. Corporate and Government Sales 1-800-382-3419 Compositor [email protected] Nonie Ratcliff For sales outside of the U.S., please contact: International Sales +1-317-581-3793 [email protected] Contents at a Glance Introduction ..................................................................................... 1 Part I Introduction to IronRuby 1 Introduction to the Ruby Language...................................................... 5 2 Introduction to the .NET Framework .................................................. 13 3 Introduction to the Dynamic Language Runtime (DLR) ....................... 21 4 Getting Started with IronRuby ........................................................... 25 Part II The Ruby Language 5 The Basic Basics................................................................................ 43 6 Ruby’s Code-Containing Structures .................................................... 87 7 The Standard Library....................................................................... 131 8 Advanced Ruby .............................................................................. 161 Part III IronRuby Fundamentals 9 .NET Interoperability Fundamentals ................................................. 207 10 Object-Oriented .NET in IronRuby ................................................... 239 Part IV IronRuby and the .NET World 11 Data Access .................................................................................... 259 12 Windows Forms.............................................................................. 281 13 Windows Presentation Foundation (WPF) ......................................... 303 14 Ruby on Rails ................................................................................. 331 15 ASP.NET MVC ................................................................................ 363 16 Silverlight ...................................................................................... 401 17 Unit Testing ................................................................................... 425 18 Using IronRuby from C#/VB.NET ..................................................... 459 Part V Advanced IronRuby 19 Extending IronRuby ........................................................................ 477 Index.............................................................................................. 511 Table of Contents Introduction 1 Part I Introduction to IronRuby 1 Introduction to the Ruby Language 5 History of the Ruby Language............................................................... 5 Implementations ................................................................................. 6 Features .............................................................................................. 6 Dynamic Language...................................................................... 6 Object Oriented .......................................................................... 7 Duck Typing ............................................................................... 8 Metaprogramming ...................................................................... 9 REPL ........................................................................................ 10 Available Libraries ..................................................................... 11 2 Introduction to the .NET Framework 13 History of the .NET Framework ........................................................... 13 Overview .......................................................................................... 15 Features ............................................................................................ 16 Common Language Infrastructure............................................... 16 Assemblies ................................................................................ 18 Base Class Library ...................................................................... 19 Security .................................................................................... 19 Memory Management................................................................ 19 Frameworks .............................................................................. 20 3 Introduction to the Dynamic Language Runtime 21 Overview .......................................................................................... 22 Features ............................................................................................ 23 Common Hosting Model ........................................................... 23 Runtime Components................................................................ 23 Language Implementation ......................................................... 24 Contents v 4 Getting Started with IronRuby 25 Overview .......................................................................................... 25 Installing IronRuby............................................................................ 26 IronRuby Folders ....................................................................... 29 Getting the Sources ................................................................... 29 Executables and Tools ........................................................................ 30 The IronRuby Interpreter (ir.exe)................................................. 31 File Execution Mode .................................................................. 32 Development Environments ............................................................... 34 Ruby in Steel............................................................................. 34 NetBeans .................................................................................. 35 RubyMine................................................................................. 36 Others ...................................................................................... 37 The Power of IronRuby ...................................................................... 38 Part II The Ruby Language 5 Ruby Basics 43 Basic Syntax ...................................................................................... 43 Comments................................................................................ 43 Setting Values to Variables.......................................................... 44 Calling Methods........................................................................ 45 Code File Structure .................................................................... 46 Coding Standards ...................................................................... 47 Hello World ...................................................................................... 48 Variables ........................................................................................... 48 Numbers................................................................................... 48 Text ......................................................................................... 50 Arrays....................................................................................... 54 Hashes ....................................................................................