1. Trang chủ
  2. » Tất cả

IronRuby Unleashed [Friedman 2010-02-21]

548 0 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 548
Dung lượng 9,3 MB

Nội dung

Shay Friedman IronRuby UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA IronRuby Unleashed Copyright © 2010 by Pearson Education, Inc 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 otherwise, without written permission from the publisher No patent liability is assumed 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 responsibility for errors or omissions Nor is any liability assumed for damages resulting from the use of the information contained herein ISBN-13: 978-0-672-33078-0 ISBN-10: 0-672-33078-4 Library of Congress Cataloging-in-Publication Data Friedman, Shay IronRuby unleashed / Shay Friedman p cm ISBN 978-0-672-33078-0 IronRuby (Computer program language) Microsoft NET Framework Ruby (Computer program language) I Title QA76.73.I586F74 2010 006.7’882—dc22 2009050114 Printed in the United States of America First Printing: February 2010 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Pearson Education, Inc cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied The information provided is on an “as is” basis The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book Bulk Sales Pearson offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales For more information, please contact: U.S Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales +1-317-581-3793 international@pearsontechgroup.com Editor-in-Chief Karen Gettman Executive Editor Neil Rowe Acquisitions Editor Brook Farling Development Editor Mark Renfrow Managing Editor Kristy Hart Project Editor Andy Beaster Copy Editor Keith Cline Indexer Word Wise Publishing Services Proofreader San Dee Phillips Technical Editor Justin Etheredge Publishing Coordinator Cindy Teeters Interior Designer Gary Adair Cover Designer Gary Adair Compositor Nonie Ratcliff Contents at a Glance Introduction Part I Introduction to IronRuby Introduction to the Ruby Language Introduction to the NET Framework 13 Introduction to the Dynamic Language Runtime (DLR) 21 Getting Started with IronRuby 25 Part II The Ruby Language The Basic Basics 43 Ruby’s Code-Containing Structures 87 The Standard Library 131 Advanced Ruby Part III 161 IronRuby Fundamentals 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 16 Silverlight 401 17 Unit Testing 425 18 Using IronRuby from C#/VB.NET 459 Part V 19 363 Advanced IronRuby Extending IronRuby 477 Index 511 Table of Contents Introduction Part I 1 Introduction to IronRuby Introduction to the Ruby Language History of the Ruby Language Implementations Features Dynamic Language Object Oriented Duck Typing Metaprogramming REPL 10 Available Libraries 11 Introduction to the NET Framework 13 History of the NET Framework Overview Features Common Language Infrastructure Assemblies Base Class Library Security Memory Management Frameworks 13 15 16 16 18 19 19 19 20 Introduction to the Dynamic Language Runtime 21 Overview Features Common Hosting Model Runtime Components Language Implementation 22 23 23 23 24 Contents Part II v Getting Started with IronRuby 25 Overview Installing IronRuby IronRuby Folders Getting the Sources Executables and Tools The IronRuby Interpreter (ir.exe) File Execution Mode Development Environments Ruby in Steel NetBeans RubyMine Others The Power of IronRuby 25 26 29 29 30 31 32 34 34 35 36 37 38 The Ruby Language Ruby Basics 43 Basic Syntax Comments Setting Values to Variables Calling Methods Code File Structure Coding Standards Hello World Variables Numbers Text Arrays Hashes Ranges Booleans Regular Expressions Date and Time Constants Control Structures Conditions Loops The yield Statement BEGIN and END 43 43 44 45 46 47 48 48 48 50 54 57 59 60 60 62 63 64 64 70 76 77 vi IronRuby Unleashed Exception Handling Exception Information rescue else ensure raise Custom Error Classes 78 78 78 81 82 83 85 Ruby’s Code-Containing Structures 87 Methods 87 Defining Methods 88 Method Naming 90 Returning a Value from Methods 90 Method Name Aliasing 91 Default Parameter Values 92 Special Parameter Types 93 Associate Methods with Objects 94 Removing Method Definitions 95 Blocks, Procs, and Lambdas 96 Blocks 96 Procs 97 Lambdas 99 Flow-Altering Keywords Within Blocks, Procs, and Lambdas 100 Classes 101 Defining Classes 101 Creating a Class Instance 102 Defining a Constructor 102 Variables Inside Classes 102 Accessors 107 Methods 109 Operator Overloading 111 Special Methods 115 The self Keyword 118 Visibility Control 118 Inheritance 120 Duck Typing 124 Modules 126 Module-Contained Objects 126 Namespaces 127 Mixins 128 Contents vii The Standard Library 131 Using the Libraries Libraries Available in IronRuby Libraries Reference Abbrev Base64 Benchmark BigDecimal Complex CSV Digest E2MMAP English Erb FileUtils Logger Monitor Net/http Observer Open-uri Ping Rational Rexml Singleton Socket Thread YAML WEBrick Zlib Finding More Libraries 131 132 135 135 135 136 136 137 137 138 139 140 141 143 143 144 144 145 145 147 152 153 154 154 157 157 157 158 159 Advanced Ruby 161 Threads Exceptions Within Threads Passing Data In and Out Thread Priority Thread State Thread Synchronization Handling Files Reading Files Writing Files 161 163 164 164 165 167 169 170 172 viii IronRuby Unleashed Accessing File Properties Listing Directories File Operations Reflection Finding Living Objects Investigating Objects Invoke Methods and Set Variables Dynamically Execute Code Dynamically Marshaling Binary Marshaling Textual Marshaling RubyGems Installing RubyGems Installing Gems Using Installed Gems Rake IronRuby RubyGems Limitations and Expertise Finding Gems Design Patterns The Strategy Pattern The Iterator Pattern The Command Pattern The Singleton Pattern The Observer Pattern The Builder Pattern Domain-Specific Languages Part III 173 174 175 176 176 177 178 180 181 181 182 183 183 183 183 184 185 185 186 186 188 190 192 194 196 199 IronRuby Fundamentals NET Interoperability Fundamentals 207 Bringing NET into Ruby require load_assembly load The $LOAD_PATH Variable NET Code Mapping Types Differences Coding Standards Collision Private Binding Mode Using NET Objects Namespaces Interfaces 207 207 209 210 210 210 211 211 213 214 214 216 ix Contents 10 Classes Structs Delegates Events Enums Constants Methods Fields Properties Generics Special IronRuby Methods Object Class Methods Class Class Methods Method Class Methods String Class Methods The IronRuby Class CLR Objects and Ruby’s Reflection The Basic Object 216 217 217 218 221 222 222 228 228 229 231 231 232 233 234 235 237 237 Object-Oriented NET in IronRuby 239 Inheriting from CLR Classes Regular Classes Abstract Classes Sealed and Static Classes Inheriting from CLR Structs Inheriting from CLR Interfaces Overriding Methods Virtual Methods Abstract Methods Regular Methods Static Methods Methods with Multiple Overloads Sealed Methods Overriding Properties Overriding Events Opening CLR Classes Using Mixins Opening the Object Class Opening Namespaces 239 239 242 243 243 243 245 245 246 247 248 249 250 251 253 254 254 255 256 x IronRuby Unleashed Part IV 11 12 IronRuby and the NET World Data Access 259 Hello, Data Access Preparing Your Environment Contacting a SQL Server Loading the Needed Assemblies Building the Class Structure Building the Connection String Opening a Connection to the SQL Server Querying the Database Wrapping Up sql.rb Using the SqlServerAccessor Class Contacting a MySQL Server Preparing the MySQL Database Loading the Assemblies Building the Class Structure Building the Connection String Opening a Connection to the MySQL Server Querying the Database Inserting Records Deleting Records Wrapping Up mysql.rb Using the MySQLAccessor Class Design Considerations The CachedDataAccess Class Wrapping Up cached_data_access.rb Using the CachedDataAccess Class A Word About LINQ 259 260 260 260 260 261 262 263 264 265 265 266 267 267 267 268 268 269 269 270 272 272 276 277 278 279 Windows Forms 281 Introduction The Application Structure Building the Chat Class Requiring the Needed Assemblies Initiating the Class Receiving Messages Sending Messages Wrapping Up the Chat Class (chat.rb) Building the Chat Windows Form Loading the Needed Assemblies Building the Class 281 282 282 282 282 283 283 284 285 285 285 ... testing frameworks and running IronRuby code from NET code The last part of the book, Part V, “Advanced IronRuby, ” covers IronRuby advanced topics If you want to extend IronRuby objects or to create... 425 18 Using IronRuby from C#/VB.NET 459 Part V 19 363 Advanced IronRuby Extending IronRuby ... 239 239 242 243 243 243 245 245 246 247 248 249 250 251 253 254 254 255 256 x IronRuby Unleashed Part IV 11 12 IronRuby and the NET World Data Access 259 Hello, Data Access

Ngày đăng: 17/04/2017, 08:25