<, element name,
</, element name,
=and the values are wrapped in
Restaurant, teach it to calculate its average rating, and add that to its instruction set. Ruby programmers very often share these new classes with one another, making the language ever more powerful — Rails itself is largely just a collection of these new nouns and verbs related to web applications!
"Hello", which is a
downcaseitself, which it happily does.
9to downcase itself? Try it right now in
NoMethodError: undefined method 'downcase' for 9:Fixnum. It looks scary, but one of your main objectives during this course is to not panic when you see an error message. Slow down, read it carefully, and see if you can make any sense of it at all. Over time, you will find that they are very helpful — and you will miss them when something is going wrong silently.
Fixnummeans "integer", as opposed to
Floatwhich means "decimal number". Almost all programming languages use two distinct data types for those two things.)
object.method. However, here's one small wrinkle: some methods require additional inputs. For example, there is a method called
gsubwhich we can call on
Strings, which will substitute characters with other characters. Try it:
gsubmethod needs to know what character to substitute and what to replace it with. So we give it inputs, or arguments, which must come in parentheses immediately following the method (no space after the method name). If the method takes multiple arguments, as
gsubdoes, then they are separated by commas.
gsubis more often used to do things like removing illegal characters from usernames before saving:
""is an empty string, so all
+s get replaced with nothing.)
object.method(argument1, argument2, ...)is the vast majority of what we'll be writing. That's it.
7 * 6? That is clearly not
object.method." A very good question. Try this instead:
*is the name of a method on the
Fixnumclass, which takes an argument of another
Fixnum, and returns the product of the two. 😳💥😲
7.*(6)every time they wanted to multiply two numbers, they would not be happy campers. So he included a few bits of "syntactic sugar", or shortcuts, that boil down to
object.methodunder the hood, but allow us to retain our sanity while typing. Phew.
object.methodexpression with the
objectthat it returns and then evaluating the next
objectthat the first
object.methodreturns matches up with the second
method.What class of object do I have? What methods can I call on this kind of object?
sand put in something new:
object.method, the syntax you need to know like the back of your hand is
storage_box, which will be created if it doesn't exist, or will have its value replaced if it does.
object.methodand store the result in some variable, then we do some more work on that variable and store the result in yet another variable, and a hundred steps later we've produced our final result and we display that to our user:
z. Use underscores to separate words in multiple word variable names.