Created
September 19, 2017 22:55
-
-
Save artemgurzhii/2bdb8a0f1e318308bd807777ee80b783 to your computer and use it in GitHub Desktop.
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 'digest' | |
class Block | |
attr_reader :index, :hash | |
def initialize(index:, timestamp: Time.now, data:, previous_hash:) | |
@data = data | |
@index = index | |
@timestamp = timestamp | |
@previous_hash = previous_hash | |
str = [index, timestamp, data, previous_hash].map(&:to_s).to_s | |
@hash = Digest::SHA256.hexdigest(str) | |
end | |
end | |
b0 = Block.new(index: 0, data: 'Zero', previous_hash: '0') | |
b1 = Block.new(index: b0.index + 1, data: 'First', previous_hash: b0.hash) | |
b2 = Block.new(index: b1.index + 1, data: 'Second', previous_hash: b1.hash) | |
[b0, b1, b2].each do |block| | |
p block | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment