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 Item | |
| def initialize(item_name) | |
| @item_name = item_name | |
| end | |
| # def show | |
| # puts "Instance method show for '#{self}'" | |
| # end | |
| # def 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
| class Animal | |
| def move | |
| "I can move" | |
| end | |
| end | |
| class Bird < Animal | |
| def move | |
| super + " by flying" | |
| 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 Rectangle | |
| def initialize(length, breadth) | |
| @length = length | |
| @breadth = breadth | |
| end | |
| def perimeter | |
| 2 * (@length + @breadth) | |
| 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
| require "benchmark" | |
| def calculation_with_explicit_block(a, b, operation) | |
| operation.call(a, b) | |
| end | |
| def calculation_with_implicit_block(a, b) | |
| yield(a, b) | |
| 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 number_shuffle(number) | |
| no_of_combinations = number.to_s.size == 3 ? 6 : 24 | |
| digits = number.to_s.split(//) | |
| combinations = [] | |
| combinations << digits.shuffle.join.to_i while combinations.uniq.size!=no_of_combinations | |
| combinations.uniq.sort | |
| end | |
| # Given: 123 | |
| # Return: [123, 132, 213, 231, 312, 321] |
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 MyArray | |
| attr_reader :array | |
| def initialize(array) | |
| @array = array | |
| end | |
| def sum(initial_value = 0) | |
| return array.inject(0) {|sum, el| sum + el } + initial_value unless block_given? | |
| sum = initial_value |
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 Restaurant | |
| def initialize(menu) | |
| @menu = menu | |
| end | |
| def cost(*orders) | |
| # your code here | |
| orders.inject(0) do |total_cost, order| | |
| total_cost + order.keys.inject(0) {|cost, key| cost + @menu[key]*order[key] } | |
| 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
| module Perimeter | |
| # Your code here | |
| def perimeter | |
| sides.inject(0) {|sum, side| sum + side} | |
| end | |
| end | |
| class Rectangle | |
| # Your code here | |
| include Perimeter |
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 calculate(*numbers) | |
| #p numbers | |
| if numbers[-1].is_a?(Hash) | |
| return subtract(*(numbers[0...-1])) if numbers[-1][:subtract] | |
| return add(*(numbers[0...-1])) if numbers[-1][:add] | |
| else | |
| add(*numbers) | |
| 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
| function replaceWord(word){ | |
| var asterics = ""; | |
| for(var i = 0; i < word.length; i++){ | |
| asterics += "*"; | |
| } | |
| return asterics; | |
| } | |
| function maskOutWords(words, text){ | |
| var maskedWord; |