Path: | lib/haml/util.rb |
Last Update: | Tue Mar 07 15:51:49 +0000 2023 |
This is used for methods in {Haml::Buffer} that need to be very fast, and take a lot of boolean parameters that are known at compile-time. Instead of passing the parameters in normally, a separate method is defined for every possible combination of those parameters; these are then called using \{static_method_name}.
To define a static method, an ERB template for the method is provided. All conditionals based on the static parameters are done as embedded Ruby within this template. For example:
def_static_method(Foo, :my_static_method, [:foo, :bar], :baz, :bang, <<RUBY) <% if baz && bang %> return foo + bar <% elsif baz || bang %> return foo - bar <% else %> return 17 <% end %> RUBY
\{static_method_name} can be used to call static methods.
@overload def_static_method(klass, name, args, *vars, erb) @param klass [Module] The class on which to define the static method @param name [to_s] The (base) name of the static method @param args [Array<Symbol>] The names of the arguments to the defined methods
(**not** to the ERB template)
@param vars [Array<Symbol>] The names of the static boolean variables
to be made available to the ERB template
@param erb [String] The template for the method code
A version of `Enumerable#enum_cons` that works in Ruby 1.8 and 1.9.
@param enum [Enumerable] The enumerable to get the enumerator for @param n [Fixnum] The size of each cons @return [Enumerator] The consed enumerator
A version of `Enumerable#enum_slice` that works in Ruby 1.8 and 1.9.
@param enum [Enumerable] The enumerable to get the enumerator for @param n [Fixnum] The size of each slice @return [Enumerator] The consed enumerator
A version of `Enumerable#enum_with_index` that works in Ruby 1.8 and 1.9.
@param enum [Enumerable] The enumerable to get the enumerator for @return [Enumerator] The with-index enumerator
Checks to see if a class has a given method. For example:
Haml::Util.has?(:public_instance_method, String, :gsub) #=> true
Method collections like `Class#instance_methods` return strings in Ruby 1.8 and symbols in Ruby 1.9 and on, so this handles checking for them in a compatible way.
@param attr [to_s] The (singular) name of the method-collection method
(e.g. `:instance_methods`, `:private_methods`)
@param klass [Module] The class to check the methods of which to check @param method [String, Symbol] The name of the method do check for @return [Boolean] Whether or not the given collection has the given method
Returns the ASCII code of the given character.
@param c [String] All characters but the first are ignored. @return [Fixnum] The ASCII code of `c`.
Tests the hash-equality of two sets in a cross-version manner. Aggravatingly, this is order-dependent in Ruby 1.8.6.
@param set1 [Set] @param set2 [Set] @return [Boolean] Whether or not the sets are hashcode equal
Returns the hash code for a set in a cross-version manner. Aggravatingly, this is order-dependent in Ruby 1.8.6.
@param set [Set] @return [Fixnum] The order-independent hashcode of `set`