기본값과 키워드
Coddy의 C# 개발자를 위한 Ruby 코스 레슨. 48개 중 13번째.
C#은 선택적 매개변수와 명명된 인수, 그리고 오버로드 해결을 지원합니다. Ruby는 기본값과 키워드 매개변수를 지원하지만 Ruby 3에서는 키워드와 위치 해시를 구분합니다. 키워드 인수를 전달하려면 이중 별표 확장을 사용하세요. Ruby 메서드를 다시 정의하면 정적으로 선택되는 C# 오버로드 집합이 생성되는 대신 기존 메서드가 대체됩니다.
C#: 완전한 프로그램
using System;
using System.Collections.Generic;
using System.Linq;
static string Border(string text,string mark=".")=>mark+text+mark;
Console.WriteLine(Border("x"));Console.WriteLine(Border("x",mark:"!"));
Ruby: 완전한 프로그램
def border(text, mark: ".")
mark + text + mark
end
puts border("x")
puts border("x", mark: "!")챌린지
쉬움charge(units, rate: 4, discount: 0)을 정의하여 units*rate-discount를 반환하세요. 입력에는 units만 있거나, units rate 또는 units rate discount가 있습니다. solve는 생략된 필드에 기본값을 사용해야 합니다.
출력하지 말고 solve(input)에서 텍스트를 반환하세요. 잠긴 드라이버는 반환된 텍스트를 마지막에 줄바꿈 하나를 붙여 출력하고, 필수 공개 API를 독립적으로 검사합니다.
코드는 exercise.rb, pricing.rb에 작성하세요. main.rb에는 제공된 입력/출력 코드가 들어 있으며 수정할 수 없습니다.
직접 해보기
require_relative 'exercise'
require_relative 'pricing'
def check(value)
raise 'Public API check failed' unless value
end
check(charge(7) == 28)
check(charge(7, discount: 3) == 25)
check(charge(2, rate: 9, discount: 1) == 17)
puts solve(STDIN.read)
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
C# 개발자를 위한 Ruby의 모든 레슨
직접 연습해 보세요: 온라인 Ruby 컴파일러