Home > Store

IronRuby Unleashed

Register your product to gain access to bonus material or receive a coupon.

IronRuby Unleashed

eBook (Watermarked)

  • Sorry, this book is no longer in print.
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader PDF The popular standard, used most often with the free Adobe® Reader® software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.

Not for Sale

Description

  • Copyright 2010
  • Edition: 1st
  • eBook (Watermarked)
  • ISBN-10: 0-672-33674-X
  • ISBN-13: 978-0-672-33674-4

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...

  • Install IronRuby and choose the right development environment for your needs
  • Master IronRuby syntax, methods, blocks, classes, modules, libraries, and more
  • Write code that takes advantage of IronRuby’s dynamic and metaprogramming features
  • Utilize .NET services and frameworks to write more powerful Ruby code than ever before
  • Incorporate efficient data access into your IronRuby applications
  • Use IronRuby to build Windows software with both WinForms and WPF
  • Rapidly build high-quality Web applications with IronRuby and Ruby on Rails
  • Create rich Web 2.0 applications with IronRuby and Microsoft Silverlight
  • Test .NET code with Ruby’s leading unit testing frameworks
  • Run IronRuby code from other .NET code, and create .NET code libraries that fit well with IronRuby code

Sample Content

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

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

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020