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 count(array) | |
| count = Hash.new(0) | |
| array.each do |elem| | |
| count[elem] += 1 | |
| end | |
| p count | |
| 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
| class Car | |
| attr_reader :speed | |
| def initialize(speed) | |
| raise ArgumentError, 'speed < 0' if speed < 0 | |
| @speed = speed | |
| end | |
| def time(distance) | |
| raise ArgumentError, 'distance < 0' if distance < 0 | |
| return nil if @speed.zero? |
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 create_hash(keys, values) | |
| hash = Hash[keys.zip values] | |
| 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 filter(array, item) | |
| array - Array(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 mod_sum(array, n) | |
| return array.length > 0 ? array.map{ |number| number % n }.inject{ |sum, number| sum + number } : 0 | |
| 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 middle(array) | |
| array.length % 2 == 0 ? array[(array.length / 2) - 1] : array[array.length / 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
| def odd_sum(n) | |
| Array(1..n).select{ |num| num % 2 == 1 }.inject(0,:+) | |
| 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
| a=gets.to_i; | |
| b=gets.to_i; | |
| puts [a+b,a-b,a*b,a/b].join(" ") |
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
| puts :Hello.to_s + :World.to_s |
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
| puts [*1...10] |