Ruby Pocket Reference

Ruby Pocket Reference

Language: English

Pages: 224

ISBN: 1491926015

Format: PDF / Kindle (mobi) / ePub


Updated for Ruby 2.2, this handy reference offers brief yet clear explanations of Ruby’s core elements—from operators to blocks to documentation creation—and highlights the key features you may work with every day. Need to know the correct syntax for a conditional? Forgot the name of that String method? This book is organized to help you find the facts fast.

Ruby Pocket Reference, 2nd Edition is ideal for experienced programmers who are new to Ruby. Whether you’ve come to Ruby because of Rails, or you want to take advantage of this clean, powerful, and expressive language for other applications, this reference will help you easily pinpoint the information you need.

You’ll find detailed reference material for:

  • Keywords, operators, comments, numbers, and symbols
  • Variables, pre-defined global variables, and regular expressions
  • Conditional statements, method use, classes, and exception handling
  • Methods for the BasicObject, Object, Kernel, String, Array, and Hash classes
  • Time formatting directives
  • New syntax since Ruby 1.9

 

 

 

 

 

 

 

 

 

 

 

 

 

class, so only one copy of a class variable exists for a given class. In Ruby, a class variable is prefixed by two at signs (@@). You must initialize a class attribute before you use it, such as @@times = 0. class Repeat @@total = 0 def initialize( string, times ) @string = string @times = times end def repeat @@total += @times return @string * @times end def total "Total times, so far: " + @@total.to_s end end data = Repeat.new( "ack ", 8 ) ditto = Repeat.new( "Again! ", 5 ) ditty = Repeat.new(

absolute path), and then store the value of the directory path in a variable as follows: Dir.chdir( "/Users/penelope" ) home = Dir.pwd # => "/Users/penelope/" p home # => "/Users/penelope" If you need a directory, create it with mkdir; later on, delete it with rmdir (or delete, a synonym of rmdir): Dir.mkdir( "/Users/herman/sandbox" ) Dir.rmdir( "/Users/herman/sandbox" ) You can also set permissions (where the mask 755 sets permissions owner, group, world [anyone] to rwxr-xr-x where r = read, w

from the result. catch(symbol) { | | block } If a throw is executed, Ruby searches up its stack for a catch block with a tag corresponding to the throw’s symbol. chomp [or] chomp(string) Equivalent to $_ = $_.chomp(string). See String#chomp. chomp! [or] chomp!(string) Equivalent to $_.chomp!(string). See String#chomp!. chop Equivalent to ($_.dup).chop!, except nil is never returned. See String#chop!. chop! Equivalent to $_.chop!. eval(string [, binding [, filename [, lineno]]]) Evaluates the

strings in self. If passed a pattern or a string, only the strings matching the pattern or starting with the string are considered. array.assoc(obj) Searches through an array whose elements are also arrays comparing obj with the first element of each contained array using obj.==. Returns the first contained array that matches (that is, the first associated array), or nil if no match is found. See also Array#rassoc. array.at(index) Returns the element at index. A negative index counts from the

absolute position. A ASCII string (space padded, count is width). a ASCII string (null padded, count is width). B Bit string (descending bit order). b Bit string (ascending bit order). C Unsigned char. c Char. D, d Double-precision float, native format. 102 | Ruby Pocket Reference Table 19. Array pack directives (continued) Directive Description E Double-precision float, little-endian byte order. e Single-precision float, little-endian byte order. F, f Single-precision

Download sample

Download