Posts

Showing posts from July, 2015

Valid Email Regex

I tried writing a perfect email regex which can be supported for almost every standard website like google, facebook etc. \A[a-zA-Z0-9]*[\.?*[a-zA-Z0-9]]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+){1,3}\z Please check on this regex if you find better please reply:

Why to use symbol rather than a string in ruby

Symbols are abstract references represented, typically, by a short string prefixed with a colon.   Examples include :blue , :good , and :name . Sadly, there is no succinct, easy-to-learn trick to symbols, so you’ll need to read this whole section—maybe even more than once—to get it to stick. Among mainstream languages, symbols are reasonably unique to Ruby (although Lisp and Erlang do have similar concepts) and tend to confuse most new users, so let’s jump straight into an illustrative example: current_situation = :good puts "Everything is fine" if current_situation == :good puts "PANIC!" if current_situation == :bad Everything is fine In this example, :good and :bad are symbols. Symbols don’t contain values or objects , like variables do. Instead, they’re used as a consistent name within code. For example, in the preceding code, you could easily replace the symbols with strings, like so: current_situation = "good" puts "Everything is fine&

How to find out all descendants of ActiveRecord::Base

To find out all descendants of ActiveRecord::Base you need to first eager load the Rails application. Rails.application.eager_load!  Then find out all the descendants models  ActiveRecord::Base.descendants To find out all descendants without STI ActiveRecord::Base.direct_descendants

Aws basic commands in ruby

First create an object of s3 client through loading yml of aws_credential which looks like: aws_credentails:   access_key_id: access_key_id   secret_access_key: secret_access_key   region: region_name   source_upload_bucket: bucket_name   rails_bucket: rails_bucket_name   ipad_bucket: ipad_bucket_name(if exists)   application_arn: if_exists name_prefix: prefix_name_of_bucket s3_client= Aws::S3::Client.new(aws_credentials) 1.) To list all buckets:  s3_client.list_buckets 2.) Listing objects of a particular bucket: s3_client.list_objects(bucket:"bucket-name") 3.) Listing all key of a bucket in an array: s3_client.list_objects({bucket: "bucket-name"})[:contents].map{|x| x.key} 4.) To delete a key from a bucket:  s3_client.delete_object(bucket: "bucket-name", key:"10_sheet.zip_256") 5.) To copy existing directory to new directory: def copy_existing_dir_to_new_dir(bucket_name=bucket-name)     responce = s3.list_o