Menu
CoddyTech

Names and Objects

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

Java local variables have a static type, including when var infers that type. Ruby local variables can be rebound to objects of different types. Java reference assignment shares an object, but primitive assignment copies a value; Ruby exposes integers through its object model. The Java example uses a separate integer binding instead of attempting an illegal string-to-integer reassignment.

Java: complete program

import java.util.*;
public class Main {

  public static void main(String[] args) {
    String label = "cedar";
    String old = label;
    int size = label.length();
    System.out.println(size);
    System.out.println(old);
  }
}

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 Java Developers

Practice on your own: Online Ruby compiler