Different ways of Iterating over an Array in Ruby (Just for me)

1) .each do |val| … end

2) .each_with_index do |val, index| … end

3) a) .map do … end  => returns new array

b) .map.with_index do |val, index| … end

4) .any? do |val| {condition} end   => returns true or false

5) .all? do |val| {condition} end   => returns true if all value satisfy the condition otherwise false

6) .select do |val| {condition} end   => returns an array of those values which satisfy condition.

7) .find do |val| {condition} end   => returns first value which passes condition.

8) .find_all do |val| {condition} end   => returns all those value which satisfy conditon. It’s equivalent to .select

9) .inject([initial_value_of_var1]) do |var1, var2 | … end => returns var1

 

Advertisements

Loops and Repetitions in Ruby

1) times:

[integer].times do |i| ... end

e.g:

5.times do |i|
  puts "iteration: #{i}"
end

2) upto:

[start_val].upto [end_val] do |i| ... end

e.g:

1.upto 5 do |i|
  puts "iteration: #{i}"
end

3) downto:
it’s inverse of upto.

[end_val].downto [start_val] do |i| ... end

e.g:

5.downto 1 do |i|
  puts "iteration: #{i}"
end

4) step:

We can do both upto and downto with step loop. It’s very flexible.

a) [start_val].step [end_val] do |i| ... end
e.g:
1.step 5 do |i|
  puts "Iteration: #{i}"
end

for stepping:

b) [start_val].step [end_val], [incremental_val] do |i| ... end
e.g: 
1.step 5, 2 do |i|
  puts "Iteration: #{i}"
end

like downto:

c) [end_val].step [start_val], -1 do |i| ... end
e.g: 
5.step 1, -1 do |i|
  puts "Iteration: #{i}"
end

5) while
a)

while conditional [do]
  code
end

e.g:
i = 1
while i <= 5 do
  puts "Iteration: #{i}"
  i += 1
end

b)

code while condition

OR

begin 
  code 
end while conditional

e.g:
i = 1
begin
  puts "Iteration: #{i}"
  i +=1
end while i < 5

7) until
It’s similar to while but having reverse condition.

e.g:
i = 1
until i > 5 do
  puts "Iteration: #{i}"
  i += 1
end

8) for

for [var_name] in [rang] do ... end
e.g: 
for i in (1..10) do
  puts "Iteration: #{i}"
end

9) each

[rang].each do |[var]| ... end
e.g: 
(1..10).each do |i|
  puts "Iteration: #{i}"
end

Debugging Methods

Here are few methods that we can use for debugging:

1) Kernal#Caller

 caller.inspect

shows you where you have been.

It will give you “file:line in method”

2) Method

It is a class.

m = "Hello World".method(:upcase)
m.class
=> Method
m.call
=> "HELLO WORLD"

3) source_location

It will give address and line number. It shows you where you are going.

Dog.new.method(:bark).source_location

It will give you address with line number of this above(:bark) method.

4) ancestors

"foo".class.ancestors
=> [String, Comparable, Object, Kernal, BasicObject]

In class method you can simply call it like:

self.class.ancestors

5) Module#method_defined?

"foo".class.method_defined?(:upcase)
=> true
99.class.method_defined?(:upcase)
=> false

6) Method#instance_method

Instead of

User.new.method(:github_url).source_location

you can use:

User.instance_method(:github_url).source_location

.

here we don’t need to create instance.

TheRequest Interview Question’s Answer

1)
Array manipulation in Ruby

Suppose you have the following array

stuff = [:dog,:cat,:orange,:banana]
How can you slice this array to create a new array [:cat,:orange]
Add the element :apple on to the end of the array.
Now take :apple back off again
Add the element :fish to the start of the array.
Now remove the element :fish.

Solution:

def example(arr=[])
  arr = arr[1..(-2)]
  puts "1. struff=> #{arr.inspect}"
  
  arr.insert((-1), :apple)
  puts "2. struff=> #{arr.inspect}"
  
  arr.pop
  puts "3. struff=> #{arr.inspect}"
  
  arr.insert(0, :fish)
  puts "4. struff=> #{arr.inspect}"
  
  arr.shift
  puts "5. struff=> #{arr.inspect}"
  
end

example([:dog,:cat,:orange,:banana])

Output:

1. struff=> [:cat, :orange]
2. struff=> [:cat, :orange, :apple]
3. struff=> [:cat, :orange]
4. struff=> [:fish, :cat, :orange]
5. struff=> [:cat, :orange]

2)
Characters in Strings in Ruby

Implement a function with signature find_chars(string1, string2) that takes two strings and returns a string that contains only the characters found in string1 and string two in the order that they are found in string1. Implement a version of order N*N and one of order N.

Solution:

def find_chars(string1, string2)
  (string1.split('') & string2.split('')).join
end

puts find_chars("test", "string")

Output:

ts