sort and sort! - by this array, you can get a new sorted array, or you can sort it on the spotuniq and uniq! - on this array, you can get a new array without repetitions, and you can delete the repetition in the array itself in placeselect methods (filter elements by a given filter), map (convert array elements according to a given function) and flatten (expand nested arrays so that we obtain a one-dimensional array whose elements are not arrays).downcase and downcase! , upcase and upcase! , sub and sub! (replacement of the first found substring by sample), gsub and gsub! (replacing all found substrings), strip and strip! (removal of extreme whitespace characters), ...make_nobang methodmake_nobang .downcase , upcase , sub , strip , gsub , having the methods downcase! upcase! sub! strip! , gsub! : class String
def downcase!
...
end
def upcase!
...
end
def sub!
...
end
def strip!
...
end
make_nobang: downcase,: upcase,: sub,: gsub,: strip
end
make_nobang method: class Module
def make_nobang (* methods)
methods.each do | method |
define_method ("# {method}") do | * args |
self.dup.send ("# {method}!", * args)
end
end
end
end
class String
def down!
self.downcase!
end
make_nobang: down
end
a = "abcABC"
puts a.down
puts a
a.down!
puts a
xyz method on the method not only directly: a.xyz(1, 2) , but also with the help of “passing a message to the object”: a.send('xyz' ,1, 2) . The principal difference is that the first argument in the latter case can be calculated by the expression. There is another difference - send ignores the protected and private scopes. The send method is the next important dynamic programming method along with the already mentioned methods eval, class_eval, instance_eval, instance_variable_get, instance_variable_set, define_method. class Array
def m! (& block)
self.map! (& block)
end
make_nobangs: m
end
a = [1,2,3]
puts (am {| i | i * i}). inspect
puts a.inspect
avoroztsov @ subuntu: ~ / meta-lectures $ ruby -v make_nobang.rb
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
make_nobang.rb: 27: in `map! ': no block given (LocalJumpError)
from make_nobang.rb: 27: in `m! '
from make_nobang.rb: 6: in `send '
from make_nobang.rb: 6: in `m '
from make_nobang.rb: 33
avoroztsov @ subuntu: ~ / meta-lectures $
make_nobang bad becausepublic scope, although the source one probably had the appearance of private or protected .make_nobang , so that it really make_nobang into account all the subtleties, and so that when changing the signature and visibility of the bang-method, it is not necessary to make the appropriate changes to the nobang-method. In addition, make_nobang calls can be processed by an automatic documentation system. class Module
def make_nobangs (* methods)
methods.each do | method |
define_method ("# {method}") do | * args, & block |
self.dup.send ("# {method}!", * args, & block)
end
end
end
end
private_methods , protected_methods , ... for class Object .eval . See Discussion Method # get_args where you can fully get an idea of what a method signature is in Ruby.sort and sort! already have arrays. But let's make sure that this post isn't in vain, write a quick sort on Ruby yourself and implement the qsort and qsort! methods qsort!partition method defined for Enumerable instances: class Array
def qsort
return self.dup if size <= 1
# we will divide into parts by the first element
l, r = partition {| x | x <= self.first}
c, l = l.partition {| x | x == self.first}
l.qsort + c + r.qsort # concatenation of three arrays
end
end
partition3 method: class Array
# given block should return 0, 1 or 2
# -1 stands for 2
# outputs three arrays
def partition3
a = Array.new (3) {| i | []}
each do | x |
a [yield (x)] << x
end
a
end
def qsort
return self.dup if size <= 1
c, l, r = partition3 {| x | first <=> x}
l.qsort + c + r.qsort
end
end
class Array
def qsort!
self.replace (self.qsort)
end
end
a = [1,7,6,5,4,3,2,1]
p a.qsort # => [1, 1, 2, 3, 4, 5, 6, 7]
pa # => [1,7,6,5,4,3,2,1]
a.qsort!
pa # => [1, 1, 2, 3, 4, 5, 6, 7]
def make_bang (* methods)
methods.each do | method |
define_method ("# {method}!") do | * args |
self.replace (self.send (method, * args))
end
end
end
class Array
make_bang: qsort
end
make_nobang and make_bang methods make_nobang and there’s make_bang nothing like that yet in core and std, and never will be in the near future. :)))Set Set no method " sort! "? Why do different classes (for example, Float Float ) there is no " to_i! " to_i! ?++ "?a = a.sort.select{|x| x > 0}.uniq a = a.sort.select{|x| x > 0}.uniq ;a.uniq!; a.select!{|x| x > 0}.sort! a.uniq!; a.select!{|x| x > 0}.sort! ;a.uniq!.select!{|x| x > 0}.sort! a.uniq!.select!{|x| x > 0}.sort! ?make_nobang . class Module
def make_nobang (* methods)
methods.each do | method |
bang_method = "# {method}!"
define_method ("# {method}") do | * args |
self.dup.send (bang_method, * args)
end
end
end
end
class Module
def make_nobang (* methods)
methods.each do | method |
define_method ("# {method}") do | * args |
self.dup.send ("# {method}!", * args)
end
end
end
end
bang_method from the method being created? If available, is it a miracle? She is local! And the created method will be called later, when the make_bang method has finished its execution!Source: https://habr.com/ru/post/50169/
All Articles