hack01 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bdf74a9eb2389d4d982d668c5e89c74820c2f4115b0585279499bbee7c229be0
4
- data.tar.gz: 34070e6b913704030749bb3c0dc0d809f04f7d48a810fed088aa4063e9edd6b4
3
+ metadata.gz: 3e6a082da6b2c59065b83d42fd81838cba9fa4922e69f4664abbc272fccbb865
4
+ data.tar.gz: 496ab8d750f44ed7de4b7156639a42fe8774c354b34d8be1e3de13381233b52f
5
5
  SHA512:
6
- metadata.gz: 56f6d44cbc2bb453423a886688931d6d6d78cc09b172415414bbcf34422de6d579fcb84333683e677b7410bafdc1bc9a92029ea301f27a6eb07dc946bb48a7d2
7
- data.tar.gz: bec9f280b565e0c8e1d298ebc1eeaeb5edd03d0d90910e0abc435ae1fdd0017da183caaadb32920e2bb4f5c546b52330abb0e105741d337a922e0d595afab1cf
6
+ metadata.gz: aab3e59cd33738a43d914b634bd546a20d4c212fbe275e8cc45f81e2c5918b441da90a697684640150475bae81ff5eb46388c410e73e97a8394ee4a86a451923
7
+ data.tar.gz: 75e36f679bac4e35a1d60896a8d8d4a68276cddddf6a69b01e9bac39be8071ef96ea1a32efaf67bafcbcc93dd4497f898c371388565926223fae464f54c3866b
data/lib/hack01.rb CHANGED
@@ -8,20 +8,29 @@ class Hack01
8
8
  #
9
9
  # Arguments:
10
10
  # language: (String)
11
- #
12
- # Example:
13
- # >> Hack.info
14
- # => "Making a first ruby gem"
15
-
16
11
  def self.hi language = "english"
17
12
  translator = Translator.new language
18
13
  translator.hi
19
14
  end
20
15
 
16
+ # Example:
17
+ # >> Hack.info
18
+ # => "Making a first ruby gem"
21
19
  def self.info
22
20
  p "Making a first ruby gem"
23
21
  p "https://guides.rubygems.org/make-your-own-gem/"
24
22
  end
23
+
24
+ # Example
25
+ # >> a = Hack.new
26
+ # >> a.hello
27
+ # => "hello there"
28
+ def hello
29
+ p "hello there"
30
+ end
25
31
  end
26
32
 
27
33
  require 'hack01/translator'
34
+ require 'hack01/nth_prime'
35
+ require 'hack01/roman_numerals'
36
+ require 'hack01/space_age'
@@ -0,0 +1,36 @@
1
+ require "pry"
2
+ class Prime
3
+ class << self # means you don't have to use self.nth etc
4
+
5
+ def is_prime number_to_test
6
+ prime = true
7
+
8
+ number_to_test
9
+ arr = (2 .. (number_to_test - 1) ).to_a
10
+ arr.each do |n|
11
+ if (number_to_test % n) == 0
12
+ prime = false
13
+ end
14
+ end
15
+
16
+ return prime
17
+ end
18
+
19
+ def nth num
20
+
21
+ throw ArgumentError.new unless num > 0
22
+
23
+ primes = [2]
24
+ candidate = primes.last
25
+
26
+ while primes.length < num
27
+ candidate = candidate + 1
28
+ if is_prime candidate
29
+ primes.push candidate
30
+ end
31
+ end
32
+ # p primes
33
+ primes.last
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,209 @@
1
+ require "pry"
2
+
3
+ class Integer
4
+ # class Integer is the pre-defined class of Integer numbers.
5
+ # what we are doing below is extending the class by adding a new method.
6
+ # to see the built in methods for class of integer type something like `5.methods.sort`
7
+
8
+ ROMANS = {
9
+ "M" => 1000,
10
+ "CM" => 900,
11
+ "D" => 500,
12
+ "CD" => 400,
13
+ "C" => 100,
14
+ "XC" => 90,
15
+ "L" => 50,
16
+ "XL" => 40,
17
+ "X" => 10,
18
+ "IX" => 9,
19
+ "V" => 5,
20
+ "IV" => 4,
21
+ "I" => 1
22
+ }
23
+
24
+ def to_roman
25
+ number = self
26
+ # self is like `this` in javascript. in this example if we call this method thusly `5.to_roman` then self is 5
27
+ output = ""
28
+
29
+ # long way. would need lots of if / else statements.
30
+ # if number == 1
31
+ # output = "I"
32
+ # elsif number == 2
33
+ # output = "II"
34
+ # else
35
+ # output = "III"
36
+ # end
37
+
38
+ # clever ruby way - eaching through the hash defined above.
39
+ ROMANS.each do |key, value|
40
+ # output = output + key * ( number / value )
41
+ output << key * ( number / value )
42
+ number = number % value
43
+ end
44
+
45
+
46
+ output
47
+ end
48
+
49
+ end
50
+
51
+
52
+ # binding.pry
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
+ # require 'pry'
96
+ #
97
+ # class Integer
98
+ #
99
+ # ROMANS = {
100
+ # "M" => 1000,
101
+ # "CM" => 900,
102
+ # "D" => 500,
103
+ # "CD" => 400,
104
+ # "C" => 100,
105
+ # "XC" => 90,
106
+ # "L" => 50,
107
+ # "XL" => 40,
108
+ # "X" => 10,
109
+ # "IX" => 9,
110
+ # "V" => 5,
111
+ # "IV" => 4,
112
+ # "I" => 1
113
+ # }
114
+ #
115
+ # def to_roman
116
+ # number = self
117
+ # output = ""
118
+ #
119
+ # # if number == 1
120
+ # # output = "I"
121
+ # # elsif number == 2
122
+ # # output = "II"
123
+ # # else
124
+ # # output = "III"
125
+ # # end
126
+ #
127
+ # ROMANS.each do |key, value|
128
+ # output << key * ( number / value)
129
+ # number = number % value
130
+ # end
131
+ #
132
+ #
133
+ # output
134
+ # end
135
+ # end
136
+ #
137
+ #
138
+ #
139
+ # # binding.pry
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+ # class Integer
181
+ # ROMANS = {
182
+ # "M" => 1000,
183
+ # "CM" => 900,
184
+ # "D" => 500,
185
+ # "CD" => 400,
186
+ # "C" => 100,
187
+ # "XC" => 90,
188
+ # "L" => 50,
189
+ # "XL" => 40,
190
+ # "X" => 10,
191
+ # "IX" => 9,
192
+ # "V" => 5,
193
+ # "IV" => 4,
194
+ # "I" => 1
195
+ # }
196
+ #
197
+ #
198
+ # def to_roman
199
+ # int = self
200
+ # result = ""
201
+ #
202
+ # ROMANS.each do |sign, num|
203
+ # result << sign * ( int / num )
204
+ # int = int % num
205
+ # end
206
+ #
207
+ # result
208
+ # end
209
+ # end
@@ -0,0 +1,38 @@
1
+ require 'pry'
2
+ # Definition of class SpaceAge for Exercism
3
+ class SpaceAge
4
+ EARTH_YEAR_IN_SECONDS = 31_557_600
5
+ PLANETS = {
6
+ Earth: 1,
7
+ Mercury: 0.2408467,
8
+ Venus: 0.61519726,
9
+ Mars: 1.8808158,
10
+ Jupiter: 11.862615,
11
+ Saturn: 29.447498,
12
+ Uranus: 84.016846,
13
+ Neptune: 164.79132
14
+ }.freeze
15
+
16
+ def initialize(seconds)
17
+ @seconds = seconds
18
+ end
19
+
20
+ PLANETS.each do |planet, _year|
21
+ define_method("on_#{planet.downcase}") do
22
+ @seconds.to_f / EARTH_YEAR_IN_SECONDS / PLANETS[planet]
23
+ end
24
+ end
25
+
26
+ def method_missing(name, *args, &block)
27
+ # first time using method_missing
28
+ # super basically checks if the method exists in the class's ancestor
29
+ # (if any)
30
+ super
31
+ raise "'#{name}' is not a method of the class #{self.class}"
32
+ end
33
+
34
+ def respond_to_missing?(*)
35
+ p 'not sure what to do here but'
36
+ p "rubocop recommends a 'respond_to_missing?' method"
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hack01
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Coote
@@ -19,6 +19,9 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - bin/hack01
21
21
  - lib/hack01.rb
22
+ - lib/hack01/nth_prime.rb
23
+ - lib/hack01/roman_numerals.rb
24
+ - lib/hack01/space_age.rb
22
25
  - lib/hack01/translator.rb
23
26
  homepage: http://rubygems.org/gems/hack01
24
27
  licenses: