SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Ruby has built an enormous following of developers attracted by its intuitiveness, flexibility, and simplicity. Meanwhile, Microsoft’s .NET has grown and matured into a platform of unparalleled power. IronRuby brings them together, enabling developers to write elegant, efficient Ruby code that seamlessly integrates with .NET objects and leverages .NET’s full capabilities. Now, in IronRuby Unleashed, one of IronRuby’s most respected early adopters demonstrates how to write outstanding production software with the brand new IronRuby 1.0.
Writing for both Ruby and .NET developers, Shay Friedman covers every facet of IronRuby programming. Friedman begins by explaining how IronRuby leverages the new Dynamic Language Runtime (DLR) libraries to run atop the .NET Framework and access its resources. Next, he presents an in-depth IronRuby tutorial that ranges from basic syntax and object-oriented programming techniques through advanced concepts. Building on this foundation, you’ll learn how to make the most of a broad spectrum of .NET platform features.
IronRuby Unleashed thoroughly illuminates the use of IronRuby and .NET with today’s most powerful frameworks and technologies, including WPF, ASP.NET MVC, Ruby on Rails, and Silverlight. You’ll also find detailed coverage of unit testing, as well as cutting-edge techniques for extending IronRuby with C# or VB.NET.
Detailed information on how to...
IronRuby Unleashed: An Interview with Shay Friedman
Testing .NET Applications Using IronRuby
Download the sample pages (includes Chapter 4 and Index)
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
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 57
Ranges 59
Booleans 60
Regular Expressions 60
Date and Time 62
Constants 63
Control Structures 64
Conditions 64
Loops 70
The yield Statement 76
BEGIN and END 77
Exception Handling 78
Exception Information 78
rescue 78
else 81
ensure 82
raise 83
Custom Error Classes 85
6 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
7 The Standard Library 131
Using the Libraries 131
Libraries Available in IronRuby 132
Libraries Reference 135
Abbrev 135
Base64 135
Benchmark 136
BigDecimal 136
Complex 137
CSV 137
Digest 138
E2MMAP 139
English 140
Erb 141
FileUtils 143
Logger 143
Monitor 144
Net/http 144
Observer 145
Open-uri 145
Ping 147
Rational 152
Rexml 153
Singleton 154
Socket 154
Thread 157
YAML 157
WEBrick 157
Zlib 158
Finding More Libraries 159
8 Advanced Ruby 161
Threads 161
Exceptions Within Threads 163
Passing Data In and Out 164
Thread Priority 164
Thread State 165
Thread Synchronization 167
Handling Files 169
Reading Files 170
Writing Files 172
Accessing File Properties 173
Listing Directories 174
File Operations 175
Reflection 176
Finding Living Objects 176
Investigating Objects 177
Invoke Methods and Set Variables Dynamically 178
Execute Code Dynamically 180
Marshaling 181
Binary Marshaling 181
Textual Marshaling 182
RubyGems 183
Installing RubyGems 183
Installing Gems 183
Using Installed Gems 183
Rake 184
IronRuby RubyGems Limitations and Expertise 185
Finding Gems 185
Design Patterns 186
The Strategy Pattern 186
The Iterator Pattern 188
The Command Pattern 190
The Singleton Pattern 192
The Observer Pattern 194
The Builder Pattern 196
Domain-Specific Languages 199
Part III IronRuby Fundamentals
9 .NET Interoperability Fundamentals 207
Bringing .NET into Ruby 207
require 207
load_assembly 209
load 210
The $LOAD_PATH Variable 210
.NET Code Mapping 210
Types Differences 211
Coding Standards Collision 211
Private Binding Mode 213
Using .NET Objects 214
Namespaces 214
Interfaces 216
Classes 216
Structs 217
Delegates 217
Events 218
Enums 221
Constants 222
Methods 222
Fields 228
Properties 228
Generics 229
Special IronRuby Methods 231
Object Class Methods 231
Class Class Methods 232
Method Class Methods 233
String Class Methods 234
The IronRuby Class 235
CLR Objects and Ruby’s Reflection 237
The Basic Object 237
10 Object-Oriented .NET in IronRuby 239
Inheriting from CLR Classes 239
Regular Classes 239
Abstract Classes 242
Sealed and Static Classes 243
Inheriting from CLR Structs 243
Inheriting from CLR Interfaces 243
Overriding Methods 245
Virtual Methods 245
Abstract Methods 246
Regular Methods 247
Static Methods 248
Methods with Multiple Overloads 249
Sealed Methods 250
Overriding Properties 251
Overriding Events 253
Opening CLR Classes 254
Using Mixins 254
Opening the Object Class 255
Opening Namespaces 256
Part IV IronRuby and the .NET World
11 Data Access 259
Hello, Data Access 259
Preparing Your Environment 260
Contacting a SQL Server 260
Loading the Needed Assemblies 260
Building the Class Structure 260
Building the Connection String 261
Opening a Connection to the SQL Server 262
Querying the Database 263
Wrapping Up sql.rb 264
Using the SqlServerAccessor Class 265
Contacting a MySQL Server 265
Preparing the MySQL Database 266
Loading the Assemblies 267
Building the Class Structure 267
Building the Connection String 267
Opening a Connection to the MySQL Server 268
Querying the Database 268
Inserting Records 269
Deleting Records 269
Wrapping Up mysql.rb 270
Using the MySQLAccessor Class 272
Design Considerations 272
The CachedDataAccess Class 276
Wrapping Up cached_data_access.rb 277
Using the CachedDataAccess Class 278
A Word About LINQ 279
12 Windows Forms 281
Introduction 281
The Application Structure 282
Building the Chat Class 282
Requiring the Needed Assemblies 282
Initiating the Class 282
Receiving Messages 283
Sending Messages 283
Wrapping Up the Chat Class (chat.rb) 284
Building the Chat Windows Form 285
Loading the Needed Assemblies 285
Building the Class 285
Initializing the Form 286
Setting the Form Properties 287
Adding Controls 289
Adding Functionality 293
Using the Visual Studio Visual Designer 295
Wrapping Up the ChatForm Class 297
Writing the Execution Code 300
13 Windows Presentation Foundation (WPF) 303
Hello, WPF 303
XAML 305
Namespaces 306
IronRuby and WPF Fundamentals 307
Running XAML 307
Retrieving WPF Elements 308
Event Handling 308
Windows 309
Window 309
Navigation Window 314
Layout Controls 317
StackPanel 317
Grid 319
Canvas 320
More Panels 321
Graphics and Animations 321
Shapes 322
Brushes 322
Animations 324
Data Binding 325
Binding to Static Data 325
Binding to Dynamic Data 327
REPL 329
14 Ruby on Rails 331
Preparing Your Environment 331
Hello, IronRuby on Rails 332
Creating the Initial Project Files 333
Directory Structure 333
Database Configuration 334
Running the Server 337
The Basic Concepts 339
MVC 339
REST 339
CoC 340
DRY 340
Main Components 340
The Model: ActiveRecord 340
The View: ActionView 341
The Controller: ActionController 341
Routes 341
Know Your Environment 342
script/server 342
script/generate 343
db:migrate 345
Creating a Page 346
Generating the Page Controller and View 346
Helper Classes 349
Adding Stylesheets 350
Adding Layouts 351
Adding Functionality 353
Creating a Database-Driven Page 354
Generating the Page Resources 354
Polishing the Index Page 356
15 ASP.NET MVC 363
Preparing Your Environment 363
Installing ASP.NET MVC 364
Obtaining the IronRubyMvc DLL 364
Adding IronRubyMvc Templates to Visual Studio 365
Hello, ASP.NET MVC 365
Generating the Initial Project 365
MVC 368
Models 368
Controllers 371
alias_action 375
Views 378
Routes 385
Custom Routes 386
Filters 387
Action Filters 387
Result Filters 390
Authorization Filters 392
Exception Filters 393
Controller-wide Filters 394
Custom Action Filter Classes 395
Validations 396
Inside the Model 396
Inside the Controller 396
Inside the View 397
Classic ASP.NET Features 398
A Word About Classic ASP.NET 398
16 Silverlight 401
Prepare Your Environment 402
Hello, Silverlight 402
The sl Tool: The Silverlight Application Creator 402
The chr Tool: The Development Server 404
Add Silverlight to a Web Page 406
XAML 409
Layout 410
Controls 411
Adding Code 411
Running XAML 411
Retrieving Silverlight Elements 412
Event Handling 414
Accessing the HTML Page and Window 414
Graphics 415
Media and Animations 417
Data Binding 419
Static Data 419
Dynamic Data 420
Data Templates 422
17 Unit Testing 425
The Tested Code 426
Test::Unit 427
Test Cases 427
Assertions 428
Setup and Teardown 432
Test Suites 433
Running the Tests 434
RSpec 435
Install RSpec 436
Requiring Needed Libraries 436
Running Tests 437
Creating a Behavior with describe 438
Creating Examples with it 439
Expectation Methods 439
Before and After 442
Cucumber 443
Installing Cucumber 445
Project Structure 445
Features 446
Scenarios 447
A Background 452
Tags 453
Hooks 454
A World 456
Multilanguage 456
Executing Cucumber 457
18 Using IronRuby from C#/VB.NET 459
Hello, IronRuby from CLR 459
The Classes of the Process 461
ScriptRuntime 462
ScriptEngine 463
ScriptScope 465
ScriptSource 466
Executing IronRuby from C#/VB.NET 468
Executing an IronRuby File 468
Executing IronRuby Code from a String 468
Pass Variables to and from IronRuby 469
Using IronRuby Objects 470
Using External Libraries 472
Part V Advanced IronRuby
19 Extending IronRuby 477
Creating an Extension 478
Main Concepts 478
The Extension Project 481
Target Environments 482
Modules 482
Classes 488
Methods 491
Constants 501
Using an Extension in IronRuby 501
Building an IronRuby Extension 501
Creating the Extension Visual Studio Project 502
Adding Build Configurations 502
Creating the Actual Code 504
Creating the Ruby Programming Interface 506
Generating the Library Initializer 508
Using the IronRuby .NET Extension in IronRuby 509
Index 511