Skip to content

Instantly share code, notes, and snippets.

@M0119
Last active March 19, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/06042c10eaad91b9f96ec074f0f68db7 to your computer and use it in GitHub Desktop.
Save M0119/06042c10eaad91b9f96ec074f0f68db7 to your computer and use it in GitHub Desktop.

Lecture ✍️

Constants

Use constants

  • to add clarity
  • to avoid 'magic numbers' in code
  • to make one central place to make a change for a common value
PASSWORD_RETRY_LIMIT = 5
MAX_VOLUME = 11

Case Statements

Use case statement as an alternative to a series of if else to make code more readable. A case statement can be clearer then if when there are a fixed number of choices.

print ("Select an option from 1-3 (0 to quit)")
option = gets().strip
case option 
when "0"
  puts ("You chose to quit")
when "1"
  puts ("You chose otpion 1")
when "2"
  puts ("You chose otpion 2")
when "3"
  puts ("You chose otpion 3")
else
  puts("Error. Invalid option.")
end

Case statements can also work over a range of values.

case capacity
when 0
  "You ran out of gas."
when 1..20
  "The tank is almost empty. Quickly, find a gas station!"
when 21..70
  "You should be ok for now."
when 71..100
  "The tank is almost full."
else
  "Error: capacity has an invalid value (#{capacity})"
end

Logical Operators

Combinging conditions

  • and (&&)
  • or (||)
if (total < 50)
  puts ("low")
elsif ((total > 50) && (total < 75))
  puts("medium")
else
  puts("high")
end
while (retry_count < PASSWORD_RETRY_LIMIT) && (!is_valid_password(password))
  password = get_password()
end

More string methods

Use chomp to remove that extra newline character (\n) is to use the chomp method.

  print ("What is your name? ")
  name = gets()
  puts("Name before chomp #{name} with length #{name.length}")
  chomped_name = name.chomp()
  puts("Name after chomp #{chomped_name} with length #{chomped_name.length}")
  stripped_name = name.strip()
  puts("Name after strip #{stripped_name} with length #{stripped_name.length}")

Testing

Some testing concepts to help organise testing efforts.

Acceptance Test

A test that checks whether a feature meets its business requirements. A certain feature may work exactly as the developer and designer intended, but if the developer and designer designed and built the wrong thing, the acceptance test would fail.

Edge Case

A path through an app that isn’t exercised frequently or that is unlikely to be exercised.

Happy Path

A normal/default/valid path through a feature. (I like to think of the opposite as the “sad path”)

Regression Testing

A type of testing meant to ensure that previously-working functionality has not been broken by newly-developed functionality.

Test Case

Wikipedia‘s definition: “A specification of the inputs, execution conditions, testing procedure, and expected results that define a single test to be executed to achieve a particular software testing objective”.

Test Coverage

The percentage of an application covered by automated tests.

Use fake data

Use the faker gem to generate data to make testing easier.

To install the gem

gem install faker

You can use faker for various types of data. Here's an example.

require 'faker'
Faker::Config.locale = 'en-AU'

10.times do
  now = DateTime.now
  created_at = now - rand(3 * 24 * 60)
  person = {
      created_at: created_at,
      updated_at: now + rand(24 * 60),
      first_name: Faker::Name.first_name,
      last_name: Faker::Name.last_name,
      mobile_number: Faker::PhoneNumber.cell_phone.gsub(/[^0-9,.]/, ''),
      postcode: Faker::Address.postcode,
      customer_email: Faker::Internet.email(:firstname),
      payment_type: ['20.0', '15.0', '5.0'].sample
    }

  puts "------------------------------------------"
  person.each do |key, value|
    puts "#{key}\t#{value}"
  end
end

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment