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
| MAX = 10 | |
| answer = rand(MAX) | |
| while true do | |
| print "Guess a number between 0 and #{MAX}: " | |
| guess = gets.chomp.to_i | |
| if guess > answer | |
| puts "\nToo high, try again..." |
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
| #!/usr/bin/env ruby | |
| # encoding: UTF-8 | |
| SUITS = ['♠', '♣', '♥', '♦'] | |
| VALUES = ( 2..10 ).to_a.map{ |n| n.to_s }.concat( ['J', 'Q', 'K', 'A'] ) | |
| # build_deck | |
| # returns a shuffled array of 52 cards | |
| def build_deck | |
| deck =[] |
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
| (1..100).each do |i| | |
| print 'Fizz' if i % 3 == 0 | |
| print 'Buzz'if i % 5 == 0 | |
| print i if i % 3 != 0 and i % 5 != 0 | |
| 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
| scores = [75, 100, 85, 65, 84, 87, 95] | |
| p scores | |
| puts "Average: #{scores.inject(0){|sum, i| sum + i} / scores.length.to_f}" | |
| puts "Minimum: #{scores.min}" | |
| puts "Maximum: #{scores.max}" |
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
| characters = { | |
| "Tyrion Lannister" => "House Lannister", | |
| "Jon Snow" => "Night's Watch", | |
| "Hodor" => "House Stark", | |
| "Stannis Baratheon" => "House Baratheon", | |
| "Theon Greyjoy" => "House Greyjoy" | |
| } | |
| characters.keys.each do |name| | |
| puts "#{name} represents the #{characters[name]}" |
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
| favorite_movies = [ | |
| { title: 'The Big Lebowski', year_released: 1998, director: 'Joel Coen', imdb_rating: 8.2 }, | |
| { title: 'The Shining', year_released: 1980, director: 'Stanley Kubrick', imdb_rating: 8.5 }, | |
| { title: 'Troll 2', year_released: 1990, directory: 'Claudio Fragasso', imdb_rating: 2.5 } | |
| ] | |
| favorite_movies.each do |movie| | |
| puts "#{movie[:year_released]}: #{movie[:title]}" | |
| 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
| EASTEREGG = "journey.txt" | |
| class TrainSchedule | |
| FILENAME = 'schedule.txt' | |
| @@schedule = {} | |
| def initialize | |
| if @@schedule.empty? | |
| File.open(FILENAME, 'r').each do |line| | |
| train, time = line.split(",") |
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 deep_copy(obj) | |
| # from: http://stackoverflow.com/a/8206537 | |
| # serializes the object, then unserializes it to make a duplicate | |
| return Marshal.load(Marshal.dump(obj)) | |
| 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 Television | |
| attr_accessor :manufacturer, :hd_compliant, :aspect_ratio, :screen_size | |
| def initialize(manufacturer, hd_compliant, aspect_ratio, screen_size) | |
| @manufacturer = manufacturer | |
| @hd_compliant = hd_compliant | |
| @aspect_ratio = aspect_ratio | |
| @screen_size = screen_size | |
| 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 :color, :owner, :cylinders | |
| def initialize(color, owner, cylinders) | |
| @color = color | |
| @owner = owner | |
| @cylinders = cylinders | |
| end | |
| end |
OlderNewer