-
Notifications
You must be signed in to change notification settings - Fork 0
/
eloquent_ruby.rb
110 lines (94 loc) · 3.05 KB
/
eloquent_ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module IfElse
class FancyIfElse
#fancy if/else
#if morning is true then 'good morning', else 'good evening'
morning = true
greetings = morning ? 'Good morning!' : 'Good Evening!'
puts greetings
end
end
module Collections
class QuickStringArray
words = %w{ this is a quick way to create an array of strings!}
words.each { |word| puts word }
end
class FindIndex
#returns the index at which the 'word' was found
words = %w{ this is a quick way to create an array of strings!}
puts words.find_index { |this_word| 'create' == this_word}
end
class MapArray
#map block returns a separate array 'words' array does not get affected
words = %w{ this is a quick way to create an array of strings!}
puts words.map { |word| word.size }
puts words.map { |word| word.upcase }
end
class InjectArray
#inject accepts a parameter, the return of inject will be passed into the next
#loop until it has ran out of elements
words = %w{ this is a quick way to create an array of strings!}
puts words.inject(0.0){ |result, word| word.size + result }
end
class SortArray
#Remember, the Ruby convention is that an exclamation point
#at the end of a method name indicates that the method is the dangerous or surprising
#version of a pair of methods
words = %w{ this is a quick way to create an array of strings!}
words.sort #this will not change the array, but will return a sorted version of it
words.sort!#this will actually change the array!
end
end
module Strings
class Quote
#allows for punctuation without the need of escaping (/)
#you can also use {} or () or <> or [] and any other special characters
#such as $string in here!$ to avoid conflicts
str = %q{"stop", she said, "I can't live without 's and s."}
#uppercase Q equates to "" allowing #{foo}
foo = 'john'
str = %Q{"Hi" he said, "my name is #{foo}. What is your name?"}
end
class MultiLine
a_multiline_string = "a multi-line
string"
puts another_one = %q{another multi-line
string}
end
class HereDocument
#Couldn't get this one to work
end
class Strip
' hello '.strip #will return 'hello'
end
class Chomp
"hello\n".chomp #will return "hello"
end
class Sub
#only subs first word it finds
'It is warm outside'.sub( 'warm', 'cold') #will return 'It is cold outside'
end
class GSub
#will sub all instances of the string
'yes yes'.gsub( 'yes', 'no' ) #will return 'no no'
end
class Split
#returns an array with elements of each string
'It was a dark and storm night'.split
'Bill:Shakespeare:Playwright:Globe'.split( ':' )
end
class ExclaimationPoint
#these will change the original string instead of returning
title = 'It was a dark and stormy night'
title.sub!( 'dark', 'bright' )
title.sub!( 'stormy', 'clear' )
puts title #'It was a bright and clear night'
end
class Each
author = 'Clarke'
author.each_char { |c| puts c }
end
class Ranges
"abcde"[3] #returns 'd'
"abcde"[3..4] #returns 'de'
end
end