Skip to content

Instantly share code, notes, and snippets.

@Haumer
Created July 10, 2020 10:51
Show Gist options
  • Save Haumer/02dff6f272af7ab6a4002587416fccc3 to your computer and use it in GitHub Desktop.
Save Haumer/02dff6f272af7ab6a4002587416fccc3 to your computer and use it in GitHub Desktop.
# string
"Hello world"
# integer
423
# float
3.14
# array
[ "micheal", "francis" ]
# range
(1..10)
# booleans
true
false && true || false
# Hash
my_hash = {}
p my_hash
students = ['Peter', 'Mary', 'George', 'Emma']
student_ages = [24, 25, 22, 20 ]
# indexes 0. 1. 2. 3
students.each_with_index do |student, index|
puts "#{student} is #{student_ages[index]} years old"
end
# CRUD ARRAY
# C array = [ "Peter", "Mary", "George", "Emma" ]
# R array[0]
# U array[0] = new_value
# D array.delete_at(index)
# CRUD HASH
# Create
ages = {
'Peter' => 24,
'Mary' => 25,
'George' => 22
}
# Hash.new
# p ages
# Read
ages["Peter"]
# p "Peter is #{ages["Peter"]}"
# Update a value
ages['Peter'] += 1
# p ages['Peter']
# Add a new Key Value pair
ages['Seb'] = 18
# p ages['Seb']
# Delete
ages.delete('Peter')
ages['Ringo'] = 100
# p ages
paris = {
'country' => 'France',
'population' => 2211000
}
# p paris['country']
# p paris['population']
# paris['monument'] = 'Tour de Eiffel'
# p paris['monument']
# paris['population'] += 9000
# p paris['population']
# paris.each do |key, value|
# puts "#{value}"
# puts "Paris' #{key} is #{value}"
# end
paris = {
'country' => 'France',
'population' => 2211000
}
p paris.key?('country')
#=> true
p paris.key?('monument')
#=> false
p paris.value?(2211100)
#=> true
p paris.value?(123123123123)
#=> false
# to get all the values:
p paris.values
# to get all the keys:
p paris.keys
p paris.size
#=> 2
# basic symbol
p :hello
# Transform to a symbol
p "goodbye".to_sym
:hello == "hello"
#=> false
# 1) With Stings as identifiers
paris = {
"country" => "France",
"population" => 2211000
}
# 2) With Symbols as identifiers (maintaining the arrow)
paris = {
:country => 'France',
:population => 2211000
}
# 3) Modern Syntax with Symbols
paris = {
country: 'France',
population: 2211000
}
p paris[:county] # for 2 and 3
p paris["country"] # for 1
# the last argument in a method can be accepted as a hash!
def tag(name, content, attrs = {})
tag_attrs = attrs.map { |k, v| "#{k}='#{v}'" }.join(' ')
return "<#{name} #{tag_attrs}>#{content}</#{name}>"
end
# we can even drop the curly braces! (notice the key value pairs after 'Hello World')
tag('h1', 'Hello world', class: 'italics', id: 'batch-423')
require 'csv'
# similar to the Date object we need to
# import this to help us out!
CSV.foreach('cities.csv') do |row|
# each row is an array
# p row
# a nice 'puts' to print out our data
puts "#{row[0]} has a population of #{row[1]}, its monument is #{row[2]}"
end
Paris 2211000 'Tour Eiffel'
London 8308000 'Big Ben'
Berlin 4000000 'Alexander Platz'
{
"name": "Paris",
"population": 2211000
}
require 'open-uri' # require to open links (urls)
require 'json' # require this to parse/extract JSON
# ask our user for his githubname
puts "What is your githubname?"
githubname = gets.chomp
# the link to the API
url = "https://api.github.com/users/#{githubname}"
# open the link and read it
user_info = open(url).read
# parse the information
json = JSON.parse(user_info)
# display a nice sentence to the user, with the info
puts "#{json["name"]} works at #{json["company"]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment