Tag Archive for ruby

Covert Pixels to Centimeters

def convert_pixels_to_cms(pixels, dpi)
return (pixels / dpi) * 2.54
end

source

Compile all css files in a rails project

  # compiles all the css files into one huge string
# this is useful when running with less
# USAGE : all_css.open # => opens the string in less allowing you to search
def all_css
css_files = Dir.glob File.join(RAILS_ROOT, "public/stylesheets/","*.css")
css_content = ""
css_files.each {|file| css_content << IO.read(file)}

css_content.instance_eval do

# opens with less command
def open
system('echo "' << self << '"|less')
end

end

css_content
end

source

port install ruby -> malloc error

# remove locks left behind when the errors aborted the install
sudo port -f clean ruby

# start fresh
sudo port -f uninstall ruby

# install without documentation
sudo port install ruby +default_variants +no_doc

source

Match Single-line Javascript Comments

Regexp.new(///.*$/)

source

Match CSS and JS Comments

# This is how it would look using the Ruby Regexp class

# Only match one-line comments
Regexp.new(//*.*?*//)

# Match single and multi-line comments
Regexp.new(//*.*?*//m)

source

Render file (no html)

send_file 'path/file_003', :disposition => 'attachment; filename=image.jpg'

source

Mixing in a Module immediately after an objects Singleton class

#You can open the class definition body of a singleton class and add instance methods, class methods, and constants
#You do this by using the 'class' keyword and a CONSTANT
class C
def hello
puts "this is a hello yo."
end
end

module M
def talk
puts "hello."
end
end

#Example 1
example1 = C.new
class << example1
include M
end
#Call the Singleton method
example1.hello
#Call the Module Mix-in
example1.talk

#Example 2
example2 = C.new
example2.extend(M)
example2.talk

source

String split by line

text_with_lines.each do |line|
p line.chomp
end

source

Función básica para eliminiar acentos en minúsculas

def quita_acentos( texto)
texto= texto.gsub('á', 'a')
texto= texto.gsub('é', 'e')
texto= texto.gsub('í', 'i')
texto= texto.gsub('ó', 'o')
texto= texto.gsub('ú', 'u')
end

source

REXML Language Identification monkey patch

require 'rexml/document'

class REXML::Element

public
def lang
if self.attributes['xml:lang']
return self.attributes['xml:lang'].to_s
elsif self.parent != nil
return self.parent.lang
else
return nil
end
end

end

describe "XML library" do
it "should handle xml:lang inheritance properly" do
xmldoc = <<-EOF;
<?xml version="1.0" ?>
<foo xml:lang="en">
<bar>Hello World!</bar>
</foo>
EOF

xml = REXML::Document.new(xmldoc)
xml.elements[1].elements[1].lang.should == "en"

xmldoc2 = <<-EOF;
<?xml version="1.0" ?>
<foo>
<bar>Hello World!</bar>
</foo>
EOF

xml2 = REXML::Document.new(xmldoc2)
xml2.elements[1].elements[1].lang.should_not == "en"
xml2.elements[1].elements[1].lang.should == nil

xmldoc3 = <<-EOF;
<?xml version="1.0" ?>
<foo>
<bar xml:lang="en">Hello World!</bar>
</foo>
EOF

xml3 = REXML::Document.new(xmldoc3)
xml3.elements[1].elements[1].lang.should == "en"
end
end

source