Menu
CoddyTech

Names and Objects

Lesson 3 of 48 in Coddy's Ruby for C# Developers course.

C# local variables have static types, even when var infers them, while dynamic explicitly changes how operations are bound. Ruby names refer to dynamically typed objects and can be rebound to different kinds of values. Assignment shares class objects in C# and ordinary objects in Ruby; C# value types follow different copy rules.

C#: complete program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
int x=4; {int inner=9; Console.WriteLine(inner);} Console.WriteLine(x);

Ruby: complete program

label = "cedar"
old = label
label = label.length
puts label
puts old
challenge icon

Challenge

Easy

Input is a quantity and a unit price, separated by whitespace. Return their product minus a fixed discount of 3 as decimal text. Both inputs are integers in 0..100.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for C# Developers

Practice on your own: Online Ruby compiler