Skip to main content

Generate Random string in Ruby On Rails

##method 1
def random_password(size = 6)
chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..size).collect{|a| chars[rand(chars.size)] }.join
end
 puts random_password.inspect

##method 2
 def newpass(len=6)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  newpass = ""
  upto(len) { |i| newpass << chars[rand(chars.size-1)] }
 return newpass
end
p newpass.inspect

##method 3
def generate_string(a)
new_code = random_string(6)
end
p  new_code.inspect

###correct_method
##method 4
def random_string(length = 6)
chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
(1..length).map {chars[rand(chars.length)]}.join
end
p random_string.inspect

##method 5
def random_string(length = 6)
rand(36**length).to_s(36)
end
 p random_string.inspect

##method 6
def random_string
 o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
 string = (0..50).map{ o[rand(o.length)] }.join
end

##method 7
def generate_activation_code(size = 6)
charset = %w{ 2 3 4 6 7 9 A C D E F G H J K L M N P Q R T V W X Y Z}
(0...size).map{ charset.to_a[rand(charset.size)] }.join
end
p generate_activation_code.inspect

##method 8
def generate_random_string(length=6)
 string = ""
 chars = ("A".."Z").to_a
 length.times do
 string << chars[rand(chars.length-1)]
end
string
end
p generate_random_string.inspect

##method 9
def random_alphanumeric(size=16)
      chars = ('a'..'z').to_a + ('A'..'Z').to_a
    (0...size).collect { chars[Kernel.rand(chars.length)] }.join
end

##method 10
def random_string
    chars = ("a".."z").to_a + ("1".."15").to_a
    newpass = Array.new(8, '').collect{chars[rand(chars.size)]}.join
end
 p random_string.inspect

Comments

Popular posts from this blog

Error malloc(): memory corruption nginx with passenger?

Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

PayUMoney Integration in Ruby on Rails

PayUMoney Integration in Ruby on Rails Generating Cheksum in Payu    Hash or Checksum = sha512(key|txnid|amount|productinfo|firstname|email|u df1|udf2|udf3|udf4|udf5||||||salt) Controller Code Will be def payumoney   @key = "gtKFFx"   @salt =  "eCwWELxi"   val = "#{key|txnid|amount|productinfo|firstname|email|u df1|udf2|udf3|udf4|udf5||||||#{@salt}"  @checksum = Digest::SHA512.hexdigest("#{val}") end <form action="https://test.payu.in/_payment" method='post' id='sub'  >     <input type="hidden" name="key" value="<%=@key%>">     <input type="hidden" name="hash" value="<%=@checksum%>">     <input type="hidden" name="txnid" value="TXN1234">     <input type="hidden" name="amount" value="200">     <input type="hidden" name=...

RSPEC TESTCASES FOR MODEL VALIDATIONS IN RAILS

describe User do    before(:each) do     @user_new = { :name => "chinna",:email => "chinna@yopmail.com",:password => "chinnaram",:password_confirmation => "chinnaram"}    end it "should create a new user details" do      @user_attr = User.create!(@user_new) end ##validations in your model ########### User.rb(your model your mention like that....) validates :name,:email,:presence => true validates :name,:uniqueness => true validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png','image/pjpeg','image/x-png','image/gif'] ###validation for name presence it "should require a name" do      user = User.new(@user_attr.merge(:name => ""))      user.should_not be_valid   end #validation for email presence  it "should require a email" do      user = User.new(@user_attr.merge(:email => ""))      user...