X7ROOT File Manager
Current Path:
/opt/alt/ruby18/share/ri/1.8/system/Enumerable
opt
/
alt
/
ruby18
/
share
/
ri
/
1.8
/
system
/
Enumerable
/
ðŸ“
..
ðŸ“
Enumerator
📄
all%3f-i.yaml
(856 B)
📄
any%3f-i.yaml
(877 B)
📄
cdesc-Enumerable.yaml
(2.9 KB)
📄
collect-i.yaml
(609 B)
📄
count-i.yaml
(815 B)
📄
cycle-i.yaml
(896 B)
📄
detect-i.yaml
(732 B)
📄
drop-i.yaml
(435 B)
📄
drop_while-i.yaml
(539 B)
📄
each_cons-i.yaml
(633 B)
📄
each_slice-i.yaml
(563 B)
📄
each_with_index-i.yaml
(609 B)
📄
entries-i.yaml
(546 B)
📄
enum_cons-i.yaml
(633 B)
📄
enum_slice-i.yaml
(563 B)
📄
enum_with_index-i.yaml
(609 B)
📄
find-i.yaml
(728 B)
📄
find_all-i.yaml
(541 B)
📄
find_index-i.yaml
(769 B)
📄
first-i.yaml
(455 B)
📄
grep-i.yaml
(845 B)
📄
group_by-i.yaml
(519 B)
📄
include%3f-i.yaml
(585 B)
📄
inject-i.yaml
(209 B)
📄
map-i.yaml
(601 B)
📄
max-i.yaml
(678 B)
📄
max_by-i.yaml
(466 B)
📄
member%3f-i.yaml
(583 B)
📄
min-i.yaml
(678 B)
📄
min_by-i.yaml
(460 B)
📄
minmax-i.yaml
(787 B)
📄
minmax_by-i.yaml
(567 B)
📄
none%3f-i.yaml
(895 B)
📄
one%3f-i.yaml
(893 B)
📄
partition-i.yaml
(537 B)
📄
reduce-i.yaml
(2.05 KB)
📄
reject-i.yaml
(485 B)
📄
reverse_each-i.yaml
(297 B)
📄
select-i.yaml
(537 B)
📄
sort-i.yaml
(926 B)
📄
sort_by-i.yaml
(3.16 KB)
📄
take-i.yaml
(398 B)
📄
take_while-i.yaml
(509 B)
📄
to_a-i.yaml
(540 B)
📄
to_set-i.yaml
(348 B)
📄
zip-i.yaml
(1.01 KB)
Editing: sort_by-i.yaml
--- !ruby/object:RI::MethodDescription aliases: [] block_params: comment: - !ruby/struct:SM::Flow::P body: Sorts <em>enum</em> using a set of keys generated by mapping the values in <em>enum</em> through the given block. - !ruby/struct:SM::Flow::VERB body: " %w{ apple pear fig }.sort_by {|word| word.length}\n #=> ["fig", "pear", "apple"]\n" - !ruby/struct:SM::Flow::P body: The current implementation of <tt>sort_by</tt> generates an array of tuples containing the original collection element and the mapped value. This makes <tt>sort_by</tt> fairly expensive when the keysets are simple - !ruby/struct:SM::Flow::VERB body: " require 'benchmark'\n include Benchmark\n\n a = (1..100000).map {rand(100000)}\n\n bm(10) do |b|\n b.report("Sort") { a.sort }\n b.report("Sort by") { a.sort_by {|a| a} }\n end\n" - !ruby/struct:SM::Flow::P body: <em>produces:</em> - !ruby/struct:SM::Flow::VERB body: " user system total real\n Sort 0.180000 0.000000 0.180000 ( 0.175469)\n Sort by 1.980000 0.040000 2.020000 ( 2.013586)\n" - !ruby/struct:SM::Flow::P body: However, consider the case where comparing the keys is a non-trivial operation. The following code sorts some files on modification time using the basic <tt>sort</tt> method. - !ruby/struct:SM::Flow::VERB body: " files = Dir["*"]\n sorted = files.sort {|a,b| File.new(a).mtime <=> File.new(b).mtime}\n sorted #=> ["mon", "tues", "wed", "thurs"]\n" - !ruby/struct:SM::Flow::P body: "This sort is inefficient: it generates two new <tt>File</tt> objects during every comparison. A slightly better technique is to use the <tt>Kernel#test</tt> method to generate the modification times directly." - !ruby/struct:SM::Flow::VERB body: " files = Dir["*"]\n sorted = files.sort { |a,b|\n test(?M, a) <=> test(?M, b)\n }\n sorted #=> ["mon", "tues", "wed", "thurs"]\n" - !ruby/struct:SM::Flow::P body: This still generates many unnecessary <tt>Time</tt> objects. A more efficient technique is to cache the sort keys (modification times in this case) before the sort. Perl users often call this approach a Schwartzian Transform, after Randal Schwartz. We construct a temporary array, where each element is an array containing our sort key along with the filename. We sort this array, and then extract the filename from the result. - !ruby/struct:SM::Flow::VERB body: " sorted = Dir["*"].collect { |f|\n [test(?M, f), f]\n }.sort.collect { |f| f[1] }\n sorted #=> ["mon", "tues", "wed", "thurs"]\n" - !ruby/struct:SM::Flow::P body: This is exactly what <tt>sort_by</tt> does internally. - !ruby/struct:SM::Flow::VERB body: " sorted = Dir["*"].sort_by {|f| test(?M, f)}\n sorted #=> ["mon", "tues", "wed", "thurs"]\n" full_name: Enumerable#sort_by is_singleton: false name: sort_by params: | enum.sort_by {| obj | block } => array visibility: public
Upload File
Create Folder