Michael James Fitzgerald
Format: PDF / Kindle (mobi) / ePub
You don't have to know everything about a car to drive one, and you don't need to know everything about Ruby to start programming with it. Written for both experienced and new programmers alike, Learning Ruby is a just-get-in-and-drive book -- a hands-on tutorial that offers lots of Ruby programs and lets you know how and why they work, just enough to get you rolling down the road.
Interest in Ruby stems from the popularity of Rails, the web development framework that's attracting new devotees and refugees from Java and PHP. But there are plenty of other uses for this versatile language. The best way to learn is to just try the code! You'll find examples on nearly every page of this book that you can imitate and hack. Briefly, this book:
- Outlines many of the most important features of Ruby
- Demonstrates how to use conditionals, and how to manipulate strings in Ruby. Includes a section on regular expressions
- Describes how to use operators, basic math, functions from the Math module, rational numbers, etc.
- Talks you through Ruby arrays, and demonstrates hashes in detail
- Explains how to process files with Ruby
- Discusses Ruby classes and modules (mixins) in detail, including a brief introduction to object-oriented programming (OOP)
- Introduces processing XML, the Tk toolkit, RubyGems, reflection, RDoc, embedded Ruby, metaprogramming, exception handling, and other topics
- Acquaints you with some of the essentials of Rails, and includes a short Rails tutorial.
Each chapter concludes with a set of review questions, and appendices provide you with a glossary of terms related to Ruby programming, plus reference material from the book in one convenient location. If you want to take Ruby out for a drive, Learning Ruby holds the keys.
a few of them, so you can get started. Math also has two constants, along with its methods.To find out what constants Math (or any other module or class) has defined, use reflection by invoking the constants method: Math. constants # => ["E", "PI"] Let’s check what the values of these constants, Euler and π, are. 88 | Chapter 5: Math print Math::E # => 2.71828182845905 print Math::PI # => 3.14159265358979 The Math.exp function returns Euler to the power of x. Math.exp(1) # =>
parameters (going back to English): months[5, 3] = "May", "June", "July" # => [nil, "January", "February", "March", "April", "May", "June" , "July" , "August" , "September", "October", "November", "December"] As a String You can extract the elements of an array as a single string using to_s. to_s is com- mon to many classes. greeting = [ "Hello! ", "Bonjour! ", "Guten Tag!" ] puts greeting.to_s # => Hello! Bonjour! Guten Tag! Use join to smash all the elements together into a single
system: system "echo 'Hello, Matz!'" Try this with and without single quotes ('), where shown. You can also submit each part of a command separately, as an argument to system: system "echo", "Hello,", "Matz!" The exec command is similar to system, but it replaces the current process and, after the command is finished, exits—not always what you want to do. Appending a String Append one string to another with the + method: puts "Hello, " + "Matz!" You can also append a string with the <<
gives you the last value in irb (conf. last_value also returns the last value). Return the instance variables with Object’s instance_variables: irb(main):006:0> instance_variables => ["@friend_2", "@friend_1"] Kernel’s method global_variables returns all the global variables, including $ship. irb(main):007:0> global_variables => ["$-p", "$:", "$FILENAME", "$defout", "$,", "$`", "$binding", "$-v", "$stdin", "$PROGRAM_NAME", "$\"", "$?", "$\\", "$=", "$-d", "$>", "$&", "$-F", "$-a",
system and 24-pound heart regulate circulation throughout its tremendous body: in the upper neck, a pressure- regulation system prevents excess blood flow to the brain when the giraffe lowers its head to drink, while thick sheaths of skin on the lower legs maintain high extravas- cular pressure to compensate for the weight of the fluid pressing down on them. Giraffes travel in herds comprised of about a dozen females, one or two males, and their young.Other males may travel alone, in pairs,