Skip to main content

The Beginning

Today I covered the remainder of S3 and the introduction of EC2.

I read the first pages of ruby under the microscope. I learned what tokenization is and parsing. There is quite some complexity to tokenization and parsing. Ruby is built on top of C. I am picking up the basics


From reading the ruby docs, I learnt:
- Numbers, strings, Arrays, Hashes are actually called Literals
- if, unless, while, until, for, break, next, redo are called Control Expressions. 
- Ternary if can be written as ? :

input_type = gets =~ /hello/i ? "greeting" : "other"

is the equivalent of:

input_type =
  if gets =~ /hello/i
    "greeting"
  else
    "other"

  end

- "unless" is the equivalent of "not true
- You can put the else condition but the elsie after unless.

- case uses the === method.
1)
 case "2"
when /^1/, "2"
  puts "the string starts with one or is '2'"
end

- /^1/ is the regex for a value starting with 1

2) if-elsif expression
a = 2

case
when a == 1, a == 2
  puts "a is one or two"
when a == 3
  puts "a is three"
else
  puts "I don't know what a is"
end

- while Loop executes while a condition is true. Until loop executes while. condition is false.

while a < 10 do
a += 1
end

- For Loop
for value in [1, 2, 3] do
  puts value
end

- the "do" is optional for For, while and Unti.
The for loop is similar to using each, but does not create a new variable scope.
The for loop is rarely used in modern ruby programs.

- modifier means the conditions can be placed behind the a+=1
- begin & end
a = 0

begin
  a += 1
end while a < 10


p a # prints 10

- next
1) Use next to skip the rest of the current iteration:


result = [1, 2, 3].map do |value|
  next if value.even?

  value * 2
end


p result # prints [2, nil, 6]

2) next accepts an argument that can be used as the result of the current block iteration:

result = [1, 2, 3].map do |value|
  next value if value.even?

  value * 2
end


p result # prints [2, 2, 6]
- Use redo to redo the entire loop from begin to end.
- unto aka FlipFlop is cool

selected = []

0.upto 10 do |value|
  selected << value if value==2..value==8
end

p selected # prints [2, 3, 4, 5, 6, 7, 8]

From CodeWars
I did likes.rb.
Learnt about how to use case, %s, %names, and array.each { n } join(" and ") << "jaljalsjdla"

Comments