Truncate string with Ruby/Rails


Ruby:-

1.9.3p125 :079 >   list = “Hi, this is Ruby on rails developer”
=> “Hi, this is Ruby on rails developer”
1.9.3p125 :080 > list.truncate(10)
=> “Hi, thi…”
1.9.3p125 :081 > list.truncate(20)
=> “Hi, this is Ruby …”
1.9.3p125 :083 > list[0..10]
=> “Hi, this is”
1.9.3p125 :084 > list[0..20]
=> “Hi, this is Ruby on r”

1.9.3p125 :108 >   list.slice(0 , 10)
=> “Hi, this i”

Rails:-

– @organization_applied_name.each do |each_org_name|
%li
= link_to “#{truncate(each_org_name, :length => 25)}”, “#”
= link_to “#{each_org_name[0..10]}”, “#”

 

Advertisements

Mongoid Keys, Convert BSON::ObjectId to string, string to BSON::ObjectId


1. Convert BSON::ObjectId to string

$profile = Profile.where(“profiles_manageds.profile_fname”=>’Admin’).first
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :077 >
1.9.3-p125 :078 >
1.9.3-p125 :079 >   profile
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :080 > profile.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :081 >
1.9.3-p125 :082 >
1.9.3-p125 :083 >   profile.id.to_s
=> “4fe969dd79216d0af9000002”

2. Convert string to BSON::ObjectId

$ profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :093 > profile_id = profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :094 >
1.9.3-p125 :095 >
1.9.3-p125 :096 >   profile_id
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :097 >
1.9.3-p125 :098 >
1.9.3-p125 :099 >   BSON::ObjectId.from_string(profile_id)
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)

Heroku useful commands


1. Access heroku database

$ heroku console

2. Delete heroku table

$ ModelName.delete_all

3.  Get all heroku releases

$ heroku releases

kl-new Releases
v31   Deploy cb9e22e   mpravin@maisasolutions.com   14m ago
v30   Deploy 0cee730   mpravin@maisasolutions.com   2012/06/22 11:08:36
v29   Deploy 07e6aa5   mpravin@maisasolutions.com   2012/06/22 10:31:06
v28   Deploy 40c13ba   mpravin@maisasolutions.com   2012/06/20 10:42:13
v27   Deploy 2464731   mpravin@maisasolutions.com   2012/06/19 16:19:16
v26   Deploy 99241c5   mpravin@maisasolutions.com   2012/06/19 15:43:15
v25   Deploy 2c7bb6e   mpravin@maisasolutions.com   2012/06/18 13:40:31
v24   Deploy f600d0a   mpravin@maisasolutions.com   2012/06/15 12:57:57
v23   Deploy b02fc6a   mpravin@maisasolutions.com   2012/06/15 12:45:32
v22   Deploy 79edf6d   mpravin@maisasolutions.com   2012/06/15 12:39:27

4. get detailed info on a release:

$ heroku releases:info v24

5. Roll back to the last release

$ heroku rollback

6. Rollback to specific version

$heroku rollback v31

Ruby Array


Basics of ruby Array.

1.How to create ruby array

1.9.2p318 :002 > order = [“Book”,”Pen”,”Computer”,”Bus”]

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :002 > list = %(car moter rails ruby)

=> “car moter rails ruby”

1.9.2p318 :003 > name = Array.new

=> []

1.9.2p318 :004 > name << ‘Pravin’

=> [“Pravin”]

1.9.2p318 :005 > name << ‘Ankit’

=> [“Pravin”, “Ankit”]

1.9.2p318 :006 > name << ‘Abhay’

=> [“Pravin”, “Ankit”, “Abhay”]

1.9.2p318 :007 > name << ‘Abinaw’

=> [“Pravin”, “Ankit”, “Abhay”, “Abinaw”]

2. collect and map

Invokes block once for each element of self. Creates a new array containing the values returned by the block

1.9.2p318 :030 > order.collect{|item| item}

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :031 > order.collect{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :032 > order

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :033 > order.map{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :034 > order

=> [“Book”, “Pen”, “Computer”, “Bus”]

3. collect! And map!

Invokes the block once for each element of self, replacing the element with the value returned by block

1.9.2p318 :035 > order.map!{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :036 > order

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :037 > order.collect!{|item| item+”2″}

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

1.9.2p318 :038 > order

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

4.compact

Returns a copy of self with all nil elements removed.

1.9.2p318 :050 > order

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

push a nil element to array

1.9.2p318 :059 > order << nil

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, nil]

1.9.2p318 :060 > order.compact

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, “”, “”, “nil”]

5.compact!

Removes nil elements from array. Returns nil if no changes were made.

1.9.2p318 :062 > order << nil

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, nil]

1.9.2p318 :063 > order.compact!

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

1.9.2p318 :064 > order.compact!

=> nil

6.delete_if

Deletes every element of self for which block evaluates to true.

1.9.2p318 :088 > number_list = [‘2′,’8′,’4′,’3′,’9’]

=> [“2”, “8”, “4”, “3”, “9”]

1.9.2p318 :089 > number_list.delete_if{|item| item >= ‘4’}

=> [“2”, “3”]

7.empty?

Returns true if self array contains no elements.

1.9.2p318 :090 > number_list

=> [“2”, “3”]

1.9.2p318 :091 > number_list.empty?

=> false

1.9.2p318 :092 > number_list.delete_if{|item| item > ‘0’}

=> []

1.9.2p318 :093 > number_list

=> []

1.9.2p318 :094 > number_list.empty?

=> true

8.eql?

Returns true if array and other are the same object, or are both arrays with the same content.

1.9.2p318 :097 > number_list << ‘4’

=> [“4”]

1.9.2p318 :098 > number_list << 8

=> [“4”, 8]

1.9.2p318 :099 > number_list << “test”

=> [“4”, 8, “test”]

1.9.2p318 :101 > number_list1 = Array.new

=> []

1.9.2p318 :102 > number_list1 << “test”

=> [“test”]

1.9.2p318 :103 > number_list1 << 5

=> [“test”, 5]

1.9.2p318 :104 > number_list1.eql?(number_list)

=> false

1.9.2p318 :105 > number_list1

=> [“test”, 5]

1.9.2p318 :106 > number_list1.delete_at(1)

=> 5

1.9.2p318 :107 > number_list1

=> [“test”]

1.9.2p318 :108 > number_list

=> [“4”, 8, “test”]

1.9.2p318 :109 > number_list.delete_at(1)

=> 8

1.9.2p318 :110 > number_list.delete_at(0)

=> “4”

1.9.2p318 :111 > number_list

=> [“test”]

1.9.2p318 :112 > number_list1.eql?(number_list)

=> true

9.include?

Returns true if the given object is present in self (that is, if any object == anObject), false otherwise.

1.9.2p318 :124 > number_list

=> [“test”]

1.9.2p318 :125 > number_list.push(5)

=> [“test”, 5]

1.9.2p318 :126 > number_list.push(“apple”)

=> [“test”, 5, “apple”]

1.9.2p318 :127 > number_list.include?(5)

=> true

1.9.2p318 :128 > number_list.include?(‘5’)

=> false

1.9.2p318 :129 > number_list.include?(‘apple’)

=> true

1.9.2p318 :130 > number_list.include?(‘apple1’)

=> false

10.index(obj)

Returns the index of the first object in self such that is == to obj. Returns nil if no match is found.

1.9.2p318 :141 > number_list

=> [“test”, 5, “apple”]

1.9.2p318 :142 > number_list.index(5)

=> 1

1.9.2p318 :143 > number_list.index(‘test’)

=> 0

1.9.2p318 :144 > number_list.index(’empty’)

=> nil

11.join

Returns a string created by converting each element of the array to a string, separated by sep.

1.9.2p318 :155 > number_list

=> [“test”, 5, “apple”]

1.9.2p318 :157 > number_list.join(‘-‘)

=> “test-5-apple”

12.nitems

Returns the number of non-nil elements in self. May be zero.

13. slice!

Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil if the index is out of range. Equivalent to:

1.9.2p318 :220 > number_list

=> [“test”, 5, “dog”]

1.9.2p318 :221 > number_list.slice!(2)

=> “dog”

1.9.2p318 :222 > number_list

=> [“test”, 5]

1.9.2p318 :223 > number_list.slice!(2)

=> nil

1.9.2p318 :224 > number_list

=> [“test”, 5]

1.9.2p318 :225 > number_list.slice!(0)

=> “test”

1.9.2p318 :226 > number_list

=> [5]

14.sort

Returns a new array created by sorting self. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1.

1.9.2p318 :229 > number = [‘2′,’9′,’7′,’4’]

=> [“2”, “9”, “7”, “4”]

1.9.2p318 :230 > number.sort{|a,b| a <=> b}

=> [“2”, “4”, “7”, “9”]

error: AttributeError(“‘module’ object has no attribute ‘binary'”, )


I was facing same issues, After couple of hours google search i found this solutions.

https://github.com/JeffAMcGee/Fang-of-Mongo/commit/202fd95b5a6558fc60d85bd94ec6a9c6a3c14a2e#diff-0

We need to do some update in /home/maisa/workspace/Fang-of-Mongo/fangofmongo/fom

In your case may it differ, Here /home/maisa/workspace is my working directory where, I cloned Fang-of-Mongo.

Go to Fang-of-Mongo/fangofmongo/fom folder and open view.py file and these changes.

https://github.com/JeffAMcGee/Fang-of-Mongo/commit/202fd95b5a6558fc60d85bd94ec6a9c6a3c14a2e#diff-0

That’s working fine for me. Enjoy..!!!!!!!!!!!!!!!1

Apache questions?


1) What’s the command to stop Apache?

->kill the specific process that httpd is running under, or killall httpd. If you have apachectl installed, use apachectl stop.

2) What is mod_vhost_alias?

->It allows hosting multiple sites on the same server via simpler configurations.

3)What is location of log files for Apache server ?

->/var.log/httpd

4)How to restart Apache web server ?

->service httpd restart

5)What does htpasswd do?

->It creates a new user in a specified group, and asks to specify a password for that user.

6)On which port Apache server works ?

->http – port 80
https – port 443

7)Name of configuration file of Apache server ?

->httpd.conf

8) Which tool you have used for Apache benchmarking?

->ab (Apache bench)

9.How do you change the default web root?

->The solution is to change the DocumentRoot in httpd.conf file.

10.Can we limit on uploads on your web server?

>This can be achieved by LimitRequestBody directive.

What are the differences betweeen Rails 2.x and Rails 3


(1) Introduction of bundler (New way to manage your gem dependencies) * 
(2) Gemfile and Gemfile.lock (Where all your gem dependencies lies, instead of environment.rb) * 
(3) A new .rb file in config/ folder, named as application.rb
 (Which has everything that previously environment.rb had) * 
(4) Change in SQL Structure: Model.where(:activated => true) * 
(5) All the mailer script will now be in app/mailers folder, 
earlier we kept inside app/models. * 
(6) Rails3-UJS support. for links and forms to work as AJAX, 
instead of writing complex lines of code, we write :remote => true * 
(7) HTML 5 support. * 
(8) Changes in the model based validation syntax: validates :name, :presence => true * 
(9) Ability to install windows/ruby/jruby/development/production specific gems to Gemfile. 
group :production do 
 gem 'will_paginate' 
end