Loading
Abi Travers

What I am learning as a Software Engineering & Growth Hacker.

Regular Expression Resources | Regex

Howdie Reader, or mum as I otherwise call her,

So week 4 of Makers Academy Pre-course and I am still working away at my codewars kyu ranking.

Since the last challenge proved too much for me I decided to go for a simpler one. One with a ranking of 7 Kyu.

The one I chose was on regular expression and this lead me to discovering some very useful resources.

A Quick reference for regular expression Terms:

Regular expression quick references REGEX

Here is another useful list of quick references (This one is from Ruby Kickstarter cheatsheet):

# .             any character except newline
# [ ]           any single character of set
# [^ ]          any single character NOT of set
# *             0 or more previous regular expression
# *?            0 or more previous regular expression (non-greedy)
# +             1 or more previous regular expression
# +?            1 or more previous regular expression (non-greedy)
# ?             0 or 1 previous regular expression
# |             alternation
# ( )           grouping regular expressions
# ^             beginning of a line or string
# $             end of a line or string
# {m,n}         at least m but most n previous regular expression
# {m,n}?        at least m but most n previous regular expression (non-greedy)
# \1-9          nth previous captured group
# \&            whole match
# \`            pre-match
# \'            post-match
# \+            highest group matched
# \A            beginning of a string
# \b            backspace(0x08)(inside[]only)
# \b            word boundary(outside[]only)
# \B            non-word boundary
# \d            digit, same as[0-9]
# \D            non-digit
# \S            non-whitespace character
# \s            whitespace character[ \t\n\r\f]
# \W            non-word character
# \w            word character[0-9A-Za-z_]
# \z            end of a string
# \Z            end of a string, or before newline at the end
# (?#)          comment
# (?:)          grouping without backreferences
# (?=)          zero-width positive look-ahead assertion
# (?!)          zero-width negative look-ahead assertion
# (?>)          nested anchored sub-regexp. stops backtracking.
# (?imx-imx)    turns on/off imx options for rest of regexp.
# (?imx-imx:re) turns on/off imx options, localized in group.

 

 

The site this was on also has a way to test regular expressions you are creating.

A series of Video Tutorials on Regular Expression.

From this set of videos I could clarify a bit more of the quick reference table. I got the \d any digit \w any word character however I found the following information chunks very useful:

.           matches any character EXCEPT a line break

\b        a space which proceeds or follows any word

?          zero or 1 repetition of whatever code proceeds it

*          zero or more reps of any code which proceeds it

\d{5}  5 digits in a row

\d{1..5} 1-5 digits in a row.

Some characters require escaping which means followed by a /.

Examples:

1.

'Jenifer\s\w+\s'

Can be explained as;

'Jenifer

the combination of letters which matches exactly ‘Jenifer’

\s

followed by a space

\w+

followed by one or more characters (could be surname ..)

\s'

followed by another space.

2.

\$\d*\.\d{2}

\$\

an actual $ sign as it has a \ before it which means we want it instead of it’s denotation of the end of a line.

d*

any number of digits

\.\

a full stop

d{2}

two digits

so $245.98 would be found with this code.

.. I can search through any sites quickly for a price??

WOW this is beginning to look like it is a very powerful tool!!

regular expression as a powerful tool

It looks like it could be a key part of building a web-scaping tool.. which as a previous growth marketer is something which is extremely exciting to me .. Even if my reader (mum) doesn’t quite get why that may be so useful!!

 

Here is the initial codewars task I was given, the one that sparked off this whole blog post:

Return the number (count) of vowels in the given string.

We will consider a, e, i, o, and u as vowels for this Kata.

And here is the solution:

def getCount(inputStr)
  inputStr.count("/[aeiou]/")
end

The .count method is used to count the number of occurrences in a string. The optional parenthesis, (), after it are to give the count method an argument. This argument allows you to count whatever is instead the () within the string.

The regular expression:

"/[aeiou]/"

means any characters which are equal to a, e, i, o, u. It is within quotes “” because when I didn’t do this I got an error message from irb which stated  there was no conversion of Regex to string. This must be done so it can count those characters within a string not within a regular expression.

The // are used to capture any regular expression.

The [] denote a “character group”.

The () is to group the string together.

Another Example

While perusing stackoverflow, I found A useful explanation of a more complicated regular expression:

/([^.]*)\.(.*)/

I have re-worded the explanation a bit into my simpler beginners language:

// mark the start and end of regular expressions.

([^.]*)

() are to group a string together and to let ruby know the expression within is related.

[] denote the character group.

^ reverses the groups meaning.

. is a fullstop (or period if American). So the ^ means to match any character which is not a fullstop.

* is a wildcard, which means anything within the square brackets before it can be matched zero or more times.

\. is an escape period. Fullstops in regex have a special meaning they match everything. The \ before it cancels this meaning out so that it literally means match fullstops.

(.*) is a new sub-group. The . takes on it’s special meaning so that it matches any character and the * wildcard means it can be repeated as many times as it needs to be.

So what does this mean the whole expression is doing?

It finds any sequence of characters (that isn’t a fullstop), followed by a single fullstop, followed then by any character.

Another way to look at it is this:

Split this as:
[1] ([^.]*) : It says match all characters except . [ period ]
[2] \. : match a period
[3] (.*) : matches any character

 

For a full list of all the types of regular expression head over to Microsofts site HERE.

So there you have it, my short but sweet experience with learning the basics of regular expression, then using this to complete a codewar’s kata challenge. And by ‘you’ I realise I am writing this for myself, as a way to solidify my learning. Sadly I known even my mum doesn’t read these .. because I can see no traffic coming from France ..

regular expression resource

This isn’t actually my mum I just wanted to show off a picture I took while living in Indonesia

 

The Ruby Kata; Minimum Bonus, A Codewars Challenge

So it is week 4 of the pre-course for Makers Academy. I know what you are thinking .. where have you been on week3? I don’t see any posts? … Well the answer to your question is Barcelona, which meant having to do the whole of week 3 in two days .. which NO i do not recommend doing…

I thought I would delight my reader by creating a post on a codewars kuta I am bumbling through, since this week is all about achieving a level 5 kyu.

You can find my GitHub Repository to this challenge HERE so you can follow along with the commits and steps there if you prefer…  I would love it if you gave my little independent blog the chance to shine and stay on here though (..please mum …I know you don’t really read this, dad).

 

ruby kuta on codewars, minimum bonus

Obviously I was going to use a post on ‘kata’s to show off my pictures of Japan.

The Challenge:

Task which was given by codewars:

International hackers group organized a programming competition, in which n teams participated.

They were assigned to separate rooms for competitions, and the rooms were lined up in a straight line.

The game was over and each team scored points. It's time to pay bonuses. The rule is:

- The bonus unit is 1K($1000), and each team gets at least 1k.
- The bonus payment process is not public.
- A team can know the bonus amount of its adjacent team, if the
  score of the adjacent team is lower than itself.
- If a team finds that its bonus is no higher than the adjacent team whose
  score is lower than itself, the team will not be satisfied
Given an integer array scores represents the score of all teams. Your task is to calculate how much bonuses international hackers group need to pay to keep all teams satisfied.

Note, the unit of bonus is 1K. All teams are in a straight line, and their order is the same as that of the array elements.

Example

For scores = [10,20,30], the output should be 6.

team1's score = 10
team2's score = 20
team3's score = 30

team1 can get 1K, The team was satisfied
because it knew nothing about the other teams.

team2 can know team1's bonus amount,
So team2 at least get 2K to be satisfied

team3 can know team2's bonus amount,
So team3 at least get 3K to be satisfied

1 + 2 + 3 = 6
For scores = [10,20,20,30], the output should be 6.

The possible bonus amount of each team can be:[1,2,1,2]

For scores = [20,30,10,30,40,10,20,30,40,30], the output should be 20.

The possible bonus amount of each team can be:[1,2,1,2,3,1,2,3,4,1]

 

The Solution:

Step 1:

Understanding what is being asked and breaking it down into an english Explanation of what I want the code to do:

breaking the ruby kata down into english

def minimum_bonus(scores)
Making sense of the problem - writing it out in English 
  #scores are an array of numbers
  #the length of this array should be known/ calculated??)
  #1 (value) is asigned to the first number
  #if the second number is larger than the first it is given 2 (new_value =value +1)
  #if the second number is the same size or smaller than the first it is given 1
  #if the 3rd number is larger than the second it returns 3 (new_value +1)
  #if the 3rd number is smaller than the second it returns 1 (value)
  #all the values returned are added together and calculated.
end

The first thing I did was google; ‘how to select each element in an array with it’s index number’.

I found one extremely useful bit of information on how to best iterate through an array in ruby on stackoverflow:

  • each is sufficient for many usages, since I don’t often care about the indexes.
  • each_ with _index acts like Hash#each – you get the value and the index.
  • each_index – just the indexes. I don’t use this one often. Equivalent to “length.times”.
  • map is another way to iterate, useful when you want to transform one array into another.
  • select is the iterator to use when you want to choose a subset.
  • inject is useful for generating sums or products, or collecting a single result.

So I did this in irb:

2.4.0 :031 > minimum_bonus=[20,30,10,30,40,10,20,30,40,30]
 => [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]
2.4.0 :032 > minimum_bonus.each_with_index
 => #<Enumerator: [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]:each_with_index>
2.4.0 :033 > minimum_bonus.each_with_index do |x, index|
2.4.0 :034 >     if x[index

…..and before completing it I though .. oh I don’t think I need the index ..What I need is to change the input scores into the bonus number .. so I need map?

  • map is another way to iterate, useful when you want to transform one array into another.

The next thing I tested in irb was this:

def minimum_bonus(scores)
  scores=[20,30,10,30,40,10,20,30,40,30]
    scores.map do |x|
        x(1) = 1
        end
end

Then I googled:

“how to access an element in an array by its index number ruby”

And I came across each_with_index again, so decided to give it another chance.

def minimum_bonus(scores)
  scores=[20,30,10,30,40,10,20,30,40,30]
    scores.map.each_with_index do |item, index|
        if index == 0 then 
          item = 1
        end
    end

 

I still needed the map because I needed to alter the values in the original array.

When I tried this in irb it seemed to work:

2.4.0 :054 > scores=[20,30,10,30,40,10,20,30,40,30]
 => [20, 30, 10, 30, 40, 10, 20, 30, 40, 30]
2.4.0 :055 >     scores.map.each_with_index do |item, index|
2.4.0 :056 >             if index == 0 then
2.4.0 :057 >                 item = 1
2.4.0 :058?>             end
2.4.0 :059?>       end
 => [1, nil, nil, nil, nil, nil, nil, nil, nil, nil]

When I extended the logic from this method it did not seem to work:

2.4.0 :088 > scores.map.each_with_index do |item, index|
2.4.0 :089 >             if index == 0 then
2.4.0 :090 >                 item = 1
2.4.0 :091?>             end
2.4.0 :092?>           if index[1] > index [0] then
2.4.0 :093 >                 item = 2
2.4.0 :094?>             end
2.4.0 :095?>         end
 => [nil, nil, 2, nil, nil, nil, 2, nil, nil, nil]
2.4.0 :096 >

So it looks like separating out the if statements over-rides the first one and the item in the array is only set for the second statement when it is returned.

It also looks like there is an issue with the numbering I am assigning to the index.

2.4.0 :096 >   scores.map.each_with_index do |item, index|
2.4.0 :097 >             if index == 0 then
2.4.0 :098 >                 item = 1
2.4.0 :099?>             elsif index[1] > index [0] then
2.4.0 :100 >                 item = 2
2.4.0 :101?>             end
2.4.0 :102?>     end
 => [1, nil, 2, nil, nil, nil, 2, nil, nil, nil]

I noticed I was still messing up on the index number while comparing values in the array. It then dawned on me that this is a ‘comparator’ question. We are to compare values in an array with each other and then assign them a new value based on that. So map may still be correct but I needed to set about googling:

“Comparing numerical values in an array with each other, Ruby”

I started to realised I had been spending too long on this kata and it is currently above my ability. I thought the best thing for me to do was to look at the answer and try to understand it and walk through it.

The Answer:

def minimum_bonus(scores)
  not_satisfy = true
  bonus = [1]
  (1...scores.size).each { |i| bonus << (scores[i] > scores[i-1] ? bonus[i-1]+1 : 1) }
  while not_satisfy
    (1...bonus.size).each do |i|  
      bonus[i-1]+=1 if bonus[i] == bonus[i-1] && scores[i] < scores[i-1]
    end
    
    (1...bonus.size).each do |i|
      if bonus[i] == bonus[i-1] && scores[i] != scores[i-1]
        not_satisfy = true
        break
      else
        not_satisfy = FALSE
      end
    end  
  end
  bonus.reduce(:+)
end

 

The Walk Through:

 

So there you have it, my complete thought process and logical reasoning while working through a codewar’s kuta on comparing elements within an array. I am very new to Ruby so I am assuming I will look back on this in the future and cringe at how long winded my approach was and how I went so wrong.

 

Logical Reasoning and Problem Solving With Ruby-Kickstarter

For those of you who aren’t regular readers of my blog, aka everyone but my mum and dad, allow me to set the scene:

I have enrolled on the intensive computer programming bootcamp at the Makers Academy. Before starting the full-time, on-campus, course you complete 4 weeks of pre-course. In this 4 weeks you get a real feel of how coders go about problem solving while learning some of the basics of Ruby, git & command line.

On week two we are given the choice of working through one of two Ruby tutorials:

Chris Pines Book, Learn to Programme. Chapter 8–End (since we did up to 8 for the interview).

Josh Cheeks Ruby Kickstarter, Session 1–3

Reading the reviews I saw past students had said the Kickstarter was more challenging and had video tutorials, so I decided I would go with that one.

I am not going to lie, I found it very challenging. Having got stuck on many of the challenges throughout sessions 1, 2 and 3 I thought I would write a post on one I am still struggling on, to show how I worked through this problem and hopefully expose where I am still going wrong.

Cue Session 3:2.

The Challenge:

Given a nonnegative integer, return a hash whose keys are all the odd nonnegative integers up to it and each key’s value is an array containing all the even non negative integers up to it.

Examples:

staircase 1 # => {1 => []}
staircase 2 # => {1 => []}
staircase 3 # => {1 => [], 3 => [2]}
staircase 4 # => {1 => [], 3 => [2]}
staircase 5 # => {1 => [], 3 => [2], 5 =>[2, 4]}

My Solution

How I broke the problem down and tried to solve the challenge. The steps I took:

Breaking the challenge down into smaller more manageable pieces:

Step 1

Check out possible examples and theory which may help, using ruby documents on hashes, google and Josh Cheeks cheatsheets:

The information I decided may be useful:

Hash.new { |this_hash, key| this_hash[key] = Array.new }
hash = Hash[ *[ Array(‘a’..’j’) , Array(1..10) ].transpose.flatten ] # populating
hash # => {“a”=>1, “b”=>2, “c”=>3, “d”=>4, “e”=>5, “f”=>6, “g”=>7, “h”=>8, “i”=>9, “j”=>10}
hash.values_at ‘j’ , ‘e’ , ‘i’ , 😡 # => [10, 5, 9, nil]

Step 2

Breaking the problem down into sudo code:

(nr_given)
integer_hash = [*[Array(starting from 0 increment numbers which   are odd up to the nr_given) , Array(starting from 0 increment numbers which are odd up to the nr_given).transpose.flattern]]
key array upto(nr_given)
value array up_to(nr_given) next if (i %2) != 0
#need to work out how to increment values and if up_to is method

Step 3

Deciding which bit to tackle first:

Incrementing the arrays seems like a good place to start as I can isolate my code for that part and test it with irb.

This was my first attempt:

Array(0..nr_given).map! {|x| puts x next if(x.even?)}

Next attempt:

Array(0..nr_given).map! {|x| if x.even? puts “x”}

Then:

Array(0..nr_given).map {|x| if x.even? puts “x”}

When I ran this in the irb it returned nothing even when I input a number, 77, in place of nr_given.

So I tried:

Array(0..nr_given).select do |x|
  if x.even?
end

When I run these in irb I still get no output even when I put a number in ‘nr_given’. I thought it may be something wrong with the way I created a new array so I tried to fix that:

a = *(1..77)

and that worked.

Then I tried setting a to that array. Checking it was definitely set by typing in a again, then playing with the .select method.

a.select { |x| return x if (x%2)==0 }

I got the following error:

unexpected return
from (irb):8:in `block in irb_binding’
from (irb):8:in `select’
from (irb):8

I looked up the select method in Ruby docs and it states the following:

[1,2,3,4,5].select { |num| num.even? } #=> [2, 4]

I therefore changed my code to:

#keys are:
a = *(0..nr_given)
a.select {|x| x.odd?}
And tested it doing:
a = *(0..77)
a.select {|x| x.odd?}

And it worked!! I changed it to a shorter version and checked that also worked:

a = *(0..77).select {|x| x.odd?}

Step 4

Joining these to the rest of the code to solve the whole challenge.

My first attempt to put these arrays into the rest of my code:

def staircase(nr_given)
integer_hash = [*[*(0..nr_given).select {|x| x.odd?} , *(0..nr_given).select {|x| x.even?}.transpose.flattern]]
end

This failed the rake test. I got the following error message from it:

Failure/Error: expect(method :staircase).to be
NameError:
undefined method `staircase’ for class

So I referred back to my notes on how to set a hash out :

character_counts = Hash.new { |this_hash, key| this_hash[key] = 0 }
character_counts[“a”] # => 0
character_counts # => {“a”=>0}

Looking at this I changed my code to:

def staircase(nr_given)
   return_hash = Hash.new
   *(0..nr_given).select {|x| x.odd?}
   return_hash[x] = *(0..nr_given).select {|x| x.even?}.transpose.flattern
   end
 end
return_hash
end

It failed the rake test so I decided the get rid of the part I didn’t feel was necessary ‘.transpose.flattern’.

def staircase(nr_given)
   return_hash = Hash.new
   *(0..nr_given).select {|x| x.odd?}
   return_hash[x] = *(0..nr_given).select {|x| x.even?}
   end
 end
return_hash
end

This was still failing on the line starting with ‘*’. So I changed this round a bit:

def staircase(nr_given)
   return_hash = Hash.new
   (0..nr_given).to_a.select {|x| x.odd?}
   return_hash[x] = (0..nr_given).to_a.select {|x| x.even?}
  end
 end
return_hash
end

I realised I was missing an if statement in the .select method:

def staircase(nr_given)
   return_hash = Hash.new
   (0..nr_given).to_a.select {|x| if x.odd?}
   return_hash[x] = (0..nr_given).to_a.select {|x| x.even?}
  end
 end
return_hash
end

I was getting an error because I had closed the statement off too early . The ‘}’ after the odd meant that it was not finishing off the if statement. It was not putting this newly created array into the return_hash[x] — the keys of the hash. This statement could not accept the x as I had closed off the select method too early. I was saying if x.odd? then not telling it what to do with x.

def staircase(nr_given)
   return_hash = Hash.new
   (0..nr_given).to_a.select do |x|
   if x.odd?
   return_hash[x] = (0..nr_given).to_a.select {|x| x.even?}
   end
  end
return_hash
end

I was now getting an error about the .select method. I noticed a few errors here, first I had forgotten to ask if the x was even then I had forgotten to tell it to return the value if it was.

def staircase(nr_given)
   return_hash = Hash.new
  (0..nr_given).to_a.select do |x|
  if x.odd?
  return_hash[x] = (0..nr_given).to_a.select {|x| return x if   x.even?}
  end
 end
return_hash
end

This is still failing. If anyone reading this can see where I am going wrong I would love to hear from you.

For now, I am parking this to move onto the next challenge in the hope a fresh head will shed some light on the problem and I will magically stumble upon the solution.

Talking Through a Logical Ruby Challenge

As part of the Makers Academy  pre-course week 2 we have been tasked with working through the Ruby-Kickstarter github tutorials by Josh Cheek.

I decided to go through my thought process on one of the challenges I got stuck on, 1:6, in the hope that this may help me discover the solution. This post is about how I went about trying to understand the challenge and what I am still confused about. I have ordered this post as the way I looked at the information, worked through it in a lineal time format.

So hold tight as we ride through the waves of my mind … and apologizes if i stray off on a visual tangent ..

 

Question Asked:

 

You'll get a string and a boolean.

When the boolean is true, return a new string containing all the odd characters.

When the boolean is false, return a new string containing all the even characters.

If you have no idea where to begin, remember to check out the cheatsheets for string and logic/control

# odds_and_evens("abcdefg",true)    # => "bdf"

# odds_and_evens("abcdefg",false)   # => "aceg"

 

So two things that are seemingly unrelated are now bound together by a method I am to create …

My solution:

def odds_and_evens(string, return_odds)

  if return_odds

   string.size.times do |n|

   next if n % 2 == 0

   print 'n'

  end

   else

    string.size.times do |n|

    next if n % 2 != 0

   print 'n'

  end

end




odds_and_evens("abcdefg",true)

odds_and_evens("abcdefg",false)

Initial Issues:

  1. I don’t understand why this does not ‘print’ the answer to the screen when I call the method odds_and_evens(“abcdefg”,true).
  2. When I run the rake test on the file I get the following error messages.:

 

syntax error, unexpected end-of-input, expecting keyword_end

rake aborted!

Command failed with status (1):

 

The Given Solution:

 

def odds_and_evens(string, return_odds)

to_return = ""

string.size.times do |index|

next if return_odds && index.even?

next if !return_odds && index.odd?

to_return << string[index]

end

to_return

end

 

My thought process, the steps it went through:

We want to design a method which takes two arguments, one is a string of characters the other is a true or false statement.

The first step is therefore to create this method name it ‘odds_and_evens’ and pass it two arguments ‘string’ and ‘return_odds’.

def odds_and_evens(string, return_odds)

We want this method to return the string, given to it, in an altered way.

We need some mechanism to return the string at the end.

We will have to split this string into an array of it’s characters in order to count which index number each character sits at. So we need something which converts it back to a string.

By putting a variable set as ‘to_retrun’ then assigning it a value of an empty string we can then push the characters of the original string which has passed through our if argument, back into it.

to_return = ""
to_return << string[index]

We want it to split up this string into its characters. So the string is now an array of it’s characters.

 

The index number of the character in a string is what will determine if it is odd or even. Index numbering starts at 0. All the characters with an odd index number are add, all the characters with an even index number are even.

The method .size returns the character length of a string.

string.size

.times do is a loop. What this does is say ‘for’ a certain amount of times (we have assigned this number with the .size method) ‘do’ the following block of code.

string.size.times do |index|

What I am still confused about here:

Does .times do or .size split a string into an array? I Have tried to look for this answer but cannot find it.

Potential answer:

We not need to split the string into its characters, do we simply need to assign a number to each character in the string depending on it’s place and act on those numbers … drawing the string back according to those numbers.

We need to do something different to the string depending on weather the returns_odd variable, is true or false.

We need an if statement which looks at the returns_odd varaibale and the split string. We need a Boolean here.

&& index.even?
&& index.odd?

If it is true then we need to return only the characters at the odd index number.

If returns_odd is not an object, i.e. it is false, we need to return only the characters at the even index numbering.

The string is passed through the loop block of code.

string.size.times do |index|

next if return_odds && index.even?

next if !return_odds && index.odd?

The name  ‘index’ is given to the block of code as a placeholder for every character in the string. So on the first loop around the code it represents the character at index 0, then on the next, the character at index 1 and so on.

The block of code then assesses if the character which is now the ‘index’ has an odd or even index number.

 

I need to go through the loop again:

When the string is split up into its characters, an index number is assigned to each character according to it’s original string, starting with 0:

string.size.times do |index|

next if return_odds && index.even?

next if !return_odds && index.odd?

 

So for size(the arrays’ length or size) number of times each character is assigned to the ‘index’ placeholder.

The character at the ‘index’ placeholder is then put through the argument;

Is the returns_odd object true? If so pass over all those characters whose index number is even.

If retruns_odd is not an object, give me back all the characters which appear at an even index number. Do not give me back any characters which have been assigned an odd index number.

At the moment these characters are still an index number.

We need to return the string which has been through the the argument.

We push this new ‘string’  into the empty string variable we set at the beginning.

to_return << string[index]

The string is now an array which has the elements assigned to ‘index’. As it is displayed as string[index].

 

Walking through this solution in depth, explaining the coding solution to myself in english has allowed me to really understand what is going on with it. As a beginner looking at a seemingly difficult logical problem can seem very daunting if you don’t understand what it is saying. I highly recommend talking, (or writing), yourself through some code. It helped to understand the logic and hopefully apply it in future. Ruby seems like a game of cards, or musical instrument, the more you play the better you get but this only comes with practice …

(Apologize to those who are simply trying to learn ruby and are not interested in the shameless plug of my photographs).

 

Git IS a Version Control System | But What Does This Mean?

how does git help me

 

It is week one of the pre-course at Makers Academy and I am working through our Git and Github tutorials.

One of the learning objectives is for us to be able to explain how git is a version control system. I thought I would delight the whole two readers I get to my blog (thanks mum and dad), by answering that question here:

What is version control?

Version control systems are ones which record changes made to files or a group of files in a directory.

YOU choose when to make these bookmarked changes. Once you have done this you can always go back to that version of the file / files.

This is useful on an individual or group basis.

As an individual it allows you to track your changes. You can compare a file against it’s earlier version so see where you may have gone wrong or how your mind was working when you made improvements.

It is very useful if you are worried a change you want to make may break something and cause it not to run the way you wanted. – Which is something which happens a lot when coding!

It allows effective collaboration!

It means that you can work with someone else at the same time on the same project but (here is the ground breaking part) on your own computer or work station!!

 

What is so great about being able to work from two computers on the same project… at the same time …without having to worry about replicating the work-load or clashes?

I hear you ask … :-/ (and by you I mean mum .. as I know dad has drifted off by now)

git merging files- version contro

 

This is incredible for two reasons!

Lean Development – like an efficient ford assembly line people can work on the exact same project at the same time without having to wait for someone else to complete their part of the project.

It is like building a house by building the roof and the floor at the same time, even though there are no walls!

git hub version control building a house analogy

This also means that a mistake in one doesn’t delay or immediately effective another part. The mistake can be more efficiently identified and solved!

See told you it was mind blowing …

Better still you can both work on the roof together .. in fact everyone can work on the roof at the same time! Yay…Here is another picture of a house to celebrate …

git house analogy version control

 

To do it like this you start with a master copy which you pull to your computer. As you edit and add on parts you update it in a centralised system. Before adding this to the ‘master branch’ everyone on the project can check they are happy.  If someone else has edited the same bit but differently you can quickly identify this and merge your changes quicker.

Think of this as two people editing an article, both correcting gramma and spelling but both doing it on the original document. When they bring these together you would have to check both documents against the original individually and it could lead to a waste of time as both editors are making the same spelling corrections.

An additional note to make here is git is NOT a centralised version control system. Versions are not held on one central server or system .. all versions are equal. The client computer (thats mine and yours) doesn’t just look at snapshots of files; they fully mirror the repositories!! This is a flat non-hierarchical system which allows you to collaborate as an equal !!

equality in git version control

 

So there you have it, my explanation of how git is a (de-centralised) version control system. Thank you for reading if you are still here. I feel like here is a good time to add the disclaimer that my parents don’t even read this blog…. 🙁

5 Things That Blew My Mind When Learning The Basics of Command Line

So here goes, my first week doing the pre-course at the Makers Academy coding bootcamp. It all started with a set of tutorials on the basics of command line.

After teaching myself online marketing & Growth Hacking it doesn’t half feel good having a bit of guidance and structure to my learning this time.

I get very excited about all things so naturally learning command line was one of them… So here is a post on the top 5 things I learnt doing this tutorial including when I went a bit off piste …. WARNING: Only very beginners will also find these things interesting …

learning command line basics

1. The Spaces Are Important.

importances of observing spaces in command line controls

Put them in the wrong place or leave them out and you fill face the wrath of command line…(that is if you also feel like an error message can be considered as wrath).

I first came across this simple finding when executing the tail command:

tail -3 longtext.txt

I had foolishly written as:

tail-3 longtext.txt

In this example I am trying to pass the parameter ‘longtext.txt’ file to the command tail – 3 which prints that last 3 lines of a file. However, without the space in-between ‘tail’ and ‘-3’ all I was getting was an error message.

I am glad I learnt about observing the spaces in commands as this came in useful later on when I was running more ‘complicated’ commands.

2.  ‘Read’, ‘Write’ & ‘Execute’ Permission.. and some …

Running the command:

ls -l

allows you to see which user classes have which type of access to a given file within a directory.

For those of you who are not aware the user classes are;

user Your account
group Any permissions group that your account belongs to
other Any account that is not yours and that does not belong to a permissions group that your account belongs to

When doing this I found more information against some of the files than the tutorial suggested.

The tutorial states that you can see the different permissions of a files in a directory by listing them in ‘long’ format with:

ls -l

This is what I saw when I did that:

command line showing permission of different users to files with ls -l command

Following the tutorial I learnt how to read the meaning of:

-rw-r–r–

The first – represents nil or not. Here is where a d appears if the file is a directory as opposed to a file. Because it is not a directory the – appears instead of the d.

rw- represents ‘read’, ‘write’ but the ‘-‘ where the ‘x’ should be means no execute permission. This first sequence of three is the permission for the ‘user’ class (this is you on your computer). So the ‘user’ class has read and write but not execute permission on this file.

r– represents ‘read’, no write no execute permission. This second sequence of 3 is the permission for the ‘group’ class. The ‘group’ class in this subDirectory example is “staff”. A group class simply means a group of users. The best example of this is when there are several users accessing a computer or system remotely.

The last r– represents ‘read’, no write no execute permission for the last class, the “other” class of users. Other users are simply users that don’t fall into the other two classes.

So that is what I learnt from the tutorial … but what is that last @ at the end of some of this data?e.g.

-rw-r–r–@

So I had a google…

Turns out the @ means a file had extended attributes and that you can use the

xattr

command to view and modify them like so:

the @ symbol in the permission data of a file shown in command line by ls -l screenshot

Wait… this raised some more questions …. what is an extended attribute?

Extended File Attribute 

Regular attributes such as ‘permission’ have a purpose which is strictly defined by the filesystem they are part of (aka the directory etc).

Extended file attributes enable uses to associate files with metadata not interpreted by the file system.

The typical use of these are;

  • -storing the author of a document,
  • -the character encoding of a plain-text document,
  • -a checksum (a code which detects errors, for example in a digital network to detect accidental changes to raw data.),
  • -a digital certificate (a digital certificate used to prove the ownership of a public key),
  • -or ‘discretionary’ access control information (another way of restricting access permissions to groups or users but done by a ‘trusted computer system evaluation criteria‘).

Ok well great but still what is com.apple.metadata:_KMDItemUserTags doing on my file??

com.apple.metadata:_KMDItemUserTags

My first bit of googling led me to find out that this MAY (yes i can’t even be sure of this right now) be associated with something called:

Mavericks

and that it is something to do with this Mavericks file tagging system.

Further googling has lead me to see that this is something to do with ISO’s, so my macs, file labeling/ tagging system. Ok this doesn’t 100% make sense still but I trust you apple, I feel this tagging system is helping me out in some way …

3. YOU can become a SUPERUSER / the “root”.

becoming the superuser/ root with command line

If you become a superuser you automatically get super powers

Some actions on a computer require administrative POWER to do. This power is only bestowed upon a SUPERUSER.

Turning yourself into a SUPERUSER means (as long as you know the superuser password) you can delete a file you haven’t got permission to delete…

I am pretty sure this isn’t the coolest thing it can do, especially with super in it’s name, but still, I am impressed.

You become a SUPERUSER (it sounds so powerful that it just needs capitals) by prefixing (starting) a command with the word sudo.

eg.

sudo rm inaccessiblefilesname

Allows you to delete that pesky inaccessible file.

4. You can see all the processes your computer is running.

The ‘ps’ command allows you to see what processes you have launched within a directory.  You can use the ‘x’ flag to see ALL processes running on your computer:

ps x

You can filter for the processes you want to see by redirecting the output to grep, then use the wc to count the number of them instead of listing:

ps x | grep bash | wc -l

Will count all the bash processes you have running.

 

5. You can hide secrete data in your code!

Hidding secrete data in environmental variables in command line

You do this by modifying environmental variables!

Modifying environmental variables in command line allows you to hide sensitive data like passwords or secret keys which allow you access to information you want to keep private.

Say you are making a Ruby programme which calls on the users facebook photos. You want to make this programme open source and you need to use your facebook photos as a sample. You can set an environmental variable with a secrete key, e.g.

export SECRET_KEY=1453637600kk

in your command line. Then in your Ruby code read this variable with:

secret_key = ENV['SECRET_KEY']

At the moment this environmental variable will be deleted when we exit the current terminal session. This would be an issue if we wanted to call on these variables in our Ruby code on a more permanent basis.

To solve this we can create permanent env vars (environmental variables) by putting them in our bash profile:

echo "export SECRET_KEY=1453637600kk" >> ~/.bash_profile

The double >> means append, whereas single means overwrite, so be careful to use two here!

This nifty command line control also allows you to modify paths which I am sure will also come in handy for security reasons in the future. But for now, I am the most impressed by it’s ability to hide data from those you don’t want accessing it.

 

So there you have it, my MINDBLOWING 5 things I got most excited by when learning the basics of command line.  Yes I know what you are thinking .. ‘this girl is very easily amused’…