[ Pobierz całość w formacie PDF ]
.active_support/core_ext/array/conversionsThe following methods are used for converting Ruby arrays into other formats.to_formatted_s(format = :default)Two formats are supported,:defaultand:db.The:defaultformat delegates to the normalto_smethod foran array, which simply concatenates the contents into one mashed-up string.1 > %w(foo bar baz quux).to_s2 => "foobarbazquux"The much more interesting:dboption returns"null"if the array is empty, or concatenates theidfields ofits member elements into a comma-delimited string with code like this:1 collect { |element| element.id }.join(",")In other words, the:dbformatting is meant to work with ActiveRecord objects (or other types of objects thatproperly respond toid).1 > %w(foo bar baz quux).to_s(:db)2 warning: Object#id will be deprecated; use Object#object_id3 => "20244090,20244080,20244070,20244060"Active Support API Reference 547to_sTheto_smethod ofArrayis aliased toto_formatted_s.to_sentence(options = {})Converts the array to a comma-separated sentence in which the last element is joined by a connector word.1 >> %w(alcohol tobacco firearms).to_sentence2 => "alcohol, tobacco, and firearms"The following options are available forto_sentence::connector The word used to join the last element in arrays with two or more elements (default: and ).:skip_last_comma Set this option totrueto return a, b and c instead of a, b, and c.to_xml(options = {}) |xml|.As covered in Chapter 21, XML and Active Resource , theto_xmlmethod onArraycan be used to create anXML collection by iteratively callingto_xmlon its members, and wrapping the entire thing in an enclosingelement.All of the array elements must respond toto_xml.1 >> ["riding","high"].to_xml2 RuntimeError: Not all elements respond to to_xmlThe following example yields theBuilderobject to an optional block so that arbitrary markup can be insertedat the bottom of the generated XML, as the last child of the enclosing element.1 {:foo => "foo", :bar => 42}.to_xml do |xml|2 xml.did_it "again"3 endoutputs the following XML:123 424 foo5 again6The options forto_xmlare:Active Support API Reference 548:builder Defaults to a new instance ofBuilder::XmlMarkup.Specify explicitly if you re callingto_xmlonthis array as part of a larger XML construction routine.:children Sets the name to use for element tags explicitly.Defaults to singularized version of the:rootnameby default.:dasherize Whether or not to turn underscores to dashes in tag names (defaults totrue).:indent Indent level to use for generated XML (defaults to two spaces).:root The tag name to use for the enclosing element.If no:rootis supplied and all members of the arrayare of the same class, the dashed, pluralized form of the first element s class name is used as a default.Otherwise the default:rootisrecords.:skip_instruct Whether or not to generate an XML instruction tag by callinginstruct!onBuilder.:skip_types Whether or not to include atype="array"attribute on the enclosing element.active_support/core_ext/array/extract_optionsActive Support provides a method for extracting Rails-style options from a variable-length set of argumentparameters.extract_options!Extracts options from a variable set of arguments.It s a bang method because it removes and returns the lastelement in the array if it s a hash; otherwise, it returns a blank hash and the source array is unmodified.1 def options(*args)2 args.extract_options!3 end45 >> options(1, 2)6 => {}78 >> options(1, 2, :a => :b)9 => {:a=>:b}active_support/core_ext/array/groupingMethods used for splitting array elements into logical groupings.in_groups(number, fill_with = nil) |group|.Thein_groupsmethod splits an array into anumberof equally sized groups.If afill_withparameter isprovided, its value is used to pad the groups into equal sizes.Active Support API Reference 5491 %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}2 ["1", "2", "3", "4"]3 ["5", "6", "7", nil]4 ["8", "9", "10", nil]56 %w(1 2 3 4 5 6 7).in_groups(3, ' ') {|group| p group}7 ["1", "2", "3"]8 ["4", "5", " "]9 ["6", "7", " "]In the special case that you don t want equally sized groups (in other words, no padding) then passfalseasthe value offill_with.1 %w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}2 ["1", "2", "3"]3 ["4", "5"]4 ["6", "7"]in_groups_of(number, fill_with = nil) {|group|.}Related to its siblingin_groups, thein_groups_ofmethod splits an array into groups of the specifiednumbersize, padding any remaining slots.Thefill_withparameter is used for padding and defaults tonil.If a blockis provided, it is called with each group; otherwise, a two-dimensional array is returned.1 >> %w(1 2 3 4 5 6 7).in_groups_of(3)2 => [[1, 2, 3], [4, 5, 6], [7, nil, nil]34 >> %w(1 2 3).in_groups_of(2, ' ') {|group| puts group }5 => [[1, 2],[3, " "]]Passingfalseto thefill_withparameter inhibits the fill behavior.1 >> %w(1 2 3).in_groups_of(2, false) {|group| puts group }2 => [[1, 2][3]]Thein_groups_ofmethod is particularly useful for batch-processing model objects and generating table rowsin view templates.split(value = nil, &block)Divides an array into one or more subarrays based on a delimiting value:Active Support API Reference 5501 [1, 2, 3, 4, 5].split(3) #=> [[1, 2], [4, 5]]or the result of an optional block:1 (1.8).to_a [ Pobierz całość w formacie PDF ]