Created
April 30, 2015 18:46
-
-
Save mayfer/854ec479b5ba2dcd4f83 to your computer and use it in GitHub Desktop.
Arrays, Hashes, Blocks, Require
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
var = "asdhasd" | |
numbers = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
] | |
# WRONG | |
numbers[1[1]] | |
# CORRECT | |
numbers[1][1] | |
(3 + 5) * (4 - 1) | |
numbers.each do |inner_numbers| | |
# print each name on a separate line | |
inner_numbers.each do |num| | |
puts num | |
end | |
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
def each(numbers) | |
index = 0 | |
while index < numbers.length | |
# DO SOMETHING | |
value = yield(numbers[index]) | |
index += 1 | |
end | |
end | |
def outside_method | |
numbers = [1, 2, 3] | |
each(numbers) do |number| | |
number * 10 | |
end | |
end | |
puts "heloooo someone ran block.rb" | |
# puts outside_method | |
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
phonebooks = [ | |
{ | |
"color": "yellow", | |
"weight": "3kg", | |
"pages": 4000, | |
}, | |
{ | |
"color": "red", | |
"weight": "1kg", | |
"pages": 2000, | |
}, | |
{ | |
"color": "transparent", | |
"weight": "100gr", | |
"pages": 4, | |
}, | |
] | |
phonebooks.each do |phonebook| | |
phonebook.each do |key, value| | |
puts "#{key}: #{value}" | |
end | |
puts | |
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
def get_all_english_words | |
file = File.open('/usr/share/dict/words').read | |
words = [] | |
rhymes = Hash.new { [] } | |
file.each_line do |line| | |
word = line.strip.downcase | |
rhyme = word[-3..-1] | |
rhymes[rhyme] <<= word | |
end | |
max = 0 | |
max_rhyme = '' | |
rhymes.each do |rhyme, words| | |
if count > max | |
max = words.length | |
max_rhyme = rhyme | |
end | |
end | |
puts max_rhyme | |
puts max | |
puts rhymes[max_rhyme] | |
end | |
get_all_english_words | |
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
# all the same... | |
# require './block.rb' | |
# require './block' | |
require_relative 'block' | |
each(["word", "thing", "boring"]) do |item| | |
puts "You passed in #{item}" | |
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
def get_all_english_words | |
file = File.open('/usr/share/dict/words').read | |
words = [] | |
file.each_line do |line| | |
word = line.strip.downcase | |
if word.match(/^([^aeiou]*y[^aeiou]*)$/) | |
# if !word.match(/[aeiou]/) && word.include?('y') | |
puts word | |
end | |
end | |
words | |
end | |
get_all_english_words | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment