Commit 385072e5 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596523695

0 → 120000
+1 −0
Changes for 9780596523695: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596523695
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# Ruby Cookbook, by Lucas Carlson
	  
[![Ruby Cookbook, by Lucas Carlson](http://akamaicovers.oreilly.com/images/9780596523695/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596523696//)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
+50 −0
Changes for Ruby Cookbook Source/00-Preface.rb: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
1 + 2                                                 # => 3

# On a long line, the expected value goes on a new line:
Math.sqrt(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
# => 7.41619848709566
#---
puts "This string is self-referential."
# This string is self-referential.
#---
$ irb
irb(main):001:0> 1 + 2
=> 3
irb(main):002:0> Math.sqrt(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
=> 7.41619848709566
irb(main):003:0> puts "This string is self-referential."
This string is self-referential.
=> nil
#---
$ irb
irb(main):001:0> 1 + 2      # => 3
=> 3
irb(main):002:0>
irb(main):003:0* # On a long line, the expected value goes on a new line:
irb(main):004:0* Math.sqrt(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
=> 7.41619848709566
irb(main):005:0> # => 7.41619848709566
irb(main):006:0*
irb(main):007:0* puts "This string is self-referential."
This string is self-referential.
=> nil
irb(main):008:0> # This string is self-referential.
#---
#!/usr/bin/ruby -w
# sample_ruby_file.rb: A sample file

1 + 2
Math.sqrt(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
puts "This string is self-referential."
#---
$ ruby sample_ruby_file.rb
This string is self-referential.
#---
$ ruby setup.rb
#---
$ gem install rails --include-dependencies
#---
$ ri Array                                  # A class
$ ri Array.new                              # A class method
$ ri Array#compact                          # An instance method
#---
+56 −0
Changes for Ruby Cookbook Source/01 - Strings/00 - Introduction: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
string = 'My first string'
#---
=> "My first string"
#---
string = 'My first string'                   # => "My first string"
#---
string.length                                # => 15
#---
string.length()                              # => 15
#---
string.count 'i'                             # => 2   # "i" occurs twice.
string.count('i')                            # => 2
#---
string.length.next                           # => 16
#---
french_string = "il \xc3\xa9tait une fois"   # => "il \303\251tait une fois"
#---
french_string.length                         # => 18
#---
puts "This string\ncontains a newline"
# This string
# contains a newline
#---
puts 'it may look like this string contains a newline\nbut it doesn\'t'
# it may look like this string contains a newline\nbut it doesn't

puts 'Here is a backslash: \\ '
# Here is a backslash: \
#---
long_string = <<EOF
Here is a long string
With many paragraphs
EOF
# => "Here is a long string\nWith many paragraphs\n"

puts long_string
# Here is a long string
# With many paragraphs
#---
string                                    # => "My first string"
string.slice(3, 5)                        # => "first"
#---
string[3].chr + string[4].chr + string[5].chr + string[6].chr +
 string[7].chr
# => "first"
#---
string[3, 5]                             # => "first"
#---
string.upcase                            # => "MY FIRST STRING"
string                                   # => "My first string"
string.upcase!                           # => "MY FIRST STRING"
string                                   # => "MY FIRST STRING"
#---
string.empty?                            # => false
string.include? 'MY'                     # => true
#---
+25 −0
Changes for Ruby Cookbook Source/01 - Strings/01 - Building a String From Parts.rb: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
hash = { "key1" => "val1", "key2" => "val2" }
string = ""
hash.each { |k,v| string << "#{k} is #{v}\n" } 
puts string
# key1 is val1
# key2 is val2
#---
string = ""
hash.each { |k,v| string << k << " is " << v << "\n" } 
#---
puts hash.keys.join("\n") + "\n"
# key1
# key2
#---
data = ['1', '2', '3']
s = ''
data.each { |x| s << x << ' and a '}
s                                             # => "1 and a 2 and a 3 and a "

data.join(' and a ')                          # => "1 and a 2 and a 3"
#---
s = ""
data.each_with_index { |x, i| s << x; s << "|" if i < data.length-1 }
s                                             # => "1|2|3"
#---
Loading