Monthly Archives: September 2012

Intro to Splat (*) operator in Ruby.

I usually use following to create a bunch of array elements in Ruby.

arr = (10..50).to_a     #converting a range to an array.

Today I noticed following which performs same operation:

arr = [*10..50]       # splat operator

My reaction was :
“Awesome!!!!”

So, then, one day I took some time to investigate more on this (*) operator.
Following are few which I could come to know.

1) Generally used in “Method definition with variable no. of parameter” (I have used it but don’t know it was implementation of splat operator (^o^) )

def demo(*numbers)
  numbers.each { |num| puts "#{num}" }
end
demo(1,2,3,4)

#output
1
2
3
4

2) Converting an array into list of arguments in method calling.
In this case, the splat converts the array into method arguments.

def demo(arg1, arg2, arg3)
  puts "arg1 is #{arg1}, arg2 is #{arg2} and arg3 is #{arg3}"
end
arr = [10, 20.45, "hello"]
demo *arr

#output
arg1 is 10, arg2 is 20.45 and arg3 is hello

3) Datatype coercion
Splat operator can be used to convert interesting datatype coercion.

3.1) String into Array : Splat can also be used to coerce string values into array.

arr = *"Hello"  #=> ["Hello"]
"Hello".to_a  #=> NoMethodError: undefined method `to_a' for "Hello":String

Note : This will only create array of size 1.

3.2) Range into Array :

arr = *(10..20)     # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Above is similar behavior as ‘arr = (10..20).to_a’

3.3) Hash into an Array:

arr = *{ :a => "111", :b => "222" }    # [[:a, "111"], [:b, "222"]]

Note : Use ‘Hash[*arr.flatten]’ to reverse it.

4) Use in ‘case’ statement :

male = ["ram", "rahul", "karan"]
female = ["kareena", "aish", "juhi", "katerina"]
person = "aish"

case person
when *male
  puts "Male"
when *female
  puts "Female"
end

#output
Female

5) Interesting Array data retrieval:

arr = ["one", "two", "three", "four"]

first, *others = arr         #first = "one", others = ["two", "three", "four"]
*others, last = arr          #others = ["one", "two", "three"] , last = "four"
first, *center, last = arr   #first = "one", center = ["two", "three"], last = "four"  

Getting started with Vim

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. Vim is distributed free as charityware.

Its navigation system include:

  1. Line navigation
  2. Screen navigation
  3. Word navigation
  4. Special navigation
  5. Paragraph navigation
  6. Search navigation
  7. Code navigation
  8. Navigation from command line

1. Vim Line Navigation

Following are the four navigation that can be done line by line.

  • k – navigate upwards
  • j – navigate downwards
  • l – navigate right side
  • h – navigate left side


By using the repeat factor in VIM we can do this operation for N times. For example, when you want to
go down by 10 lines, then type “10j”.

Within a line if you want to navigate to different position, you have 4 other options.

  • 0 – go to the starting of the current line.
  • ^ – go to the first non blank character of the line.
  • $ – go to the end of the current line.
  • g_ – go to the last non blank character of the line.

2. Vim Screen Navigation

Following are the three navigation which can be done in relation to text shown in the screen.

  • H – Go to the first line of current screen.
  • M – Go to the middle line of current screen.
  • L – Go to the last line of current screen.
  • ctrl+f – Jump forward one full screen.
  • ctrl+b – Jump backwards one full screen
  • ctrl+d – Jump forward (down) a half screen
  • ctrl+u – Jump back (up) one half screen

3. Vim Special Navigation

You may want to do some special navigation inside a file, which are:

  • N% – Go to the Nth percentage line of the file.
  • NG – Go to the Nth line of the file.
  • G – Go to the end of the file.
  • `” – Go to the position where you were in NORMAL MODE while last closing the file.
  • `^ – Go to the position where you were in INSERT MODE while last closing the file.
  • g – Go to the beginning of the file.

4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

  • e – go to the end of the current word.
  • E – go to the end of the current WORD.
  • b – go to the previous (before) word.
  • B – go to the previous (before) WORD.
  • w – go to the next word.
  • W – go to the next WORD.

WORD – WORD consists of a sequence of non-blank characters, separated with white space.
word – word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

  • 192.168.1.1 – single WORD
  • 192.168.1.1 – seven words.

5. Vim Paragraph Navigation

  • { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
  • } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.

6. Vim Search Navigation

  • /i – Search for a pattern which will you take you to the next occurrence of it.
  • ?i – Search for a pattern which will you take you to the previous occurrence of it.
  • – Go to the next occurrence of the current word under the cursor.
  • – Go to the previous occurrence of the current word under the cursor.

7. Vim Code Navigation

% – Go to the matching braces, or parenthesis inside code.

8. Vim Navigation from Command Line

Vim +N filename: Go to the Nth line of the file after opening it.

vim +10 /etc/passwd


Vim +/pattern filename: Go to the particular pattern’s line inside the file, first occurrence from first. In the following example, it will open the README file and jump to the first occurrence of the word “install”.

vim +/install README


Vim +?patten filename: Go to the particular pattern’s line inside the file, first occurrence from last. In the following example, it will open the README file and jump to the last occurrence of the word “bug”.

vim +?bug README

Ruby Quirks (You would amaze at first point)

Right now i am at the stage of learning. (I guess this line applies to every people who are associated with ruby (^o^) since learning never ends )

While studying Ruby i am finding(**still continuing) that language looks quite simple but has its own complex and hidden secrets.

So, i am writing my experience of unexpected or peculiar behavior (Quirks) i have observed or will be going in future in this post.

1) Why following statement results into ‘nil’ ?

expand = defined?( expand ) ? expand : true

Assume that expand encountered here for the first time.

2) In the following Ruby code, x gets the value nil and pqr remains an undefined local variable. Why?

if false
  x = pqr
end
puts x
puts pqr

3) What is happening in following code and why, how?

x = 5
y = 10
x, y = y, x
puts x
puts y

4) Why expression 1 == 1.0 evaluates to true ?
Check following irb session:

> 1.class   #output Fixnum
> 1.0.class #output Float
> 1 == 1.0  #output true

5) In case of multiple values at right hand side(RHS) of assignment, the rules of parallel assignment come into play. First, all the RHS values evaluated, left to right, and collected into an array (unless they are already an array). This array will be the eventual value returned by the overall assignment. Next, the left hand side (lhs) is inspected. If it contains a single element, the array is assigned to that element otherwise values get corresponding assignment.

a = 1, 2, 3, 4             # => a == [1, 2, 3, 4]  
b = [1, 2, 3, 4] (Array)   # => b == [1, 2, 3, 4]  
c,d = 1,2,3,4              # => c = 1, d = 2