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 list_to_frame list=[] | |
| list = ["Hello", "World", "in", "a", "frame"] if list.empty? | |
| longest = 0 | |
| list.each do |element| | |
| longest = element.length if longest < element.length | |
| end | |
| (0..longest+3).each do |
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 english_2_pig_latin string | |
| string.split(/ /).each do |word| | |
| word << word[0] << "ay " | |
| print word[1..word.length] | |
| 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 greet message, *people | |
| people.each { |p| puts "#{message}, #{p}!" } | |
| end | |
| greet "Hello", "Tony", "Ann", "BadMan" |
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 by_three? number | |
| number % 3 == 0 ? true : false | |
| end | |
| by_three? 5 | |
| # => false | |
| by_three? 9 | |
| # => true |
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
| a1 = "A Test" | |
| a2 = "A Boom" | |
| a1 <=> a2 | |
| # => 1 | |
| b1 = "2" | |
| b2 = "9" | |
| b1 <=> b2 |
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
| array = [3,7,1,9,2,12,34,0] | |
| # sort in ascending order | |
| array.sort | |
| # => [0, 1, 2, 3, 7, 9, 12, 34] | |
| # sort in descending order | |
| array.sort.reverse | |
| # => [34, 12, 9, 7, 3, 2, 1, 0] |
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 alphabetize arr, rev=false | |
| if rev | |
| arr.sort.reverse | |
| else | |
| arr.sort | |
| end | |
| end | |
| puts alphabetize ["b","e","a"], true | |
| # => ["e","b","a"] |
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 'benchmark' | |
| string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)] | |
| symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)] | |
| string_time = Benchmark.realtime do | |
| 100_000.times { string_AZ["r"] } | |
| end | |
| symbol_time = Benchmark.realtime do |
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
| case var | |
| when 1 | |
| puts 1 | |
| when 2 | |
| puts 2 | |
| else | |
| puts 'none' | |
| end | |
| case var |
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
| floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911] | |
| # Write your code below this line! | |
| round_down = Proc.new { |f| f.floor } | |
| # Write your code above this line! | |
| ints = floats.collect(&round_down) |