- 01Getting Started
- 02Your First Output (puts)
- 03Working with Variables
- 04Conditionals (if statements)
- 05Loops
- 06Writing a Method (Function)
- 07Working with Arrays
- 08Loops (while statements)
- 09Classes (Object-Oriented Programming)
- 10Error Handling (begin...rescue)
- 11Handy Array Methods
- 12Recursive Functions
- 13Using the Standard Library
- 14The case/when Statement
- 15The Ternary Operator
- 16Inheritance (Extending a Class)
- 17Writing Comments
- 18Logical Operators (AND, OR, NOT)
- 19Constants (names starting with a capital)
- 20Splitting and Joining Strings (split, join)
- 21Writing nil-Safe Code
- 22Searching an Array (include?)
- 23Transforming an Array (map)
- 24Two-Dimensional Arrays (Tabular Data)
- 25Writing a Custom Exception Class
- 26Default Parameter Values
- 27Working with Set
- 28Testing with Your Own assert Method
- 29Higher-Order Functions (Passing a Block)
- 30Stacks and Queues (Basic Data Structures)
- 31Type Conversion (Casting) Basics
- 32Intro to Regular Expressions (Pattern Matching)
- 33The Binary Search Algorithm
- 34Building a Caesar Cipher (a Character-Shifting Cipher)
- 35Understanding How Bubble Sort Works
- 36Building and Displaying Dates (Basic Year/Month/Day Operations)
- 37Writing Multiple Test Cases Together
- 38Speeding Up Calculations with Memoization (Caching)
- 39Normalizing Strings (strip, Unifying Case)
- 40Shallow Copy vs. Deep Copy
- 41Enum Basics
- 42Flattening an Array
- 43Reversing a String and Checking for Palindromes
- 44Pairing Up Two Arrays (the zip operation)
- 45Rounding Numbers (floor, ceil, round)
- 46Multi-Line Strings (Heredoc)
- 47Methods That Return Multiple Values
- 48Finding the GCD and LCM
- 49Formatting Numbers (Padding Digits, Decimal Places)
- 50Cleanup Logic with begin/rescue/ensure
- 51Methods That Work with Any Type
- 52File Reading and Writing Basics
- 53Generating Random Numbers
- 54Bitwise Operators (AND, OR, XOR, shifts)
- 55Reading Command-Line Arguments
- 56Waiting for a Fixed Amount of Time (sleep)
- 57Watch Out for Floating-Point Rounding Errors
- 58Reading from Standard Input
- 59FizzBuzz (the Classic Practice Problem)
- 60Checking Whether a Number Is Prime
- 61Set Operations with Set (Union, Intersection, Difference)
- 62Converting Number Bases (Binary, Hex)
- 63Checking Balanced Parentheses (an Application of Stacks)
- 64Checking Whether Two Words Are Anagrams
- 65Checking Whether a Year Is a Leap Year
- 66Converting Temperature (Celsius to Fahrenheit)
- 67Prime Factorization
- 68[Applied] Build a Simple Inventory Management Tool
Converting Temperature (Celsius to Fahrenheit)
In this lesson you will convert between Celsius and Fahrenheit, practising how to turn a formula straight into code. This is for anyone searching for "Ruby Celsius Fahrenheit conversion".
Celsius to Fahrenheit is "C x 9/5 + 32", and Fahrenheit to Celsius is "(F - 32) x 5/9". Unit conversion is a classic programming exercise.
The sample code turns each formula directly into code as celsius_to_fahrenheit and fahrenheit_to_celsius. Notice how writing 9.0 explicitly as a decimal avoids the error that integer division would introduce.
A common stumbling block for beginners is integer division truncating the fractional part. In Ruby 9 / 5 between two integers gives an integer, so an accurate calculation needs an explicit decimal such as 9.0.
It is a classic conversion exercise: simple formulas turned straight into code, with good practice at keeping types in mind. The same pattern applies to plenty of other unit conversions, such as length and weight.
๐งช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
