Loop Method (and others) - What is "#{i}"


#1


https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/2/1?curriculum_id=5059f8619189a5000201fbcb

I keep seeing a series of characters and I have no idea what they mean. I had assumed that in another exercise or two it would be explained, but it hasn't been. I went back to the start and did everything again and if this was explained I missed it entirely twice (totally possible).

i = 20
loop do
i -= 1
print "#{i}"
break if i <= 0
end

1) So, what does "#{i}" mean?
2) When is that explained to the user?

The only usage for quotes that I can recall off the top of my head is for strings of characters as in "Hello world!"

The only usage for the pound/hashtag that I can recall so far is to tell ruby to ignore what follows for the sake of creating notes.
x = 15 # the variable x will be 15

the curly braces have several usages so far. I can't define them readily besides saying that they house a thing.

the letter i is a variable, in this case it starts as 20 and it's being changed as the loop runs to count down from 20 until and including 0.

In all of the previous exercises we can just use print or puts and the variable.
EX:
print i

So, why are we now using "#{i}" ?


counter = 1
while counter < 11
  puts counter
  counter += 1
end  # prints 1 through 10



i = 20
loop do
  i -= 1
  print "#{i}"
  break if i <= 0
end  # count from 20 to 0


#2

I'm not exactly sure when we were introduced to this, but going forward you may know it as string interpolation of an expression or variable. Big words for a fairly simple concept. It means inserting values of any class, including String, into a string, which may require class conversion in the process.

A number, a boolean, an array or hash, even a function all fit the definition of an expression, which interpolation is able to boil down to a string representation.

i is an integer. "#{i}" is the string representation of i in a dynamic way. If we knew what i was and it was fixed, then we could just write "8" and that would be enough. But i is changing all the time so we need some way to change the literal value that is displayed, regardless what i is. That is where this syntax comes in to save the day. Method, actually, not syntax.


#3

Thanks!

This hasn't been taught where I'm at in the lessons for certain (just went over it again).

Why is it in quotes?
Why is it using a hashtag?

Are any of these things modifiers to change functionality or should I just brute memorize "#{VARIABLE}" means string interpolation of an expression or variable (I still don't feel like I know exactly what that means - even after looking up interpolation ... which sounded like a made up word)

Also you explanation uses some other terms I'm not familiar with:
What is a class?
What is a class conversion?
What is a hash?

(I feel comfortable with the definitions of string, number, boolean, array, expression, and variable)

I'm still not clear on why they went from just printing the changing variable as they did with previous lessons to using this new, and as of yet undefined within the lessons, functionality. I know you don't have that answer. I'm just whining (and hoping the lessons get fixed, or that the next series of lessons will learn from the current generation).


#4

Because it is written inside a string.

home = 4
visitor = 2
puts "The Oilers doubled up on the Flames, #{home} to #{visitor}."

# The Oilers doubled up on the Flames, 4 to 2.

Because it is a Ruby method.

An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
http://www.tutorialspoint.com/ruby/ruby_classes.htm

String objects are instances of the String class, integers are instances of the Integer class which is a subclass of the Numeric class. Classes contain the methods that are applicable to their instances. When you read about string methods, for example, they are written into the 'blueprint' and may only be invoked on string objects.

A class conversion is when we need to represent a non-string, such as a number, as a string. So it is converted to the String class. In Ruby there are several methods for converting objects, usually in the form, to_x where x could be s, i, f, a, etc. In many languages we refer to a class as a data type.

It is an associative array, in a sense. A hash takes the form { :symbol => value } or { key: value }. The data in a hash is written in comma separated key-value pairs.

With practice this will all become commonplace. Remember we are only getting an introduction here and the older courses are written by volunteers, not teachers.


#5

Sorry to ask more questions! I'm not trying to be difficult. I just want to understand what is going on in these exercises designed to teach us. I'm VERY thankful for the community (and especially yourself) who are taking the time to explain these (not terribly well written - but ya get what ya pay for and at least the ruby ones are better than the javascript ones) exercises.

So, in previous exercises we used the variable i to perform the task of displaying the changing number (the original question posted two different scripts, the first just uses i, and the second does this much more complex thing).

So, instead if just using i we are turning the variable i into a string by using quotes, then we are using a ruby method which does some sort of thing, then we call that original variable, and the end result is that by adding the quotes to make it a string, and use the hashtag for some purpose we end up with displaying the variable itself as if we hadn't used any of these extra characters?

Seems like a needlessly complicated step to take unless they're trying to introduce some of these new concepts to us. But, that's ok!

I still do not understand what a hash is, but this is my fault, not yours. I'm sure your definition is wonderful. I just can't visualize it yet to start understanding it. Maybe this will be taught in a bit!

Thanks again for all of your help. I'm very appreciative of your time and effort!


#6

The simplest form of string interpolation is concatenation, but as we can see, it is heavily laden with quotes.

home_team =  "Oilers"
visiting_team = "Flames"
home = 4
visitor = 2
"The score in tonight's game between " + home_team + " and " + visiting_team + " was " + home + " - " + visitor + "."

By using the string interpolation method we write one string and simply insert the method with a variable or expression argument in the curly braces.

"The score in tonight's game between #{home_team} and #{visiting_team} was #{home} - #{visitor}."

If you recall in JavaScript we wrote objects as key-value pairs in much the same way we do in Ruby hashes. In Python these objects are called dictionaries. They all serve pretty much the same purpose in their basic forms. They are an important data structure in the general group of reference objects.

JavaScript object literal

var myObj = {
    prop1: "value 1",
    prop2: "value 2"
};

Ruby hash literal

myHash = {
    prop1: "value 1",
    prop2: "value 2"
}

We liken these to associative array because they have keys, each with corresponding associated values. We look up values in a hash by referring to the key.

puts myHash['prop2']     # value 2

Both languages have constructors for these objects.

var myObj = new Object()    // a new object instance

myHash = Hash.new            # a new hash instance

Ruby's constructor is much more powerful than JS's since it can assign default values to new keys.

frequencies = Hash.new(0)

Every time a new key is created it is automatically given an initial value of 0. You will see this in the Create a Histogram module (which you should not be in hurry to reach).

Ruby hashes also permit the use of symbols as keys. You will cover this in the module, "A Night at the Movies".

Stay with the current lessons until you are fully comfortable with the terms and concepts presented. If that means giving yourself more practice and reading, then commit to it. Don't rush headlong through the track. Let it sink in.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.