Skip to content

Instantly share code, notes, and snippets.

View sandheepg's full-sized avatar

Sandeep Guduru sandheepg

View GitHub Profile
@sandheepg
sandheepg / ruby-include-extend.md
Created March 8, 2017 07:47
Difference between include and extend in ruby

The extend method will mix a module’s methods at the class level.

On the other hand, the include method will mix a module’s methods at the instance level, meaning the methods will become instance methods of the class.

module Stringify
  # Requires an instance variable @value
  def stringify
    if @value == 1
      "One"
@sandheepg
sandheepg / rails-abstract-class.md
Last active March 8, 2017 07:46
Abstract classes in rails
class AbstractModel < ActiveRecord::Base  
  self.abstract_class = true
end

class Foo < AbstractModel
end

class Bar < AbstractModel
end
@sandheepg
sandheepg / class-object-methods.md
Created March 8, 2017 07:41
Listing methods of class and object in ruby
class Myclass
  def one
    puts "one is called"
  end
end    

ss = Myclass.new

puts Myclass.methods 
@sandheepg
sandheepg / root.md
Created March 8, 2017 07:39
Get root access in unix

To access root user privileges in Unix,

sudo su
## prompts for password and grants access

sudo su -
## switch as root user without password
@sandheepg
sandheepg / detect-select.md
Created March 8, 2017 07:37
Difference between detect and select methods in ruby

detect method in ruby returns the first item in the collection for which the block returns TRUE and returns nil if it doesn't find any. It has an optional argument containing a proc that calculates a “default” value — that is, the value to return if no item in the collection matches the criteria without returning nil.

find is the alias for detect and find_all is an alias for select.

[1,2,3,4,5,6,7].detect { |x| x.between?(3,6) }
# 3
[1,2,3,4,5,6,7].select { |x| x.between?(3,6) }
# [3,4,5,6]
[1,2,3,4,5,6,7].find { |x| x.between?(13,61) }
@sandheepg
sandheepg / collect-map.md
Created March 8, 2017 07:36
Collect or map in ruby

map is an alias to collect. It basically returns an array by performing some operation on every item in the given collection. It expects a block containing the operation.

[1, 2, 3, 4].map {|number| number ** 2}
# [1, 4, 9, 16]

In the above example, note that the result has same number of elements as given array.

@sandheepg
sandheepg / rake-tasks.md
Created March 8, 2017 07:31
Understanding rake tasks

Rake is a build language (like make and ant), an internal DSL build in Ruby language.

task :task_name => [:clean] do
  ## ruby code comes here
  ## ruby code comes here
  ## ruby code comes here
end

task is a method that accepts two parameters, a hash and a block. The key in the hash is task name and the value is an array which contains the dependencies (optional). You can remove the square braces in case of single dependency and the value for the hash can be removed in case of no dependency.

@sandheepg
sandheepg / redirect-with-object.md
Last active March 8, 2017 07:29
Passing objects using redirect_to in rails

Scenario : You have some manipulation done in your controller action and redirect the user to another action after that. Now you want the output of the manipulation available in the page user is redirected to.

You can pass the object with redirect_to but that will be added as a query parameter. redirect_to is always a GET request and the params are always visible to the user.

To achieve this you can either store the stuff in the session and clear it once you are done or pass the arbitrary objects to the template using flash. Though its a poor practice, it serves the purpose.

@sandheepg
sandheepg / package-details.md
Created March 8, 2017 07:23
Check if a package is installed in linux

To check if a given package is installed in Debian / Ubuntu, type

dpkg -s <package-name>

To get a neater output use dpkg-query which accepts wildcards as well.

dpkg-query -l <package-name>
@sandheepg
sandheepg / ack-grep.md
Created March 8, 2017 07:22
Using ack in place of grep

To use Ack, make sure you have Perl installed and run the following commands in Ubuntu

sudo apt-get install ack
sudo apt-get install ack-grep

Now you can use ack-grep command to search for matches in the files

ack-grep -i some-text