Skip to content

Instantly share code, notes, and snippets.

Avatar

Pasha Bratanov zgfif

View GitHub Profile
@zgfif
zgfif / program1.rb
Created Dec 15, 2018
Реалізувати програму, яка буде приймати аргументом рядок (String) і рахувати скільки разів зустрічається кожна літера в цьому рядку.
View program1.rb
def count_letters(text)
array = []
text = text.downcase
text.each_char do |char|
if char =~ /[A-Za-z]+/
count = text.count(char)
array << [char, count]
end
end
array = array.uniq
View program4_validation.rb
require 'active_model'
class Person
include ActiveModel::Validations
attr_accessor :name, :age
def initialize(name: nil, age: nil)
@name = name
@age = age
View program2_animal.rb
class Animal
attr_reader :distance
def initialize
@distance = 0
end
def step!
@distance += 1
end
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
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 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 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 args.rb
def summarize *args
"#{args[0]} #{args[1]} #{args[2]}"
end
p summarize 1,2,3,4,5
View square.rb
ar = [1, 2, 4]
def square_values array
array.map! {|e| e * e }
end
p square_values ar
View method.rb
class Arsenal
def self.hello
'Hello as class method!'
end
def mymy
'Arsen Wenger'
end
You can’t perform that action at this time.