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 initialize(name, food) | |
| @name = name | |
| @legs = 4 | |
| @food = food | |
| end | |
| def breathe | |
| puts "This animal breathes." | |
| 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 'open-uri' | |
| url = "http://ruby.bastardsbook.com/files/fundamentals/hamlet.txt" | |
| local_fname = "hamlet.txt" | |
| File.open(local_fname, "w") {|file| file.write(open(url).read)} | |
| File.open(local_fname, "r") do |file| | |
| file.readlines.each_with_index do |line, idx| | |
| puts line if idx % 42 == 41 | |
| 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 BottleConsumer | |
| attr_accessor :bottles, :caps, :dollars, :empty, :bottles_returned, :caps_returned | |
| def initialize(dollars) | |
| @bottles = 0 | |
| @caps = 0 | |
| @empty = 0 | |
| @dollars = dollars | |
| @caps_returned = 0 | |
| @bottles_returned = 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
| require 'active_support/all' | |
| @candidates = [ | |
| { | |
| id: 5, | |
| years_of_experience: 4, | |
| github_points: 293, | |
| languages: ['C', 'Ruby', 'Python', 'Clojure'], | |
| date_applied: 5.days.ago.to_date, | |
| age: 26 |
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
| # Determine whether a string contains a SIN (Social Insurance Number). | |
| # A SIN is 9 digits and we are assuming that they must have dashes in them | |
| def has_sin?(string) | |
| !!/\b\d{3}-\d{3}-\d{3}\b/.match(string) | |
| end | |
| puts "has_sin? returns true if it has what looks like a SIN" | |
| puts has_sin?("please don't share this: 234-604-142") == true | |
| puts "has_sin? returns false if it doesn't have a SIN" |
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 benchmark | |
| start_time = Time.now | |
| yield | |
| Time.now - start_time | |
| end | |
| long_string = "apple"*100000000 | |
| running_time = benchmark do | |
| long_string.reverse | |
| 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
| @states = { | |
| OR: 'Oregon', | |
| FL: 'Florida', | |
| CA: 'California', | |
| NY: 'New York', | |
| MI: 'Michigan' | |
| } | |
| @states.store("WV","Charleston") | |
| @states.store("MN", "Augusta") |
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
| @states = { | |
| OR: 'Oregon', | |
| FL: 'Florida', | |
| CA: 'California', | |
| NY: 'New York', | |
| MI: 'Michigan' | |
| } | |
| @states.store("WV","Charleston") | |
| @states.store("MN", "Augusta") |
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_letters(strings) | |
| count = Hash.new(0) | |
| strings.split('').each_with_index do |string, index| | |
| count[string] = index | |
| end | |
| count | |
| end | |
| puts count_letters("lighthouse in the house...") |
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_letters(strings) | |
| count = Hash.new(0) | |
| strings.split('').each do |string| | |
| count[string] += 1 | |
| end | |
| count | |
| end | |
| puts count_letters("lighthouse in the house...") |
NewerOlder