Objectives |
---|
Identify data types, operators, and control flow patterns in JavaScript and utilize them in Ruby |
Apply Ruby control flow to create command line applications |
As we learn Ruby, it's important to revisit how we learned our first language and use that to organize the study of our new language. Learning our second programming language is a process of translating concepts, expressions, and patterns from our familiar language into our new language. Learning our first language involved more identification and comprehension of the knowledge required to implement our first programs. We should begin by organizing this knowledge to build a better understanding as we transition to Ruby.
- Declarative Knowledge > your "what is" knowledge, i.e. describing what something is
- Imperative knowledge > your "how to" knowledge, i.e. describing how to do something
Primitives
Combinations
Abstractions
- What is JavaScript? What does it look like?
- What are some of the primitives in JavaScript?
- Think data types, variable declarations, conditionals, functions, etc.
- How did we use JavaScript to build things? How did we build up from the fundamentals of the language?
- What could possibly be different in another language? How could we change the syntax, but keep the semantics?
Options for running these challenges:
-
Use
irb
in your terminal. -
Create a
.rb
file. Put your code there and run withruby YOUR_FILE_NAME.rb
.
-
Store your
first_name
in a variable and yourlast_name
in another variable. -
Concatenate your
first_name
andlast_name
variables, and store the output in a new variable calledfull_name
. -
Use
.split
to turn yourfull_name
variable into an array.
- Print (
puts
) "Ruby is awesome!" 50 times. Implement this 3 different ways, using:
- Save any string to a variable, then create an empty hash called count (
count = {}
). Loop through the string, and count occurrences of each letter. Store the counts in your hash like this example:
- For the string
apple
, yourcount
hash would look like this:{a: 1, p: 2, l: 1, e: 1}
.
- Write a program that gets user input from the terminal and
puts
it until the input is the word"quit"
or"q"
.
- Hint: Use
gets.chomp
instead ofgets
to remove trailing\n
.
- Write a program that prints the "bottles of beer on the wall" song:
5 bottles of beer on the wall,
5 bottles of beer!
Take one down and pass it around,
4 bottles of beer on the wall!
...
- Use
gets.chomp
to ask the user how many verses they want to hear. - Make sure your song prints "1 bottle of beer".
- When the song gets to "0 bottles of beer on the wall", it should print "No more bottles of beer on the wall" instead.
-
Define an array of 4 phrases:
"Hello, world"
,"OMG"
,"Ruby"
, and"Pair Programming"
. Use.each
to iterate over the array andputs
each phrase. -
Iterate over your array of phrases again, but this time, only
puts
the phrase if its length is 5 letters or longer. Otherwise, print a message that the phrase is too short, and include the phrase's index in the message (Hint: Look up.each_with_index
).
-
Write a program that maps an array of numbers to double each number.
-
Write a program that maps an array of words to the reverse of each word. (Hint: Look up
.reverse
)
- Create a simple temperature convertor. It should function like the example below:
Type '1' to convert from Celsius to Fahrenheit or '2' to convert from Fahrenheit to Celsius
1
Enter Celsius temperature:
24
24 degrees Celsius is equal to 75.2 degrees Fahrenheit
- Create a simple calculator that first asks the user what method they would like to use (addition, subtraction, multiplication, or division), then asks the user for two numbers, printing the result of the method with the two numbers. Here is a sample prompt:
What calculation would you like to do? (add, sub, mult, div)
add
What is the first number?
3
What is the second number?
6
The result is 9