||= operator in Ruby
x ||= y is similar to x || x = y.
In the above statement, if x evaluates to logical true then assignment is not done.
If x evaluates to logical false only then the assignment is done.
Though the above two statements are similar, there are subtle differences.
# if x is not defined
puts x || x=y ## Undefined local variable or method `x`
puts x ||= y ## prints y
Related cards:
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
# R...
Adobe flash player installation in ubuntu chromium browser
For Chromium browser versions greater than 34, to use flash one must install Pepper flash.
For Ubuntu 14.04 or newer, run
sudo apt-get install pepperflashplugin-nonfree
sudo update-pepperflashplugin-nonfree --status
# to see what version of...
Adobe flash player installation in Ubuntu
This is to install adobe flash player in Ubuntu 14.04 or higher. This works for Firefox browser.
If you have a 32-bit OS, run
sudo apt-get update
sudo apt-get install flashplugin-installer
Make sure each of the commands executed success...
Using send method in Ruby
In Ruby, send is often introduced as a way to show how everything is an object
1.send(:+, 1) ## -> 2
It can also be used to call private or protected methods from outside a given class as explained below.
class Myclass
def method...
$: in Ruby
In Rails programming you might seldom come across the below statement
$:.unshift File.dirname(__FILE__)
$: represents load path in Ruby.
Its a pre-defined variable and short hand for $LOAD_PATH which ruby uses to look for files.
unshif...
detect and select method 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 ...
Isues w/ caching in Incognito mode
I've noticed that Incognito mode retain cache from the previous session, it just don't collect any new cache while in incognito.
I usually clear the cache/cookies then go into incognito, or run a different browser profile, or a different browser ...
change string encoding in ruby
# Ruby 1.9
string_in_utf8_encoding = string_in_latin1_encoding.encode('UTF-8')
#Ruby 1.8
gem install iconv
require 'iconv'
string_in_utf8_encoding = Iconv.conv("UTF8", "LATIN1", string_in_latin1_encoding)
However, if you want to let Rub...
Check if 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>
In Redhat / Fedora / CentOS use the foll...
difference between attr_accessor and initialize
attr_accessor is used to create setter and getter methods for instance variables so that they can be accessed outside the class. initialize is the special method in ruby that gets called when an object is instantiated.
class Birthday
attr_ac...