Skip to content

Add lesson-1 comments#1

Open
psylone wants to merge 1 commit into
dronky:masterfrom
psylone:master
Open

Add lesson-1 comments#1
psylone wants to merge 1 commit into
dronky:masterfrom
psylone:master

Conversation

@psylone
Copy link
Copy Markdown

@psylone psylone commented May 16, 2017

No description provided.

Comment thread comments/ruby1.4.rb
# вернет 4, т.е. квадратный корень из 16.

puts "Enter a b c (with delimiter ','):"
sides = gets.chomp.split(',').map {|i| Float(i) }
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно используют to_f.

Ещё можно применить сокращённую форму вызова метода в итераторе map:

sides = gets.chomp.split(',').map(&:to_f)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Второй момент, я догадываюсь почему, но sides здесь совсем не sides, а coefficients.

Comment thread comments/ruby1.4.rb
sides = gets.chomp.split(',').map {|i| Float(i) }
a = sides[0]
b = sides[1]
c = sides[2]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно воспользоваться параллельным присваиванием:

a, b, c = sides

А можно сразу работать через массив sides, чтобы не плодить лишних локальных переменных.

Comment thread comments/ruby1.4.rb
a = sides[0]
b = sides[1]
c = sides[2]
if (d=b**2-4*a*c) == 0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В данном случае присваивание лучше вынести за пределы if.

Comment thread comments/ruby1.4.rb
elsif d<0
puts "no roots of equation"
elsif d>0
puts "x1 = #{(-b+Math.sqrt(d))/(2*a)}\nx2 = #{(-b-Math.sqrt(d))/(2*a)}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь стоит разделить логику вывода и вычислений. Иначе трудно читать, поддерживать и ориентироваться.

Comment thread comments/ruby1.3.rb
puts 'triangle is rectangular'
end
else
puts "something went wrong"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему что-то пошло не так? Треугольник может существовать, просто не быть прямоугольным.

Comment thread comments/ruby1.3.rb
b = sides[1]
c = sides[2]
case sides.max
when a
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Подумай как можно это отрефакторить чтобы не считать три раза теорему Пифагора.

Comment thread comments/ruby1.3.rb
else
puts "something went wrong"
end
if [a, c].include? b
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[3, 5].include?(3) - равнобедренный, но не равносторонний.

Comment thread comments/ruby1.3.rb
puts 'triangle is equilateral and isosceles'
elsif a == b || a == c || b == c
puts 'triangle is isosceles'
end No newline at end of file
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обычно пишут в таком формате:

if <condition>
  <code>
else
  <code>
end

Comment thread comments/ruby1.3.rb
end
if [a, c].include? b
puts 'triangle is equilateral and isosceles'
elsif a == b || a == c || b == c
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Подумай как ещё можно определить это условие.

Comment thread comments/ruby1.2.rb
a = gets.chomp.to_f
puts 'Enter h'
h = gets.chomp.to_f
puts "S = #{0.5*a*h}" No newline at end of file
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит вычислять площадь заранее и присваивать результат локальной переменной, затем её здесь выводить чтобы разделить логику вычисления и вывода.

Comment thread comments/ruby1.1.rb
puts 'Fine, now i want to know your height'
height = gets.chomp.to_i
best_weight = height - 110
if best_weight <= 0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По-моему, должно быть наоборот )

Comment thread comments/ruby1.1.rb
if best_weight <= 0
puts "Ok, #{name}, yor best weight is #{best_weight}"
else puts "Your weight is good"
end No newline at end of file
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как правило, условные выражения форматируют так:

if <condition>
  <code>
else
  <code>
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant