├── .gitignore
├── Gemfile
├── Rakefile
├── ChangeLog.rdoc
├── README.rdoc
├── ruby_info.gemspec
├── LICENSE.txt
└── lib
└── ruby_info.rb
/.gitignore:
--------------------------------------------------------------------------------
1 | Gemfile.lock
2 | pkg
3 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gemspec
4 |
5 | platform :rbx do
6 | gem 'rubysl-date'
7 | gem 'rubysl-singleton'
8 | end
9 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | # encoding: utf-8
2 |
3 | require 'rubygems'
4 |
5 | begin
6 | require 'bundler'
7 | rescue LoadError => e
8 | warn e.message
9 | warn "Run `gem install bundler` to install Bundler."
10 | exit -1
11 | end
12 |
13 | begin
14 | Bundler.setup(:development)
15 | rescue Bundler::BundlerError => e
16 | warn e.message
17 | warn "Run `bundle install` to install missing gems."
18 | exit e.status_code
19 | end
20 |
21 | require 'rake'
22 |
23 | require 'rubygems/tasks'
24 | Gem::Tasks.new
25 |
--------------------------------------------------------------------------------
/ChangeLog.rdoc:
--------------------------------------------------------------------------------
1 | === 1.0.1 / 2014-01-15
2 | * Fix VERSION constant
3 |
4 | === 1.0.0 / 2014-01-15
5 | * New method: all (Output all available information)
6 | * Rename platform to ruby_platform
7 | * Rename environment to env
8 | * Add hostname (Socket.gethostname)
9 | * Add signals and system_call_errors (Signal.list and Errno.constants)
10 | * Add script_lines (SCRIPT_LINES__)
11 | * Add encodings and encoding_aliases (Encoding.name_list and Encoding.aliases)
12 | * Add gc (GC.stat)
13 | * Add user_home (Dir.home)
14 | * Add ruby_revision and ruby_copyright (RUBY_REVISION and RUBY_COPYRIGHT)
15 | * Add verbose and debug ($VERBOSE and $DEBUG)
16 | * Fix program_arguments
17 | * Moved from zucker 13.1 gem into its own gem
18 |
19 |
--------------------------------------------------------------------------------
/README.rdoc:
--------------------------------------------------------------------------------
1 | = RubyInfo
2 |
3 | Provides a +RubyInfo+ class that simplifies accessing global information. Run RubyInfo.list to get started. You can also run RubyInfo.all to get a hash of all available information (may be a lot).
4 |
5 | == Setup
6 |
7 | On your command-line:
8 |
9 | $ gem install ruby_info
10 |
11 | In Ruby:
12 |
13 | require 'ruby_info'
14 |
15 | == Usage
16 |
17 | Call methods on the +RubyInfo+ class. See the source for more documentation.
18 |
19 | == Also See
20 |
21 | https://github.com/janlelis/ruby_engine |
22 | https://github.com/janlelis/ruby_version |
23 | https://github.com/rdp/os
24 |
25 | == J-_-L
26 |
27 | Copyright (c) 2010-2014 Jan Lelis. MIT License. Originated from the zucker gem.
28 |
--------------------------------------------------------------------------------
/ruby_info.gemspec:
--------------------------------------------------------------------------------
1 | # -*- encoding: utf-8 -*-
2 |
3 | require File.expand_path('../lib/ruby_info', __FILE__)
4 |
5 | Gem::Specification.new do |gem|
6 | gem.name = "ruby_info"
7 | gem.version = RubyInfo::VERSION
8 | gem.summary = 'Adds the RubyInfo pseudo-constant.'
9 | gem.description = 'Provides a RubyInfo class that simplifies accessing global information. Run RubyInfo.list to get started.'
10 | gem.license = "MIT"
11 | gem.authors = ["Jan Lelis"]
12 | gem.email = ["hi@ruby.consulting"]
13 | gem.homepage = "https://github.com/janlelis/ruby_info"
14 |
15 | gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) }
16 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18 | gem.require_paths = ['lib']
19 |
20 | gem.add_development_dependency 'rake', '~> 13.0'
21 | gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
22 | end
23 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2014 Jan Lelis
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/lib/ruby_info.rb:
--------------------------------------------------------------------------------
1 | require 'rbconfig'
2 | require 'etc'
3 | require 'socket'
4 |
5 | module RubyInfo
6 | VERSION = '1.0.1'
7 |
8 | class << self
9 | def [](what)
10 | send what
11 | end
12 |
13 | def list
14 | singleton_methods.map(&:to_sym) - [:[], :list, :all]
15 | end
16 |
17 | def all
18 | Hash[ list.map{ |l| [l, self.public_send(l)] }]
19 | end
20 | end
21 |
22 | module_function
23 |
24 | # # #
25 | # program info
26 |
27 | def program_name
28 | $0
29 | end
30 |
31 | def program_arguments
32 | $*
33 | end
34 |
35 | def loaded_programs
36 | $"
37 | end
38 |
39 | def program_data
40 | defined?(::DATA) && DATA
41 | end
42 |
43 | def script_lines
44 | defined?(::SCRIPT_LINES__) && SCRIPT_LINES__
45 | end
46 |
47 | def child_program_status
48 | $CHILD_STATUS
49 | end
50 |
51 | # # #
52 | # system info
53 |
54 | def env
55 | ::ENV
56 | end
57 |
58 | def os
59 | RbConfig::CONFIG['host_os']
60 | end
61 |
62 | def process_id
63 | $$
64 | end
65 |
66 | def load_path
67 | $:
68 | end
69 |
70 | def gc
71 | GC.stat
72 | end
73 |
74 | # # #
75 | # user info
76 |
77 | def user_login
78 | Etc.getlogin
79 | end
80 |
81 | def user_name
82 | Etc.getpwnam(user_login).gecos.split(',')[0]
83 | end
84 |
85 | def user_home
86 | Dir.home
87 | end
88 |
89 | # # #
90 | # network info
91 |
92 | def hostname
93 | Socket.gethostname
94 | end
95 |
96 | # # #
97 | # current info
98 |
99 | def current_file # __FILE__
100 | return $` if caller[0].rindex(/:\d+(:in `.*')?$/)
101 | end
102 |
103 | def working_directory
104 | Dir.pwd
105 | end
106 |
107 | def current_file_directory
108 | if current_file[0,1] == '(' && current_file[-1,1] == ')'
109 | current_file
110 | else
111 | File.dirname(current_file)
112 | end
113 | end
114 |
115 | def current_line # __LINE__
116 | return $1.to_i if caller[0].rindex( /:(\d+)(:in `.*')?$/ )
117 | end
118 |
119 | def current_method # __method__ (except aliases)
120 | return $1.to_sym if caller(1)[0].rindex( /\`([^\']+)\'/ )
121 | end
122 |
123 | def current_callstack
124 | caller
125 | end
126 |
127 | # # #
128 | # input / string info
129 |
130 | def last_input_file
131 | $FILENAME
132 | end
133 |
134 | def last_input_line_number
135 | $.
136 | end
137 |
138 | def last_input
139 | $_
140 | end
141 |
142 | def gets_separator
143 | $/
144 | end
145 |
146 | def join_separator
147 | $,
148 | end
149 |
150 | def print_separator
151 | $,
152 | end
153 |
154 | def split_separator
155 | $;
156 | end
157 |
158 | def external_encoding
159 | Encoding.default_external
160 | end
161 |
162 | def internal_encoding
163 | Encoding.default_internal
164 | end
165 |
166 | def encodings
167 | Encoding.name_list
168 | end
169 |
170 | def encoding_aliases
171 | Encoding.aliases
172 | end
173 |
174 | # # #
175 | # misc
176 |
177 | def security_level
178 | $SAFE
179 | end
180 |
181 | def verbose
182 | $VERBOSE
183 | end
184 |
185 | def warnings_activated?
186 | !! $VERBOSE
187 | end
188 |
189 | def debug
190 | $DEBUG
191 | end
192 |
193 | def debug_activated?
194 | !! $DEBUG
195 | end
196 |
197 | def last_exception
198 | $!
199 | end
200 |
201 | # # #
202 | # ruby objects
203 |
204 | def global_variables
205 | Object.send :global_variables
206 | end
207 |
208 | def global_constants
209 | Object.constants
210 | end
211 |
212 | def system_call_errors
213 | Errno.constants
214 | end
215 |
216 | def signals
217 | Signal.list
218 | end
219 |
220 | # # #
221 | # ruby info
222 |
223 | def ruby_version # also see the ruby_version gem
224 | ::RUBY_VERSION
225 | end
226 |
227 | def ruby_patchlevel
228 | ::RUBY_PATCHLEVEL
229 | end
230 |
231 | def ruby_description
232 | ::RUBY_DESCRIPTION
233 | end
234 |
235 | def ruby_release_date
236 | ::RUBY_RELEASE_DATE
237 | end
238 |
239 | def ruby_engine # also see the ruby_engine gem
240 | defined?(::RUBY_ENGINE) && ::RUBY_ENGINE
241 | end
242 |
243 | def ruby_platform
244 | ::RUBY_PLATFORM
245 | end
246 |
247 | def ruby_revision
248 | defined?(::RUBY_REVISION) && ::RUBY_REVISION
249 | end
250 |
251 | def ruby_copyright
252 | defined?(::RUBY_COPYRIGHT) && ::RUBY_COPYRIGHT
253 | end
254 | end
255 |
256 |
--------------------------------------------------------------------------------