Ruby Update Part 1.

I’m walking myself through the Ruby Course, by Brian Schroder, and using SciTE as my temporary IDE. Here are a few things that made me scratch me head.

Single and double quotation marks matter a lot:

puts #{multi_foo(2)} #outputs just text
puts #{multi_foo(2)} #outputs results of function ‘multi_foo(2)’

Stacks and queues can be implemented two ways to get the same behavior, but the container (array) is way different.

Stack as I know it:

stack = Array.new()
stack.push(‘a1’)
stack.push(‘a2’)
stack.push(‘a3’)
puts stack.pop until stack.empty?
Queue as I know it:

queue = Array.new()
queue.push(‘q1’)
queue.push(‘q2’)
queue.push(‘q3’)
puts queue.shift until queue.empty?
No-no stack:

stack = Array.new()
stack.unshift(‘s1’)
stack.unshift(‘s2’)
stack.unshift(‘s3’)
puts stack.shift until stack.empty?
No-no queue:

queue = Array.new()
queue.unshift(‘q1’)
queue.unshift(‘q2’)
queue.unshift(‘q3’)
puts queue.pop until queue.empty?

I also came across some funky data type, the DEQUE/DEQUEUE” – a double-ended queue. I’m not sure how to use this in real life, can anyone give me a good example? I’m still trying to get me head wrapped around this data structure…

Deque 1:

deque = Array.new()
deque.unshift(‘unshift1’)
deque.unshift(‘unshift2’)
deque.unshift(‘unshift3’)
deque.push(‘push1’)
deque.push(‘push2’)
deque.push(‘push3’)
puts deque.shift until deque.empty?
Deque 2:

deque = Array.new()
deque.unshift(‘unshift1’)
deque.unshift(‘unshift2’)
deque.unshift(‘unshift3’)
deque.push(‘push1’)
deque.push(‘push2’)
deque.push(‘push3’)
puts deque.pop until deque.empty?

Right now, I’m trying to get a good understanding of the hashes, iterators and blocks. It’s not new to me, but the syntax hurts. After a few exercises, hopefully I’ll get it.

Oh yeah, and Happy Valentine’s Day.

One thought on “Ruby Update Part 1.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve this: Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.