Computer, please say something

Ruby programming learning guide for beginners. Covers various topics such as 'Computer, say something', 'Drawing stars', 'Ruby online interpreter', etc.

밤치 66

0. Preparation: Where to run the code?

You can install Ruby or run it online using an online execution tool (search for "ruby online repl").
The important thing is to "type and run the code by yourself".

Just remember one rule

  • Instead of copy-pasting, make sure to type the code yourself

  • It's okay to encounter errors. That's when it gets deeply engraved in your brain.


Step 1: "Computer, say something" - Grasping the smallest frame

1-1. First code: Hello, Ruby

puts "Hello, Ruby"
  • puts: Feels like "print line". Means to output a line to the console

  • "Hello, Ruby": Just a string (a bunch of characters)

In fact, this one line is like this sentence.

"Computer, please display this text on the screen."

Brain Frame (Park Munho style)

  • Frame: "Code is an instruction" → One line is one thought.

  • Visualization: You can imagine puts as a mouth. → Computer's mouth.

  • Recall: Repeat entering by changing only the characters.


Practice 1-1. Create your own first sentence

Try outputting three lines of your desired sentences like this:

puts "Coding is so intimidating..."
puts "Let's give it a try."
puts "It's not as difficult as I thought."

Assignment A

  1. Replace the above 3 lines with your own sentences

  2. Try outputting at least 5 lines

  3. Write a list of "Things I want to achieve by the end of this year" using puts only


Step 2: "Can you say it 5 times?" - Embracing the magic of repetition

Now, let's plant the frame of repetition pattern in your brain.

2-1. Saying the same thing multiple times

5.times do
  puts "Coding is fun"
end
  • 5.times: "Do it 5 times"

  • do ... end: Range of the repeating code

  • The puts inside will be executed 5 times

Brain Frame

  • Frame: "Repeating things is expressed as a loop."

  • Visualization: Imagine a circular loop where numbers 1, 2, 3, 4, 5 are spinning around

  • Recall: Try changing the numbers to 3, 10, 100 and repeat typing


Practice 2-1. Brainwash yourself

10.times do
  puts "I learn much faster than I thought"
end

Assignment B

  1. Change 10.times to 3.times, 7.times, and try running it

  2. Change the sentence to:

- "I never give up"

- "Even a little every day"

Create at least 3 versions of each sentence and output them multiple times

Step 3: When the pattern emerges - Turning on the 'visualization circuit' with star printing

Now, let's dive into the moment when your brain visualizes patterns.

It's time to print stars.

3-1. A single line of stars

puts "*"

This is a single star.

3-2. Printing 5 stars vertically

5.times do
  puts "*"
end

It will display:

*
*
*
*
*

Your brain will perceive this sensation.

"Ah, this is the pattern of '5 stars vertically'."


3-3. Creating a growing staircase

Now, here comes the really fun part.

1.upto(5) do |n|
  puts "*" * n
end
  • 1.upto(5): From 1 to 5, putting each number into n and repeating

  • |n|: Variable name used in each iteration

  • "*" * n: String that repeats the character "*" n times

Output:

*
**
***
****
*****

At this point, your brain will experience an explosion of pattern recognition.

  • Numbers increase from 1→2→3→4→5

  • Number of stars increases from 1→2→3→4→5

  • A 'staircase' is drawn before your eyes

Brain Frame

  • Frame: "Relationship between numbers and shapes"

  • Visualization: Feeling like the code has become a pen drawing shapes

  • Recall: Try changing the number range and drawing multiple times (3, 7, 10, etc.)


Practice 3-1. Making the staircase bigger, even bigger

Assignment C

  1. Change 1.upto(5) to 1.upto(10) and see what happens

  2. Instead of puts "*" * n

    puts "#" * n
    

    Try changing it to this (Emojis work too!)

  3. Try creating the following shape yourself:

    #
    ##
    ###
    ####
    #####
    

    → Hint: Use "#" instead of "*"


Step 4: Implanting 'thought' into the code with conditional statements

Now, let's raise the frame of conditions (if) in your brain.

Conditions are a structure of thoughts like "In this case, do this; in that case, do that."

4-1. Distinguishing between even and odd numbers

1.upto(10) do |n|
  if n.even?
    puts "#{n} is an even number"
  else
    puts "#{n} is an odd number"
  end
end
  • n.even?: Returns true if n is even, false otherwise

  • if ... else ... end: Executes different code depending on the condition

  • #{n}: Inserting a variable into a string

Output example:

1 is an odd number
2 is an even number
3 is an odd number
...
10 is an even number

Here, your brain learns these things.

"Ah, the code can make decisions."

"I can express all situations with rules and conditions."

Brain Frame

  • Frame: "Branching structure based on conditions"

  • Visualization: A picture of a tree branch splitting into two branches from top to bottom

  • Recall: Practice by changing the condition to something other than even?


Practice 4-1. Handling mood branching(?)

Assignment D

Create the following code yourself and change the sentences in your own style.

1.upto(5) do |score|
  if score >= 4
    puts "Mood score #{score}: Today is the best mood!"
  else
    puts "Mood score #{score}: Still getting things done today."
  end
end

Extra Challenge:

  • Expand the score range to 1.upto(10)

  • Divide the scores into 3 levels based on score

    • 8 or higher: "Life's on easy mode"
    • 4 to 7: "Average but meaningful"
    • 3 or lower: "Need another cup of coffee"

Step 5: Trying to create a very small "mini program"

Now, let's combine what you've learned (output, repetition, conditions)

and create a small program.

Topic:

"Record the time I study every day,

and show the total study time of the day."

5-1. Daily study time calculator

study_times = [30, 45, 60, 20]  # In minutes: Studied four times

total = 0

study_times.each do |minutes|
  total = total + minutes
end

puts "Study time today: #{total} minutes"

if total >= 120
  puts "Great job! You did a lot today "
elsif total >= 60
  puts "Good, this pace is enough for growth "
else
  puts "A bit disappointing, but it's a start. Let's add more tomorrow "
end

Code Interpretation

  • study_times: Stores today's study time as an array

  • total = 0: Variable to store the sum

  • each do |minutes|: Take one by one from the array and use it as minutes

  • total = total + minutes: Summation

  • Last if ~ elsif ~ else: Displays different messages based on the total study time

Brain Frame

  • Frame: "Data (array) + Repetition (each) + Condition (if) = Small system"

  • Visualization: Feeling like four time values are going into the total box through a pipe

  • Recall: Change the array content, change the benchmark time, change the messages


Practice 5-1. Create your own study·exercise·work record keeper

Assignment E

  1. Change study_times to exercise time

    exercise_times = [10, 15, 20]  # Exercised three times
    
  2. Change the if condition based on exercise time

- Over 60 minutes: "Escaped the beginner stage today"

- Over 30 minutes: "Maintaining a good routine"

- Otherwise: "Still got up from bed. Quite meaningful"
  1. Completely change the topic to something new
- "Time spent making YouTube videos today"

- "Time spent reading books today"

- "Time spent playing with kids today"

Step 6: "This is the foundation of Ruby on Rails"

What you've done now

is actually applied like this in Rails:

  • Array → List of posts fetched from the database (Post.all)

  • each loop → Repeating the post titles in the post list on the screen

  • if condition → Displaying buttons if logged in, hiding them if not

  • String output → Rendering HTML text

The code snippets we just created

will later transform into something like this in Rails:

@posts.each do |post|
  if post.published?
    puts post.title
  end
end

Right now, it's just console output,

but with a little expansion, it becomes a real web service screen.


Final: Today, you've already tasted the "first flavor of coding"

If you've followed along this far,

you've already planted these frames in your brain.

  • Command (output)

  • Repetition (times, upto, each)

  • Pattern (star printing)

  • Condition (if)

  • Data structure (array)

  • Small logic (study time calculator)

This is not just about learning a few syntaxes.

In Park Munho's words,

"A new structural thinking circuit has been created in your brain."

Comments

Add a Comment

Sign in to comment