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 LiddleLizzard | |
| CHROMOSOME_LENGTH = 500 | |
| include Comparable | |
| attr_accessor :chromosome | |
| def initialize | |
| @chromosome = BitString.new(CHROMOSOME_LENGTH).bit_string |
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 -w | |
| class BitString | |
| attr_accessor :bit_string | |
| def initialize string_length | |
| @bit_string = random_bit_string string_length | |
| 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 TopLevel | |
| module Greeter | |
| module Person | |
| def greeting | |
| "Hello from #{name}. I am a #{age} year old #{gender}." | |
| end | |
| 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
| fields = [:switch, :sha_ind, :dtdm, :utdm, :ttdm, :actual] | |
| def create_struct name, fields | |
| Struct.new(name, *fields) | |
| end | |
| def create_singleton_struct name, fields | |
| if Struct::const_defined? name | |
| Struct.const_get name | |
| else |
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 MockRetriever | |
| def self.fetch_rows database, table | |
| return ["#{rand(100)} Main St.", "#{rand(100)} Second Ave.", "#{rand(100)} Peach Ln."] | |
| end | |
| end | |
| module AddressHandler | |
| # @addresses = Hash.new( {} ) # old code - returns the SAME hash, not a new hash | |
| @addresses = Hash.new { |hash, key| hash[key] = {} } |
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 -w | |
| # chmod +x inject_0001.rb | |
| sclass = Struct.new('Person', :name, :age, :gender) | |
| people = [] | |
| people << sclass.new("Sue", 20, 'M') | |
| people << sclass.new("Sue", 20, 'F') | |
| people << sclass.new("Sue", 21, 'M') | |
| people << sclass.new("Joe", 20, 'M') |
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 InitialState | |
| def self.state | |
| :unborn | |
| end | |
| end | |
| module Baby | |
| def self.state | |
| :feed_me | |
| 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 Greeter | |
| def greet | |
| puts "Hello #{name}! You're a #{party}." | |
| end | |
| end | |
| members = %w[name party] | |
| customer = Struct.new('Customer', *members) do | |
| include Greeter |
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
| members = %w[NAME ADDRESS] | |
| members.map!(&:downcase) | |
| members.map!(&:to_sym) | |
| Customer = Struct.new(*members) do | |
| def print | |
| puts self.to_a.join(",") | |
| 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
| class VariableArguments | |
| def initialize *args | |
| @first, @second, @third = *args | |
| end | |
| end | |
| v = VariableArguments.new | |
| puts v.inspect | |
| v = VariableArguments.new("Fred") |
NewerOlder