In Files

  • rss/utils.rb

Class/Module Index [+]

Quicksearch

RSS::Utils

RSS::Utils is a module that holds various utility functions that are used across many parts of the rest of the RSS library. Like most modules named some variant of 'util', its methods are probably not particuarly useful to those who aren't developing the library itself.

Public Instance Methods

element_initialize_arguments?(args) click to toggle source

This method is used inside of several different objects to determine if special behavior is needed in the constructor.

Special behavior is needed if the array passed in as args has true or false as its value, and if the second element of args is a hash.

 
               # File rss/utils.rb, line 123
def element_initialize_arguments?(args)
  [true, false].include?(args[0]) and args[1].is_a?(Hash)
end
            
get_file_and_line_from_caller(i=0) click to toggle source

Returns an array of two elements: the filename where the calling method is located, and the line number where it is defined.

Takes an optional argument i, which specifies how many callers up the stack to look.

Examples:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

def baz
  bar
end

baz
# => ["test.rb", 5]
# => ["test.rb", 9]

If i is not given, or is the default value of 0, it attempts to figure out the correct value. This is useful when in combination with instance_eval. For example:

require 'rss/utils'

def foo
  p RSS::Utils.get_file_and_line_from_caller(1)
end

def bar
  foo
end

instance_eval "def baz
  bar
end
", *RSS::Utils.get_file_and_line_from_caller

baz

# => ["test.rb", 8]
 
               # File rss/utils.rb, line 82
def get_file_and_line_from_caller(i=0)
  file, line, = caller[i].split(':')
  line = line.to_i
  line += 1 if i.zero?
  [file, line]
end
            
h(s) click to toggle source
Alias for: html_escape
html_escape(s) click to toggle source

Takes a string s with some HTML in it, and escapes ‘&’, ‘“’, ‘<’ and ‘>’, by replacing them with the appropriate entities.

This method is also aliased to h, for convenience.

Examples:

require 'rss/utils'

RSS::Utils.html_escape("Dungeons & Dragons")
# => "Dungeons &amp; Dragons"
RSS::Utils.h(">_>")
# => "&gt;_&gt;"
 
               # File rss/utils.rb, line 102
def html_escape(s)
  s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end
            
Also aliased as: h
new_with_value_if_need(klass, value) click to toggle source

If value is an instance of class klass, return it, else create a new instance of klass with value value.

 
               # File rss/utils.rb, line 109
def new_with_value_if_need(klass, value)
  if value.is_a?(klass)
    value
  else
    klass.new(value)
  end
end
            
to_class_name(name) click to toggle source

Given a name in a name_with_underscores or a name-with-dashes format, returns the CamelCase version of name.

If the name is already CamelCased, nothing happens.

Examples:

require 'rss/utils'

RSS::Utils.to_class_name("sample_name")
# => "SampleName"
RSS::Utils.to_class_name("with-dashes")
# => "WithDashes"
RSS::Utils.to_class_name("CamelCase")
# => "CamelCase"
 
               # File rss/utils.rb, line 26
def to_class_name(name)
  name.split(/[_\-]/).collect do |part|
    "#{part[0, 1].upcase}#{part[1..-1]}"
  end.join("")
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus