rubykeyword 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rubykeyword.rb +26 -51
  2. data/lib/settings.yaml +350 -0
  3. metadata +5 -4
@@ -1,63 +1,38 @@
1
+ require 'yaml'
2
+
1
3
  class String
2
4
 
3
- #Reference : http://ruby-doc.org/docs/keywords/1.9/
4
- KEYWORDS = {
5
- "BEGIN" => "Designates, via code block, code to be executed unconditionally before sequential execution of the program begins. Sometimes used to simulate forward references to methods.",
6
- "END" => "Designates, via code block, code to be executed just prior to program termination.",
7
- "__ENCODING__" => "The current default encoding, as an Encoding instance.",
8
- "__END__" => "Denotes the end of the regular source code section of a program file. Lines below __END__ will not be executed. Those lines will be available via the special filehandle DATA. The following code will print out two stanzas of personal information. Note that __END__ has to be flush left, and has to be the only thing on its line.",
9
- "__FILE__" => "The name of the file currently being executed, including path relative to the directory where the application was started up (or the current directory, if it has been changed). The current file is, in some cases, different from the startup file for the running application, which is available in the global variable",
10
- "__LINE__" => "The line number, in the current source file, of the current line.",
11
- "alias" => "Creates an alias or duplicate method name for a given method. The original method continues to be accessible via the alias, even if it is overriden. Takes two method-name arguments (which can be represented by strings or symbols but can also be the bare names themselves).",
12
- "and" => "Boolean and operator. Differs from && in that and has lower precedence.",
13
- "begin" => "Together with end, delimits what is commonly called a “begin” block (to distinguish it from the Proc type of code block). A “begin” block allows the use of while and until in modifier position with multi-line statements",
14
- "break" => "Causes unconditional termination of a code block or while or until block, with control transfered to the line after the block. If given an argument, returns that argument as the value of the terminated block.",
15
- "case" => "The case statement operator. Case statements consist of an optional condition, which is in the position of an argument to case, and zero or more when clauses. The first when clause to match the condition (or to evaluate to Boolean truth, if the condition is null) “wins”, and its code stanza is executed. The value of the case statement is the value of the successful when clause, or nil if there is no such clause.",
16
- "class" => "Opens a class definition block. Takes either a constant name or an expression of the form << object. In the latter case, opens a definition block for the singleton class of object.",
17
- "def" => "Paired with a terminating end, constitutes a method definition. Starts a new local scope; local variables in existence when the def block is entered are not in scope in the block, and local variables created in the block do not survive beyond the block.",
18
- "defined?" => "defined? expression tests whether or not expression refers to anything recognizable (literal object, local variable that has been initialized, method name visible from the current scope, etc.). The return value is nil if the expression cannot be resolved. Otherwise, the return value provides information about the expression.",
19
- "do" => "Paired with end, can delimit a code block",
20
- "else" => "The else keyword denotes a final conditional branch. It appears in connection with if, unless, and case, and rescue. (In the case of rescue, the else branch is executed if no exception is raised.) The else clause is always the last branch in the entire statement, except in the case of rescue where it can be followed by an ensure clause.",
21
- "elsif" => "Introduces a branch in a conditional (if or unless) statement. Such a statement can contain any number of elsif branches, including zero.",
22
- "end" => "Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct.",
23
- "ensure" => "Marks the final, optional clause of a begin/end block, generally in cases where the block also contains a rescue clause. The code in the ensure clause is guaranteed to be executed, whether control flows to the rescue block or not.",
24
- "false" => "false denotes a special object, the sole instance of FalseClass. false and nil are the only objects that evaluate to Boolean falsehood in Ruby (informally, that cause an if condition to fail.)",
25
- "for" => "A loop constructor, used with in:",
26
- "if" => "Ruby’s basic conditional statement constructor. if evaluates its argument and branches on the result. Additional branches can be added to an if statement with else and elsif.",
27
- "in" => "See for.",
28
- "module" => "Opens a module definition block. Takes a constant (the name of the module) as its argument. The definition block starts a new local scope; existing variables are not visible inside the block, and local variables created in the block do not survive the end of the block.",
29
- "next" => "Bumps an iterator, or a while or until block,to the next iteration, unconditionally and without executing whatever may remain of the block.",
30
- "nil" => "A special “non-object”. nil is, in fact, an object (the sole instance of NilClass), but connotes absence and indeterminacy. nil and false are the only two objects in Ruby that have Boolean falsehood (informally, that cause an if condition to fail).",
31
- "not" => "Boolean negation.",
32
- "or" => "Boolean or. Differs from || in that or has lower precedence.",
33
- "redo" => "Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.",
34
- "rescue" => "Designates an exception-handling clause. Can occur either inside a begin<code>/<code>end block, inside a method definition (which implies begin), or in modifier position (at the end of a statement).",
35
- "retry" => "Inside a rescue clause, retry causes Ruby to return to the top of the enclosing code (the begin keyword, or top of method or block) and try executing the code again.",
36
- "return" => "Inside a method definition, executes the ensure clause, if present, and then returns control to the context of the method call. Takes an optional argument (defaulting to nil), which serves as the return value of the method. Multiple values in argument position will be returned in an array.",
37
- "self" => "self is the current object and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context.",
38
- "super" => "Called from a method, searches along the method lookup path (the classes and modules available to the current object) for the next method of the same name as the one being executed. Such method, if present, may be defined in the superclass of the object’s class, but may also be defined in the superclass’s superclass or any class on the upward path, as well as any module mixed in to any of those classes.",
39
- "then" => "Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.",
40
- "true" => "The sole instance of the special class TrueClass. true encapsulates Boolean truth; however, <emph>all</emph> objects in Ruby are true in the Boolean sense (informally, they cause an if test to succeed), with the exceptions of false and nil.",
41
- "undef" => "Undefines a given method, for the class or module in which it’s called. If the method is defined higher up in the lookup path (such as by a superclass), it can still be called by instances classes higher up.",
42
- "unless" => "The negative equivalent of if.",
43
- "until" => "The inverse of while : executes code until a given condition is true, i.e., while it is not true. The semantics are the same as those of while; see while.",
44
- "when" => "See case.",
45
- "while" => "while takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.",
46
- "yield" => "Called from inside a method body, yields control to the code block (if any) supplied as part of the method call. If no code block has been supplied, calling yield raises an exception.",
47
- }
5
+ def set_keywords
6
+ @keywords = YAML::load( File.open( "#{File.expand_path File.dirname(__FILE__)}/settings.yaml" ) )
7
+ end
48
8
 
49
- def keyword
50
- return true if KEYWORDS.keys.include? self
51
- return false
9
+ def keyword?
10
+ set_keywords
11
+ return (@keywords.keys.include? self)
52
12
  end
53
13
 
54
14
  def define
55
- if keyword
56
- return KEYWORDS[self]
15
+ set_keywords
16
+ return keyword? ? @keywords[self]['definition'] : false
17
+ end
18
+
19
+
20
+ def example_string
21
+ set_keywords
22
+ if keyword?
23
+ return @keywords[self].has_key?('example') ? @keywords[self]['example'].join("\n") : "No example"
24
+ else
25
+ return false
26
+ end
27
+ end
28
+
29
+ def example
30
+ set_keywords
31
+ if keyword?
32
+ puts @keywords[self].has_key?('example') ? @keywords[self]['example'].join("\n") : "No example"
57
33
  else
58
34
  return false
59
35
  end
60
36
  end
61
37
 
62
38
  end
63
-
@@ -0,0 +1,350 @@
1
+ #Reference : http://ruby-doc.org/docs/keywords/1.9/
2
+
3
+ "BEGIN":
4
+ definition: "Designates, via code block, code to be executed unconditionally before sequential execution of the program begins. Sometimes used to simulate forward references to methods."
5
+ example:
6
+ - "puts times_3(gets.to_i)"
7
+ - "BEGIN {"
8
+ - " def times_3(n)"
9
+ - " n * 3"
10
+ - " end"
11
+ - "}"
12
+
13
+ "END":
14
+ definition: "Designates, via code block, code to be executed just prior to program termination."
15
+ example:
16
+ - " DATA.each do |line|"
17
+ - " first, last, phone, email = line.split('|')"
18
+ - " puts <<-EOM"
19
+ - " First name: #{first}"
20
+ - " Last name: #{last}"
21
+ - " Phone: #{phone}"
22
+ - " Email: #{email}"
23
+ - " EOM"
24
+ - " end"
25
+ - "__END__"
26
+ - " "
27
+ - "David|Black|123-456-7890|dblack@..."
28
+ - "Someone|Else|321-888-8888|someone@else"
29
+
30
+ "__ENCODING__":
31
+ definition: "The current default encoding, as an Encoding instance."
32
+
33
+
34
+ "__END__":
35
+ definition: "Denotes the end of the regular source code section of a program file. Lines below __END__ will not be executed. Those lines will be available via the special filehandle DATA. The following code will print out two stanzas of personal information. Note that __END__ has to be flush left, and has to be the only thing on its line."
36
+
37
+ "__FILE__":
38
+ definition: "The name of the file currently being executed, including path relative to the directory where the application was started up (or the current directory, if it has been changed). The current file is, in some cases, different from the startup file for the running application, which is available in the global variable"
39
+
40
+ "__LINE__":
41
+ definition: "The line number, in the current source file, of the current line."
42
+
43
+ "alias":
44
+ definition: "Creates an alias or duplicate method name for a given method. The original method continues to be accessible via the alias, even if it is overriden. Takes two method-name arguments (which can be represented by strings or symbols but can also be the bare names themselves)."
45
+ example:
46
+ - "class Person"
47
+ - " def name=(name)"
48
+ - " puts 'Naming your person #{name}!'"
49
+ - " @name = name"
50
+ - " end"
51
+ - " "
52
+ - " alias full_name= name="
53
+ - "end"
54
+ - " "
55
+ - "p = Person.new"
56
+ - "p.name = 'David' # Naming your person David!"
57
+ - " "
58
+ - "class Person"
59
+ - " def name=(name)"
60
+ - " puts 'Please use full_name='"
61
+ - " end"
62
+ - "end"
63
+ - " "
64
+ - "p.name = 'Joe' # Please use full_name="
65
+ - "p.full_name = 'Joe' # Naming your person Joe!"
66
+
67
+ "and":
68
+ definition: "Boolean and operator. Differs from && in that and has lower precedence."
69
+ example:
70
+ - "if a == 1 and b == 2"
71
+ - " puts 'foo'"
72
+ - "end"
73
+
74
+ "begin":
75
+ definition: "Together with end, delimits what is commonly called a “begin” block (to distinguish it from the Proc type of code block). A “begin” block allows the use of while and until in modifier position with multi-line statements"
76
+ example:
77
+ - "begin"
78
+ - " i += 1"
79
+ - " puts i"
80
+ - "end until i == 10"
81
+
82
+ "break":
83
+ definition: "Causes unconditional termination of a code block or while or until block, with control transfered to the line after the block. If given an argument, returns that argument as the value of the terminated block."
84
+ example:
85
+ - "result = File.open('lines.txt') do |fh|"
86
+ - " fh.each do |line|"
87
+ - " break line if my_regex.match(line)"
88
+ - " end"
89
+ - " nil"
90
+ - "end"
91
+
92
+ "case":
93
+ definition: "The case statement operator. Case statements consist of an optional condition, which is in the position of an argument to case, and zero or more when clauses. The first when clause to match the condition (or to evaluate to Boolean truth, if the condition is null) “wins”, and its code stanza is executed. The value of the case statement is the value of the successful when clause, or nil if there is no such clause."
94
+ example:
95
+ - "case x"
96
+ - "when 1,2,3"
97
+ - " puts '1, 2, or 3'"
98
+ - "when 10"
99
+ - " puts '10'"
100
+ - "else"
101
+ - " puts 'Some other number'"
102
+ - "end"
103
+
104
+ "class":
105
+ definition: "Opens a class definition block. Takes either a constant name or an expression of the form << object. In the latter case, opens a definition block for the singleton class of object."
106
+ example:
107
+ - "class Person"
108
+ - " def self.species"
109
+ - " 'Homo sapiens'"
110
+ - " end"
111
+ - "end"
112
+
113
+ "def":
114
+ definition: "Paired with a terminating end, constitutes a method definition. Starts a new local scope; local variables in existence when the def block is entered are not in scope in the block, and local variables created in the block do not survive beyond the block."
115
+ example:
116
+ - "def method_name(parameter1)"
117
+ - " return 'This is your parameter #{parameter1}'"
118
+ - "end"
119
+
120
+ "defined?":
121
+ definition: "defined? expression tests whether or not expression refers to anything recognizable (literal object, local variable that has been initialized, method name visible from the current scope, etc.). The return value is nil if the expression cannot be resolved. Otherwise, the return value provides information about the expression."
122
+ example:
123
+ - "def x; end"
124
+ - "p defined?(x) # 'method'"
125
+
126
+ "do":
127
+ definition: "Paired with end, can delimit a code block"
128
+ example:
129
+ - "array.each do |element|"
130
+ - " puts element * 10"
131
+ - "end"
132
+
133
+ "else":
134
+ definition: "The else keyword denotes a final conditional branch. It appears in connection with if, unless, and case, and rescue. (In the case of rescue, the else branch is executed if no exception is raised.) The else clause is always the last branch in the entire statement, except in the case of rescue where it can be followed by an ensure clause."
135
+ example:
136
+ - "if x == 0"
137
+ - " puts 'Zero'"
138
+ - "elif x == 1"
139
+ - " puts 'One'"
140
+ - "else"
141
+ - " puts 'Not Zero or One'"
142
+ - "end"
143
+
144
+ "elsif":
145
+ definition: "Introduces a branch in a conditional (if or unless) statement. Such a statement can contain any number of elsif branches, including zero."
146
+ example:
147
+ - "if x == 0"
148
+ - " puts 'Zero'"
149
+ - "elif x == 1"
150
+ - " puts 'One'"
151
+ - "else"
152
+ - " puts 'Not Zero or One'"
153
+ - "end"
154
+
155
+ "end":
156
+ definition: "Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct."
157
+ example:
158
+ - "[1,2,3].each do |i|"
159
+ - " puts i"
160
+ - "end"
161
+
162
+ "ensure":
163
+ definition: "Marks the final, optional clause of a begin/end block, generally in cases where the block also contains a rescue clause. The code in the ensure clause is guaranteed to be executed, whether control flows to the rescue block or not."
164
+ example:
165
+ - "begin"
166
+ - " 1/0"
167
+ - "rescue ZeroDivisionError"
168
+ - " puts 'Can't do that!'"
169
+ - "ensure"
170
+ - " puts 'That was fun!'"
171
+ - "end"
172
+
173
+ "false":
174
+ definition: "false denotes a special object, the sole instance of FalseClass. false and nil are the only objects that evaluate to Boolean falsehood in Ruby (informally, that cause an if condition to fail.)"
175
+
176
+ "for":
177
+ definition: "A loop constructor, used with in:"
178
+ example:
179
+ - "for a in [1,2,3,4,5] do"
180
+ - " puts a * 10"
181
+ - "end"
182
+
183
+ "if":
184
+ definition: "Ruby’s basic conditional statement constructor. if evaluates its argument and branches on the result. Additional branches can be added to an if statement with else and elsif."
185
+ example:
186
+ - "if x == 0"
187
+ - " puts 'Zero'"
188
+ - "elif x == 1"
189
+ - " puts 'One'"
190
+ - "else"
191
+ - " puts 'Not Zero or One'"
192
+ - "end"
193
+
194
+ "in":
195
+ definition: "See for."
196
+ example:
197
+ - "for a in [1,2,3,4,5] do"
198
+ - " puts a * 10"
199
+ - "end"
200
+
201
+ "module":
202
+ definition: "Opens a module definition block. Takes a constant (the name of the module) as its argument. The definition block starts a new local scope; existing variables are not visible inside the block, and local variables created in the block do not survive the end of the block."
203
+
204
+ "next":
205
+ definition: "Bumps an iterator, or a while or until block,to the next iteration, unconditionally and without executing whatever may remain of the block."
206
+ example:
207
+ - "[0,1,2,3,4].each do |n|"
208
+ - " next unless n > 2"
209
+ - " puts n"
210
+ - "end"
211
+
212
+ "nil":
213
+ definition: "A special “non-object”. nil is, in fact, an object (the sole instance of NilClass), but connotes absence and indeterminacy. nil and false are the only two objects in Ruby that have Boolean falsehood (informally, that cause an if condition to fail)."
214
+
215
+ "not":
216
+ definition: "Boolean negation."
217
+ example:
218
+ - "not true # false"
219
+ - "not 10 # false"
220
+ - "not false # true"
221
+
222
+ "or":
223
+ definition: "Boolean or. Differs from || in that or has lower precedence."
224
+ example:
225
+ - "if a == 0 or a == 1"
226
+ - " puts a"
227
+ - "end"
228
+
229
+ "redo":
230
+ definition: "Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution."
231
+
232
+ "rescue":
233
+ definition: "Designates an exception-handling clause. Can occur either inside a begin<code>/<code>end block, inside a method definition (which implies begin), or in modifier position (at the end of a statement)."
234
+ example:
235
+ - "begin"
236
+ - " 1/0"
237
+ - "rescue ZeroDivisionError"
238
+ - " puts 'Can't do that!'"
239
+ - "end"
240
+
241
+ "retry":
242
+ definition: "Inside a rescue clause, retry causes Ruby to return to the top of the enclosing code (the begin keyword, or top of method or block) and try executing the code again."
243
+ example:
244
+ - "a = 0"
245
+ - "begin"
246
+ - " 1/a"
247
+ - "rescue ZeroDivisionError => e"
248
+ - " puts e.message"
249
+ - " puts 'Let's try that again...'"
250
+ - " a = 1"
251
+ - " retry"
252
+ - "end"
253
+ - "puts 'That's better!'"
254
+
255
+ "return":
256
+ definition: "Inside a method definition, executes the ensure clause, if present, and then returns control to the context of the method call. Takes an optional argument (defaulting to nil), which serves as the return value of the method. Multiple values in argument position will be returned in an array."
257
+ example:
258
+ - "def three"
259
+ - " return 3"
260
+ - "end"
261
+ - " "
262
+ - "a = three"
263
+ - "puts a # 3"
264
+
265
+ "self":
266
+ definition: "self is the current object and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context."
267
+
268
+ "super":
269
+ definition: "Called from a method, searches along the method lookup path (the classes and modules available to the current object) for the next method of the same name as the one being executed. Such method, if present, may be defined in the superclass of the object’s class, but may also be defined in the superclass’s superclass or any class on the upward path, as well as any module mixed in to any of those classes."
270
+ example:
271
+ - "module Vehicular"
272
+ - " def move_forward(n)"
273
+ - " @position += n"
274
+ - " end"
275
+ - "end"
276
+ - " "
277
+ - "class Vehicle"
278
+ - " include Vehicular # Adds Vehicular to the lookup path"
279
+ - "end"
280
+ - " "
281
+ - "class Car < Vehicle"
282
+ - " def move_forward(n)"
283
+ - " puts 'Vrooom!'"
284
+ - " super # Calls Vehicular#move_forward"
285
+ - " end"
286
+ - "end"
287
+
288
+ "then":
289
+ definition: "Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons."
290
+ example:
291
+ - "if a > b; puts 'a wins!' end"
292
+ - "if a > b then puts 'a wins!' end"
293
+
294
+ "true":
295
+ definition: "The sole instance of the special class TrueClass. true encapsulates Boolean truth; however, <emph>all</emph> objects in Ruby are true in the Boolean sense (informally, they cause an if test to succeed), with the exceptions of false and nil."
296
+
297
+ "undef":
298
+ definition: "Undefines a given method, for the class or module in which it’s called. If the method is defined higher up in the lookup path (such as by a superclass), it can still be called by instances classes higher up."
299
+ example:
300
+ - "class C"
301
+ - " def m"
302
+ - " 'Hi'"
303
+ - " end"
304
+ - "end"
305
+ - "class D < C"
306
+ - "end"
307
+ - "class E < D"
308
+ - "end"
309
+ - " "
310
+ - "class D"
311
+ - " undef m"
312
+ - "end"
313
+ - " "
314
+ - "C.new.m # Hi"
315
+ - "D.new.m # error"
316
+ - "E.new.m # error"
317
+
318
+ "unless":
319
+ definition: "The negative equivalent of if."
320
+ example:
321
+ - "unless y.score > 10"
322
+ - " puts 'Sorry; you needed 10 points to win.'"
323
+ - "end"
324
+
325
+ "until":
326
+ definition: "The inverse of while : executes code until a given condition is true, i.e., while it is not true. The semantics are the same as those of while; see while."
327
+
328
+ "when":
329
+ definition: "Used with keyword case"
330
+ example:
331
+ - "case x"
332
+ - "when 1,2,3"
333
+ - " puts '1, 2, or 3'"
334
+ - "when 10"
335
+ - " puts '10'"
336
+ - "else"
337
+ - " puts 'Some other number'"
338
+ - "end"
339
+
340
+ "while":
341
+ definition: "while takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true."
342
+ example:
343
+ - "i = 0"
344
+ - "while i < 10"
345
+ - " i += 1"
346
+ - "end"
347
+
348
+ "yield":
349
+ definition: "Called from inside a method body, yields control to the code block (if any) supplied as part of the method call. If no code block has been supplied, calling yield raises an exception."
350
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubykeyword
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Edwin Rozario
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-23 00:00:00 +05:30
18
+ date: 2012-03-07 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,6 +30,7 @@ extra_rdoc_files: []
30
30
 
31
31
  files:
32
32
  - lib/rubykeyword.rb
33
+ - lib/settings.yaml
33
34
  has_rdoc: true
34
35
  homepage: https://github.com/EdwinRozario/RubyKeyword
35
36
  licenses: []