you are in: codestackercodes [RSS] → tag: ruby [RSS]

how to use number_to_currency in controller Delicious Email

show/hide lines
   1  include ActionView::Helpers::NumberHelper
created by leozera — 26 August 2011 — get a short url — tags: helper rails ruby embed

simple random string method Delicious Email

show/hide lines
   1  def rand_str(length)
   2  	return rand(36**length).to_s(36)
   3  end
created by leozera — 24 July 2011 — get a short url — tags: ruby embed

simple random password method Delicious Email

show/hide lines
   1  def random_password(size = 8)
   2  	charset = %w{ 2 3 4 6 7 8 9 A B C D E F G H J K L M N P Q R T V W X Y Z}
   3  	(0...size).map{ charset.to_a[rand(charset.size)] }.join
   4  end
created by leozera — 25 June 2011 — get a short url — tags: password ruby embed

rails 3 validates method Delicious Email

show/hide lines
   1  validates :login, :presence => true, :length => {:minimum => 4}, :uniqueness => true, :format => { :with => /[A-Za-z0-9]+/ }
   2  
   3  # :presence => true
   4  # :uniqueness => true
   5  # :numericality => true
   6  # :length => { :minimum => 0, maximum => 2000 }
   7  # :format => { :with => /.*/ }
   8  # :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } # email format
   9  # :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix } # url format
  10  # :inclusion => { :in => [1,2,3] }
  11  # :exclusion => { :in => [1,2,3] }
  12  # :acceptance => true
  13  # :confirmation => true
created by leozera — 05 June 2011 — get a short url — tags: rails ruby validation embed

rails 3 money validation Delicious Email

show/hide lines
   1  validates :price, :presence => true, :format => { :with => /[0-9\,]*/ }
created by leozera — 04 June 2011 — get a short url — tags: ruby validation embed

rails 3 url validation Delicious Email

show/hide lines
   1  # paste in your model
   2  
   3  validates :url, :presence => true, :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix }
created by leozera — 23 May 2011 — get a short url — tags: rails embed

grep an object's available methods Delicious Email

show/hide lines
   1  Object.methods.grep /inspect/
   2  => ["inspect", "pretty_inspect", "pretty_print_inspect"]
created by leozera — 28 March 2011 — get a short url — tags: ruby embed

return the url base in ruby Delicious Email

show/hide lines
   1  >> url = "http://www.codestacker.com/tag/ruby"
   2  >> url[/https?:\/\/.[^\/]+/]
   3  => "http://www.codestacker.com"
created by leozera — 27 March 2011 — get a short url — tags: ruby embed

activerecord without rails Delicious Email

show/hide lines
   1  require 'active_record'
   2  
   3  ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :host => 'localhost', :user => 'root', :password => 'mypass', :database => 'mydatabase')
   4  
   5  class User < ActiveRecord::Base ; end
   6  
   7  puts User.count
   8  #=> 9
created by leozera — 11 February 2011 — get a short url — tags: activerecord rails embed

writing a file Delicious Email

r – Open a file for reading. The file must exist.
w – Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a – Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
r+ – Open a file for update both reading and writing. The file must exist.
w+ – Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a+ – Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

show/hide lines
   1  doc = "hello world"
   2  
   3  File.open(my_file, 'w') {|f| f.write(doc) }
created by leozera — 06 February 2011 — get a short url — tags: ruby embed
Displaying records 1 - 10 of 81