Quick Ruby Review

December 10th, 2012

Getting ready for a technical interview involving Ruby? Check out this simple program that summarizes a few key features of Ruby.

# Simple node class
class Node
	attr_accessor :next
	attr_accessor :data

	def initialize(n, data)
		@next = n
		@data = data
	end

	def to_s
		@data
	end
end

# Simple linked list
class LinkedList
private
	attr_accessor :root
public

	def initialize
		@root = nil
	end

	def push(data)
	  if @root.nil?
	  	@root = Node.new(nil, data) 
	  else
	  	new_node = Node.new(@root, data)
	  	@root = new_node
	  end
	
	end

	def pop
		return nil if @root.nil?

		root = @root
		if @root.next != nil
			@root = @root.next
		else
			@root = nil
		end
		root
	end

	def print
		current = @root
		while not current.nil?
			puts current.data
			current = current.next
		end
	end

end

# Linked list usage
list = LinkedList.new
list.push("Hello 1")
list.push("Hello 2")
list.push("Hello 3")
list.print
puts "Removed: #{list.pop}"
list.print
puts "Removed: #{list.pop}"
list.print
puts "Removed: #{list.pop}"
list.print
puts "Removed: #{list.pop}"
list.print

# Print items on the same line
print "same"
print " line\n"

# Case-when
var = "test2"
puts (case var
	when "test"
		"OK"
	when "test2"
		"OK2"
	else
		"OKELSE"
	end)

# Hash table operations
hash = {}
hash["key"] = "value"
hash[5] = 10
hash.each do |key, value|
		puts "Key: #{key} Value: #{value}"
end
hash.delete("key")

for i in 0...hash.count do
	puts i
end

# List operations
list = []
list[0] = "hey!"
list.push "haha"
puts list.pop
list.each do |i|
	puts i
end
puts list.count

# String operations
str = "this"
str.each_char do |c|
	puts c
end
puts "---"
for i in 0...str.length do
	puts str[i]
end

# Do-while in ruby
i = 0
begin
 puts "hello"
 i += 1
end while i < 5 

# While
i = 0
while i < 5 do
	puts "hi"
	i += 1
end

# List length
list = [1,2,3]
puts list.length
puts "----"

# Select, reject, collect, inject
=begin 
Use select or reject if you need to select or reject items based on a condition.
Use collect if you need to build an array of the results from logic in the block.
Use inject if you need to accumulate, total, or concatenate array values together.
Use detect if you need to find an item in an array. 
=end
a = [1,2,3,4]
puts a.select{ |i| i % 2 == 0 } # Iterate each, true ones get added
puts a.reject{ |i| i % 2 == 0 } # Iterate each, false ones get added
puts a.collect{ |i| i**2 } # Iterate each, insert expression
puts a.inject([]){ |accumulator, i| accumulator << i+1; accumulator } #Iterate each, add to accumulator each element

# Sort
puts a.sort{|a,b| b<=>a} #reverse order, not changing a
puts %w(aaa bb c).sort_by{|a| a.length }

# Print string ascii components
str = "hello"
puts str[0]
str.length.times { |i| puts str[i].ord }

# Yield example
def yield_example(p)
	puts "Before yield"
	yield p # Call code block
	puts "After yield"
end

yield_example("hello") do |param|
	puts "Received: #{param}"
end

0 Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.