Ruby for Rails: Ruby techniques for Rails developers

Free Download

Authors:

Edition: illustrated edition

ISBN: 9781932394696, 1932394699

Size: 5 MB (5645572 bytes)

Pages: 528/528

File format:

Language:

Publishing Year:

Category: Tags: , ,

David Black9781932394696, 1932394699

Ruby for Rails, written by Ruby expert David Black (with a forward by David Heinemeier Hansson), helps Rails developers achieve Ruby mastery. Each chapter deepens your Ruby knowledge and shows you how it connects to Rails. You’ll gain confidence working with objects and classes and learn how to leverage Ruby’s elegant, expressive syntax for Rails application power. And you’ll become a better Rails developer through a deep understanding of the design of Rails itself and how to take advantage of it.Newcomers to Ruby will find a Rails-oriented Ruby introduction that’s easy to read and that includes dynamic programming techniques, an exploration of Ruby objects, classes, and data structures, and many neat examples of Ruby and Rails code in action. Ruby for Rails: the Ruby guide for Rails developers!

Table of contents :
Ruby for Rails……Page 1
contents……Page 10
foreword……Page 20
preface……Page 22
acknowledgments……Page 24
about this book……Page 27
Part I The Ruby/Rails landscape……Page 36
How Ruby works……Page 38
1.1 The mechanics of writing a Ruby program……Page 39
1.1.2 A Ruby literacy bootstrap guide……Page 40
1.1.3 A brief introduction to method calls and Ruby objects……Page 42
1.1.4 Writing and saving a sample program……Page 43
1.1.5 Feeding the program to Ruby……Page 44
1.1.6 Keyboard and file input……Page 46
1.1.7 One program, multiple files……Page 49
1.2 Techniques of interpreter invocation……Page 50
1.2.1 Command-line switches……Page 51
1.2.2 A closer look at interactive Ruby interpretation with irb……Page 55
1.3.1 Using standard extensions and libraries……Page 56
1.3.2 Using C extensions……Page 57
1.3.3 Writing extensions and libraries……Page 58
1.4.1 The layout of the Ruby source code……Page 59
1.4.2 Navigating the Ruby installation……Page 60
1.4.3 Important standard Ruby tools and applications……Page 62
1.5 Summary……Page 66
How Rails works……Page 68
2.1 Inside the Rails framework……Page 69
2.1.1 A framework user’s-eye view of application development……Page 70
2.1.2 Introducing the MVC framework concept……Page 71
2.1.3 Meet MVC in the (virtual) flesh……Page 72
2.2 Analyzing Rails’ implementation of MVC……Page 73
2.3 A Rails application walk-through……Page 76
2.3.1 Introducing R4RMusic, the music-store application……Page 77
2.3.2 Modeling the first iteration of the music-store domain……Page 78
2.3.3 Identifying and programming the actions……Page 85
2.3.4 Designing the views……Page 88
2.3.5 Connecting to the application……Page 93
2.4 Tracing the lifecycle of a Rails run……Page 94
2.4.1 Stage 1: server to dispatcher……Page 96
2.4.3 Stage 3: performance of a controller action……Page 97
2.5 Summary……Page 100
Ruby-informed Rails development……Page 102
3.1 A first crack at knowing what your code does……Page 104
3.1.1 Seeing Rails as a domain-specific language……Page 105
3.1.2 Writing program code with a configuration flavor……Page 108
3.1.3 YAML and configuration that’s actually programming……Page 110
3.2 Starting to use Ruby to do more in your code……Page 112
3.2.1 Adding functionality to a controller……Page 114
3.2.2 Deploying the Rails helper files……Page 115
3.2.3 Adding functionality to models……Page 117
3.3.1 Converting legacy data to ActiveRecord……Page 120
3.3.2 The irb-based Rails application console……Page 124
3.4 Summary……Page 125
Part II Ruby building blocks……Page 128
Objects and variables……Page 130
4.1 From “things” to objects……Page 131
4.1.1 Introducing object-oriented programming……Page 132
4.1.2 I, object!……Page 133
4.1.3 Modeling objects more closely: the behavior of a ticket……Page 138
4.2 The innate behaviors of an object……Page 143
4.2.1 Identifying objects uniquely with the object_id method……Page 144
4.2.2 Querying an object’s abilities with the respond_to? method……Page 145
4.2.3 Sending messages to objects with the send method……Page 146
4.3.1 Required and optional arguments……Page 147
4.3.2 Default values for arguments……Page 148
4.3.3 Order of arguments……Page 149
4.4 Local variables and variable assignment……Page 150
4.4.1 Variable assignment in depth……Page 152
4.4.2 Local variables and the things that look like them……Page 154
4.5 Summary……Page 155
Organizing objects with classes……Page 156
5.1 Classes and instances……Page 157
5.1.1 A first class……Page 158
5.1.2 Instance variables and object state……Page 161
5.2 Setter methods……Page 165
5.2.1 The equal sign (=) in method names……Page 166
5.2.2 ActiveRecord properties and other = -method applications……Page 168
5.3 Attributes and the attr_* method family……Page 171
5.3.1 Automating the creation of attribute handlers……Page 172
5.3.2 Two (getter/setter) for one……Page 173
5.3.3 Summary of attr_* methods……Page 174
5.4.1 Classes are objects too!……Page 175
5.4.2 When, and why, to write a class method……Page 176
5.4.3 Class methods vs. instance methods, clarified……Page 178
5.4.4 The Class class and Class.new……Page 179
5.5.1 Basic usage of constants……Page 180
5.5.2 Reassigning vs. modifying constants……Page 181
5.6 Inheritance……Page 183
5.6.1 Inheritance and Rails engineering……Page 184
5.6.2 Nature vs. nurture in Ruby objects……Page 186
5.7 Summary……Page 188
Modules and program organization……Page 189
6.1 Basics of module creation and use……Page 190
6.1.1 A module encapsulating “stack-like-ness”……Page 192
6.1.2 Mixing a module into a class……Page 193
6.1.3 Leveraging the module further……Page 195
6.2.1 Illustrating the basics of method lookup……Page 198
6.2.2 Defining the same method more than once……Page 201
6.2.3 Going up the method search path with super……Page 203
6.3 Class/module design and naming……Page 205
6.3.1 Mix-ins and/or inheritance……Page 206
6.3.2 Modular organization in Rails source and boilerplate code……Page 208
6.4 Summary……Page 211
The default object (self) and scope……Page 212
7.1.1 Who gets to be self, and where……Page 214
7.1.2 Self as default receiver of messages……Page 219
7.1.3 Instance variables and self……Page 221
7.2.1 Global scope and global variables……Page 223
7.2.2 Local scope……Page 226
7.2.3 Scope and resolution of constants……Page 229
7.3.1 Private methods……Page 232
7.3.2 Private methods as ActionController access protection……Page 234
7.3.3 Protected methods……Page 236
7.4.1 Defining a top-level method……Page 238
7.4.2 Predefined (built-in) top-level methods……Page 239
7.5 Summary……Page 240
Control flow techniques……Page 241
8.1 Conditional code execution……Page 242
8.1.1 The if keyword and friends……Page 243
8.1.3 Case statements……Page 246
8.2.1 Unconditional looping with the loop method……Page 250
8.2.2 Conditional looping with the while and until keywords……Page 251
8.2.3 Looping based on a list of values……Page 253
8.3.1 The basics of yielding to a block……Page 254
8.3.2 Performing multiple iterations……Page 257
8.3.4 More about for……Page 258
8.4.1 Raising and rescuing exceptions……Page 260
8.4.2 Raising exceptions explicitly……Page 262
8.4.3 Creating your own exception classes……Page 263
8.5 Summary……Page 265
Part III Built-in classes and modules……Page 266
Built-in essentials……Page 268
9.1 Ruby’s literal constructors……Page 269
9.2 Recurrent syntactic sugar……Page 271
9.2.1 Special treatment of +=……Page 272
9.3 Methods that change their receivers (or don’t)……Page 273
9.3.1 Receiver-changing basics……Page 274
9.3.2 bang (!) methods……Page 275
9.3.3 Specialized and extended receiver-changing in ActiveRecord objects……Page 276
9.4 Built-in and custom to_* (conversion) methods……Page 277
9.4.1 Writing your own to_* methods……Page 278
9.5 Iterators reiterated……Page 279
9.6 Boolean states, Boolean objects, and nil……Page 280
9.6.1 True and false as states……Page 281
9.6.2 true and false as objects……Page 283
9.6.3 The special object nil……Page 284
9.7.1 Equality tests……Page 286
9.7.2 Comparisons and the Comparable module……Page 287
9.8 Listing an object’s methods……Page 288
9.8.1 Generating filtered and selective method lists……Page 289
9.9 Summary……Page 290
Scalar objects……Page 292
10.1.1 String basics……Page 293
10.1.2 String operations……Page 295
10.1.3 Comparing strings……Page 300
10.2.1 Key differences between symbols and strings……Page 302
10.2.2 Rails-style method arguments, revisited……Page 303
10.3.1 Numerical classes……Page 305
10.3.2 Performing arithmetic operations……Page 306
10.4 Times and dates……Page 307
10.5 Summary……Page 310
Collections, containers, and enumerability……Page 312
11.1 Arrays and hashes compared……Page 313
11.2.1 Creating a new array……Page 314
11.2.2 Inserting, retrieving, and removing array elements……Page 315
11.2.3 Combining arrays with other arrays……Page 318
11.2.4 Array transformations……Page 320
11.2.5 Array iteration, filtering, and querying……Page 321
11.2.6 Ruby lessons from ActiveRecord collections……Page 324
11.3 Hashes……Page 327
11.3.1 Creating a new hash……Page 328
11.3.2 Inserting, retrieving, and removing hash pairs……Page 329
11.3.3 Combining hashes with other hashes……Page 331
11.3.4 Hash transformations……Page 332
11.3.5 Hash iteration, filtering, and querying……Page 333
11.3.6 Hashes in Ruby and Rails method calls……Page 336
11.4 Collections central: the Enumerable module……Page 338
11.4.1 Gaining enumerability through each……Page 339
11.4.2 Strings as Enumerables……Page 341
11.5 Sorting collections……Page 342
11.5.1 Sorting and the Comparable module……Page 344
11.5.2 Defining sort order in a block……Page 345
11.6 Summary……Page 346
Regular expressions and regexp-based string operations……Page 347
12.1 What are regular expressions?……Page 348
12.2 Writing regular expressions……Page 349
12.2.1 The regular expression literal constructor……Page 350
12.2.2 Building a pattern……Page 351
12.3.1 Capturing submatches with parentheses……Page 354
12.3.2 Match success and failure……Page 356
12.4.1 Quantifiers and greediness……Page 358
12.4.2 Anchors and lookahead assertions……Page 361
12.4.3 Modifiers……Page 363
12.4.4 Converting strings and regular expressions to each other……Page 364
12.5 Common methods that use regular expressions……Page 366
12.5.2 String#split……Page 367
12.5.3 sub/sub! and gsub/gsub!……Page 368
12.5.4 grep……Page 369
12.6 Summary……Page 370
Ruby dynamics……Page 372
13.1 The position and role of singleton classes……Page 373
13.1.1 Where the singleton methods live……Page 374
13.1.2 Examining and modifying a singleton class directly……Page 375
13.1.3 Singleton classes on the method lookup path……Page 377
13.1.4 Class methods in (even more) depth……Page 380
13.2.1 eval……Page 382
13.2.3 The most useful eval: class_eval (a.k.a. module_eval)……Page 384
13.3.1 Proc objects……Page 386
13.3.2 Creating anonymous functions with the lambda keyword……Page 390
13.3.3 Code blocks, revisited……Page 391
13.3.4 Methods as objects……Page 392
13.4 Callbacks and hooks……Page 394
13.4.1 Intercepting unrecognized messages with method_missing……Page 395
13.4.2 Trapping include operations with Module#included……Page 396
13.4.3 Intercepting inheritance with Class#inherited……Page 398
13.5 Overriding and adding to core functionality……Page 400
13.5.1 A cautionary tale……Page 401
13.6 Summary……Page 402
Part IV Rails through Ruby, Ruby through Rails……Page 404
(Re)modeling the R4RMusic application universe……Page 406
14.1 Tracking the capabilities of an ActiveRecord model instance……Page 407
14.1.1 An overview of model instance capabilities……Page 408
14.1.2 Inherited and automatic ActiveRecord model behaviors……Page 409
14.1.3 Semi-automatic behaviors via associations……Page 413
14.2.1 Abstracting and adding models (publisher and edition)……Page 415
14.2.2 The instruments model and many-to-many relations……Page 417
14.2.3 Modeling for use: customer and order……Page 421
14.3 Summary……Page 425
Programmatically enhancing ActiveRecord models……Page 427
15.1 Soft vs. hard model enhancement……Page 428
15.1.1 An example of model-enhancement contrast……Page 429
15.2 Soft programmatic extension of models……Page 431
15.2.1 Honing the Work model through soft enhancements……Page 433
15.2.2 Modeling the customer’s business……Page 434
15.2.4 Ruby vs. SQL in the development of soft enhancements……Page 436
15.3.1 Prettification of string properties……Page 439
15.3.2 Calculating a work’s period……Page 444
15.3.3 The remaining business of the Customer……Page 449
15.4.1 Soft and hard class methods……Page 454
15.5 Summary……Page 456
Enhancing the controllers and views……Page 457
16.1 Defining helper methods for view templates……Page 459
16.1.1 Organizing and accessing custom helper methods……Page 460
16.1.2 The custom helper methods for R4RMusic……Page 462
16.2.1 Anatomy of a master template……Page 464
16.2.2 Using partials in the welcome view template……Page 465
16.3.1 The new face of the welcome action……Page 471
16.4.1 The login and signup partial templates……Page 473
16.4.2 Logging in and saving the session state……Page 474
16.4.3 Gate-keeping the actions with before_filter……Page 476
16.4.4 Implementing a signing-up facility……Page 479
16.4.5 Scripting customer logout……Page 480
16.5.1 The view_cart action and template……Page 481
16.5.2 Viewing and buying an edition……Page 483
16.5.4 Completing the order(s)……Page 484
16.6.1 From rankings to favorites……Page 485
16.6.2 The favorites feature in action……Page 487
16.7 Summary……Page 489
Techniques for exploring the Rails source code……Page 490
17.1 Exploratory technique 1: panning for info……Page 491
17.1.1 Sample info panning: belongs_to……Page 492
17.2.1 Choosing a starting point……Page 493
17.2.2 Choose among forks in the road intelligently……Page 494
17.2.3 On the trail of belongs_to……Page 495
17.2.4 A transliteration of belongs_to……Page 498
17.3 Exploratory technique 3: consulting the documentation……Page 499
17.3.1 A roadmap of the online Rails API documentation……Page 501
17.4 Summary……Page 504
appendix: Ruby and Rails installation and resources……Page 506
A.1 Online resources for Ruby and Rails……Page 507
A.2.1 One-Click Ruby and Instant Rails for Windows……Page 508
A.2.4 Installing the RubyGems package manager……Page 509
A.2.5 Installing Rails with RubyGems……Page 510
index……Page 512

Reviews

There are no reviews yet.

Be the first to review “Ruby for Rails: Ruby techniques for Rails developers”
Shopping Cart
Scroll to Top