Ruby libraries / Core
From WhyNotWiki
Ruby libraries / Core edit (Category edit)
[edit] ActiveSupport
[Collection (category)]
See ActiveSupport
[edit]
Facets
[Collection (category)]
require "facets/string/margin"
# prior to Facets 2.0 require "facets/core/string/margin"
[edit]
Ruby/Extensions
[Collection (category)]
http://extensions.rubyforge.org/rdoc/index.html
[edit] Object
http://extensions.rubyforge.org/rdoc/classes/Object.html
[edit] in?(enumerable)
Test this object for inclusion in a given collection.45.in? (1...100) => trueThis method is contained in object.rb and enumerable.rb, because it logically belongs in both.
[edit] non_nil?()
The opposite of nil?."hello".non_nil? # -> true nil.non_nil? # -> false
[edit] singleton_class()
Returns the singleton class associated with this object.
[edit] pp_s
Want the output of pp as a string? Well, you're in luck: obj.pp_s does just that!
http://extensions.rubyforge.org/rdoc/classes/Object.html#M000020
Returns a pretty-printed string of the object. Requires libraries pp and stringio from the Ruby standard library.The following code pretty-prints an object (much like p plain-prints an object):
pp objectThe following code captures the pretty-printing in str instead of sending it to STDOUT.
str = object.pp_s
gem install -r extensions require 'extensions/object' puts obj.pp_s
(This is useful in ERb files, where you can't just do a normal pp obj. Instead, do a <%= obj.pp_s %>.)
To do it without pp_s extension:
irb -> s = ''; out = PP.pp({:a => 1}, s)
=> "{:a=>1}\n"
A bit obfuscated.
[edit] Comparison of Facets and Ruby/Extensions
Thomas on Facets-Universal 2007-03-26 19:10 [1]
[...] there is another gem called extensions (http://extensions.rubyforge.org), which is somehow similar to what facets core does. I just discovered it,
Yes, I've known of the project since it's inception. In fact a number of facets come from there. A well done project, but much more conservative than Facets. I've talked to Gavin (the maintainer) on a number of occasions. It's complimented by his project "additions". But development trailed off a couple of years ago.
require 'facets/core/symbol/to_proc'
[edit] (This is the middle)
[edit] assert_has_only_keys / assert_valid_keys
Comparison (shows any name differences; behavior differences will also be noted if any are found)
| Facets | QualitySmith Extensions | Rails ActiveSupport | Comment/Description | |
|---|---|---|---|---|
style="background: #DDFFDD" assert_has_only_keys |
style="background: #FFAF6F" assert_valid_keys |
|||
style="background: #DDFFDD" symbolize_keys(!) |
style="background: #FFAF6F" symbolize_keys(!) |
|||
style="background: #DDFFDD" stringify_keys(!) |
style="background: #FFAF6F" stringify_keys(!) |
|||
style="background: #DDFFDD" reverse_merge(!) |
style="background: #FFAF6F" reverse_merge(!) |
|||
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/hash/keys.rb require 'facets/core/hash/assert_has_only_keys' require 'facets/core/hash/symbolize_keys' require 'facets/core/hash/reverse_merge'
[edit] Kernel#constant / String/Symbol#constantize/
| Facets | QualitySmith Extensions | Rails ActiveSupport | Comment/Description | |
|---|---|---|---|---|
Kernel#constant String#to_const Symbol#to_const |
style="background: #DDFFDD" String#constantize/Symbol#constantize |
String#constantize/Symbol#constantize |
require 'qualitysmith_extensions/symbol/constantize' require 'facets/core/kernel/constant'
Someone had written that Facets Kernel#constant is "not as object oriented as ActiveSupport's; it's like a global method". Well, There's a reason that Facets uses a Kernel method. It makes it easier to get the constant you want since it can search from the "bottom up". By confining the code to a String binding the method can only work from the "top down". Even so, Facets also has String#to_const.
[edit] Module#modspace / Module#nesting / Module#parent(s)
| Facets | QualitySmith Extensions | Rails ActiveSupport | Comment/Description | |
|---|---|---|---|---|
style="background: #DDFFDD" Module#modspace |
style="background: #DDFFDD" Module#namespace |
style="background: #FFAF6F" Module#parent |
Returns the module’s container module. | |
nesting (A::B, B) |
style="background: #DDFFDD" parents |
style="background: #FFAF6F" parents |
||
style="background: #DDFFDD" Module#split ('A', 'B')
|
||||
style="background: #FFBBBB" Module#modspace (Instance method only) |
style="background: #DDFFDD" Module#namespace, Module.namespace_name_of/Module.dirname |
|||
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/module/introspection.rb require 'qualitysmith_extensions/module/namespace' #require 'facets/core/module/modspace' require 'facets/core/module/nesting'
Preferred name? I don't know. I don't really like the sound of modspace. But is "parent" too reminiscent of superclass / ancestry terminology? I'm afraid it might be...
I like how modspace alludes to the fact that it's a namespace created by a module. It's more about containers and nesting than actual ancestry (parent, descendant), so I think it would be good to use one of these words (or some combination):
- nesting
- container/contained_in
- namespace/space
- mod/module
I went with namespace and dirname in 'qualitysmith_extensions/module/namespace'.
Problem with namespace is that it is often used for forms of selector namespaces. Facets has a lib for them, and Rake uses the method for it's own thing. dirname is ok, in Facets that returns a string and Facets has actually had that forever --seeing that I came up with it ;) --Trans.
[edit]
Module#create / Class#create
require 'qualitysmith_extensions/module/create'
Differences from Module creation helper:
# r2964 (Tyler): # * Started from http://svn.pluginaweek.org/trunk/plugins/ruby/module/module_creation_helper/ (Last Changed Rev: 320) # * Renamed :parent option to :namespace. (:parent is still allowed for backwards compatibility) # * Changed examples and tests to pass in the name as a symbol instead of a string. # * Made it so you can pass in the namespace as part of the name if you want: Module.create(:'Foo::Bar') instead of Module.create(:Foo, :parent => Bar) # * Added to the documentation # * Added new tests # * test_with_block_2 # * test_nested_class_with_superclass_with_same_name # * test_referencing_a_namespace_that_isnt_defined # * test_creating_a_class_more_than_once # * test_using_return_value_of_one_create_within_another_create # * Added __FILE__, __LINE__ to class_eval so that error messages would be more helpful.
[edit] ↓ Module creation helper
| Homepage: | http://wiki.pluginaweek.org/Module_creation_helper |
|---|---|
| Source code: | http://svn.pluginaweek.org/trunk/plugins/ruby/module/module_creation_helper/ (no gem)
|
| Description: | module_creation_helper adds a helper method for creating new modules and classes at runtime. |
| Depends on: | ActiveSupport
|
# With a block
>> Module.create('Foo') do
def hello
"hello world"
end
end
=> Foo
>> include Foo
=> Object
>> hello
=> "hello world"
# With a parent
>> Module.create('Bar', :parent => Foo)
=> Foo::Bar
irb -> require 'lib/module_creation_helper.rb'
irb -> Module.create('Foo')
NoMethodError: undefined method `assert_valid_keys' for {}:Hash
from ./lib/module_creation_helper.rb:30:in `create'
from (irb):9
irb -> require 'active_support'
irb -> Module.create('Foo')
=> Foo
irb -> Class.create(:Woddle, :parent => Module.create(:Waddle))
=> Waddle::Woddle
Problems:
- I'd rather it be distributed in a gem, as part of Facets
- I'd rather say:
Class.create(:'Waddle::Woddle') - I'd rather it not depend on ActiveSupport at all
[edit] OrderedHash
| Categories/Tags: | |
|---|---|
| Homepage: | OrderedHash - Hash which preserves items order like PHP array. And other scripts comming soon. |
| Documentation: | http://simplypowerful.rubyforge.org/wiki/wiki.pl
|
| Project/Development: | http://rubyforge.org/projects/simplypowerful/
|
| Authors: | Jan Molič |
| Readiness: | 1.2005.1.1 August 9, 2005
|
[edit] Flexible extend
| Homepage: | http://wiki.pluginaweek.org/Flexible_extend |
|---|---|
| Source code: | http://svn.pluginaweek.org/trunk/plugins/ruby/module/flexible_attributes |
| Project/Development: | http://dev.pluginaweek.org/browser/trunk/plugins/ruby/module/flexible_attributes
|
| Description: | flexible_attributes allows strings and Procs (in addition to modules) to be used for extending objects.
|
class Klass
end
k = Klass.new
k.extend Proc.new {
def hello
"Hello from Proc.\n"
end
}
k.hello # => "Hello from Proc.\n"
[edit] autorequire
http://codeforpeople.com/lib/ruby/autorequire/autorequire-0.0.0/README
- autorequire is sleeker, more aerodynamic, greasier version of autoload.
- like autoload it's primary usage is to speed up the load time of large libraries which themselves do many requires.
autorequire 'Queue', 'thread'
# but we can also specify nested classes. note the alternative hash syntax.
# a list of const => lib pairs may also be given.
autorequire 'CGI::Session' => 'cgi/session'
[edit] QualitySmith Extensions
http://qualitysmithext.rubyforge.org
[edit] Ruby/DLX
| Project/Development: | http://rubyforge.org/projects/ruby-dlx/
|
|---|---|
| Description: | No more ruby-C-extensions needed to use the shared C library of your choice in ruby, now you can do it directly in Ruby itself! Ruby/DLX shows how simple interfacing ruby with c-libraries should be. The next version will no longer be a frontend to DL2.
|
[edit] SecuredRuby
| Project/Development: | http://rubyforge.org/projects/securedruby/
|
|---|---|
| Description: | Tainting objects and the tainting level seems quite primitive. This project aims to create a security manager like the security manager in the Java Virtual machine.
|
| Readiness: | 2 - Pre-Alpha, This Project Has Not Released Any Files 2007-03-23 19:20
|
[edit] Genie
| Documentation: | http://genie.rubyforge.org
|
|---|---|
| Project/Development: | http://rubyforge.org/projects/genie/
|
| Description: | Genie is an implementation of the command pattern. It includes undo/redo functionality, transaction bundling, and load balancing features.
|
[edit] ToadCode
| Homepage: | http://toad.rubyforge.org/ |
|---|---|
| Source code: | http://toad.rubyforge.org/ |
| Project/Development: | http://rubyforge.org/projects/toad/
|
| Description: | [[description := The dusty old archives of the Toady Codey and his dabbles of scripty confection. [Miscellaneous stuff that wasn't ready/useful enough to throw in Facets.]|The dusty old archives of the Toady Codey and his dabbles of scripty confection. [Miscellaneous stuff that wasn't ready/useful enough to throw in Facets.]]]
|
| Authors: | Thomas Sawyer
|
Files: actionplan ballyhoo codepack fermat flashcard indexable mynil openproxy proto rtar texml type world advice benchmarks conditionals fhsmap function knowself namespace ostructable pry scanip tit4tat uri xmlproof annscript capsule crossit! finance harray local2inst once predicate quicktest selfshimmy toplevel vars yip associo closecall datetime fixnum-const implements merge oni prettyxml rerexml service trulyprivate webscriptable
[edit] Unicode Chars
| Project/Development: | http://rubyforge.org/projects/unicodechars/
|
|---|---|
| Description: | This project wraps the Rails unicode support in a gem, and mixes in support for the unicode gem. Its goal is to make the excellent work done by the Rails community seamless in traditional Ruby environments.
|
| Readiness: | 0.0.2 October 30, 2006
|
| Depends on | ActiveSupport + |
| Description | [Oops! No type defined for attribute] |
