Posts Tagged 'Tips'

Change default user shell

Example of how to use chsh command to change current user shell to bash:

chsh -s /bin/bash

Git diff pacth

How to create a patch with git diff:

git diff HEAD~2 > changes.diff


How to use it:

patch -Np2 -i changes.diff

thanks to Andy ;)

Hash to Object

Sometimes I have the need to create objects that responds to some methods with a specific values.
Something that I can use like
HashObject.new :method1 => value_for_method1, :method2 => value_for_method2
There is already a way to do it, with OpenStruct, but I created a HashObject. Just for academic proposals. I didn’t knew the OpenStruct at the time.

class HashObject
  def initialize(hash)
    hash.each do |k,v|
      self.instance_variable_set("@#{k}", v)
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
    end
  end

  def to_hash
    hash_to_return = {}
    self.instance_variables.each do |var|
      hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
    end
    return hash_to_return
  end
end

Git clone from remote repositorium with ssh and non default port

just a reminder, suppose your ssh port is 3022: (sometimes I swap the way to specify the port in those two commands)

git clone ssh://user@domain.com:3022/~/Projects/my_project

ssh user@domain.com -p 3022


(when connecting through ssh the non default port goes as param, the default port is 22)

Get all values of a single column efficiently

Imagine you want this:

@names = MyModel.find(:all).map{ |i| i.name }.uniq

Yes, that works! You get all distinct names of one column, you can even do something like that:

#Using Shortcut Blocks with Symbol to_proc
@names = MyModel.all.map(&:name).uniq

Cool, rely small code(almost seems I’m using Haskel) but not that efficient if your model’s table has lots of columns. (Imagine! The whole table is being loaded to memory) So what is the solution?

MyModel.find( :all, :select > 'DISTINCT name' )

If you specify wich columns you need, in the sql query, you will spare some memory. And in my earlier example you can also pass the “uniq” work to the database.

will_paginate without losing params

I’m sure you like will_paginate, it seams magic! But I was having the issue, that I simply solved like that:

will_paginate(@reports, :params => params.except(:page))

How to Install Skype (Ubuntu)

Easy as it is. Just add the repository into your rep. list, update the package information and install Skype.

Open your list:

sudo vim /etc/apt/sources.list

Add this line at the end:

deb http://download.skype.com/linux/repos/debian/ stable non-free

Update:

sudo aptitude update

Install skype, answer yes to all question(but think for your self!)

sudo aptitude install skype

If you have some problems with audio(like I had) try this lines:


killall pulseaudio
sudo aptitude remove pulseaudio
sudo aptitude install esound
sudo rm /etc/X11/Xsession.d/70pulseaudio

(I’m using Ubuntu 8.10, but also works with Ubuntu 9.10)


Last Regedor’s twitts

Last Regedor's bookmarks