The Book of Ruby: A Hands-On Guide for the Adventurous

The Book of Ruby: A Hands-On Guide for the Adventurous

Huw Collingbourne

Language: English

Pages: 400

ISBN: 1593272944

Format: PDF / Kindle (mobi) / ePub

The Book of Ruby: A Hands-On Guide for the Adventurous

Huw Collingbourne

Language: English

Pages: 400

ISBN: 1593272944

Format: PDF / Kindle (mobi) / ePub


Ruby is famous for being easy to learn, but most users only scratch the surface of what it can do. While other books focus on Ruby's trendier features, The Book of Ruby reveals the secret inner workings of one of the world's most popular programming languages, teaching you to write clear, maintainable code.

You'll start with the basics—types, data structures, and control flows—and progress to advanced features like blocks, mixins, metaclasses, and beyond. Rather than bog you down with a lot of theory, The Book of Ruby takes a hands-on approach and focuses on making you productive from day one. As you follow along, you'll learn to:

  • Leverage Ruby's succinct and flexible syntax to maximize your productivity
  • Balance Ruby's functional, imperative, and object-oriented features
  • Write self-modifying programs using dynamic programming techniques
  • Create new fibers and threads to manage independent processes concurrently
  • Catch and recover from execution errors with robust exception handling
  • Develop powerful web applications with the Ruby on Rails framework

Each chapter includes a "Digging Deeper" section that shows you how Ruby works under the hood, so you'll never be caught off guard by its deceptively simple scoping, multithreading features, or precedence rules.

Whether you're new to programming or just new Ruby, The Book of Ruby is your guide to rapid, real-world software development with this unique and elegant language.

Beginning Perl for Bioinformatics

Ace the Programming Interview: 160 Questions and Answers for Success

Functional Programming Patterns in Scala and Clojure: Write Lean Programs for the JVM

Talend for Big Data

API Design for C++

Learning Neo4j

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Classes In Ruby it is not obligatory to define a class all in one place. If you want, you can define a single class in separate parts of your program. When a class descends from a specific superclass, each subsequent partial (or open) class definition may optionally repeat the superclass in its definition using the < operator. 30 Chapter 2 Here I create one class, A, and another that descends from it, B: partial_classes.rb class A def a puts( "a" ) end end class B < A def ba1 puts( "ba1" )

accessor methods): p(h1.default) h1.default = 'A mysterious place' Copying a Hash As with an array, you can assign one Hash variable to another, in which case both variables will refer to the same hash, and a change made using either variable will affect that hash: hash2.rb h4 = h1 h4['room1']='A new Room' puts(h1['room1']) #=> 'A new Room' If you want the two variables to refer to the same items in different Hash objects, use the clone method to make a new copy: h5 = h1.clone h5['room1'] =

if such a method exists. In brief then, the new method is the constructor, and the initialize method is used to initialize the values of any variables immediately after an object is created. But why can’t you just write your own new method and initialize variables in it? Well, let’s try that: new.rb class MyClass def initialize( aStr ) @avar = aStr end def MyClass.new( aStr ) super @anewvar = aStr.swapcase end end ob = MyClass.new( "hello world" ) puts( ob ) puts( ob.class ) Here, I’ve written

returns "goodbye" # returns nil My own programming prejudice is to write code that is clear and unambiguous whenever possible. For that reason, whenever I plan to use the value returned by a method, I prefer to specify it using the return keyword; only when I do not plan to use the returned value do I omit this. However, this is not obligatory—Ruby leaves the choice to you. P a s s i ng A r g u m en t s a n d Ret u r n i n g V a l u es 123 Returning Multiple Values But what about those

that row by obtaining the row length and using the Integer’s times method with that value. As a result, it displays the data from all three rows: multiarr.each{ |arr| multiarr[0].length.times{|i| puts(arr[i]) } } The third version reverses these operations: The outer block iterates along the length of row 0, and the inner block obtains the item at index i in each row. Once again, this displays the data from all three rows: multiarr[0].length.times{|i| multiarr.each{ |arr| puts(arr[i]) } }

Download sample

Download