Skip to content

Instantly share code, notes, and snippets.

Avatar

Pasha Bratanov zgfif

View GitHub Profile
View superhero_2_0.rb
require 'active_model'
class Superhero
include ActiveModel::Validations
@@total = []
attr_reader :name, :age
def initialize name, age
@name = name
View superhero.rb
require 'active_model'
class Superhero
include ActiveModel::Validations
attr_accessor :name, :age
def initialize **args
@name = args[:name]
View act_as.rb
module ActsAsWalker
attr_reader :distance
def walk!
@distance += 1
end
end
module ActsAsSpeaker
attr_reader :speak, :gills
View method.rb
class Arsenal
def self.hello
'Hello as class method!'
end
def mymy
'Arsen Wenger'
end
View square.rb
ar = [1, 2, 4]
def square_values array
array.map! {|e| e * e }
end
p square_values ar
View args.rb
def summarize *args
"#{args[0]} #{args[1]} #{args[2]}"
end
p summarize 1,2,3,4,5
View params.rb
def hello_message options = {}
first_name = options.fetch :first_name
last_name = options.fetch :last_name
"Hello, #{first_name} #{last_name}"
end
data = {first_name: 'Lero4ka', last_name: 'Melnik' }
# p hello_message data
View validation_basics.rb
require 'active_model'
class Person
include ActiveModel::Validations
attr_accessor :name, :age, :size
def initialize name: nil, age: nil, size: nil
@name = name
@age = age
View program6_spec.rb
require 'rspec'
require 'rspec/its'
require_relative 'task_seven'
RSpec.describe TaskSeven do
let(:array) { [5, -3, 3, -5, 6, 6, 6, -1, 0, 2] }
subject { described_class.new array }
its(:number_elements_after_max) { should eq 3 }
View program5_calculator.rb
class Calculator
def initialize(var_x, var_y)
@var_x = var_x
@var_y = var_y
end
def divide
@var_x / @var_y
end
end
You can’t perform that action at this time.