Created
July 10, 2020 10:52
-
-
Save Haumer/49a3961b7356e7da0dc0b86f528917ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # basic symbol | |
| p :hello | |
| # Transform to a symbol | |
| p "goodbye".to_sym | |
| :hello == "hello" | |
| #=> false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Paris | 2211000 | 'Tour Eiffel' | |
|---|---|---|---|
| London | 8308000 | 'Big Ben' | |
| Berlin | 4000000 | 'Alexander Platz' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "Paris", | |
| "population": 2211000 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Hashes and Symbols |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment