Variables and Data Types
1.Overview & Sneak Peek
- High-level, meaning reading and writing Ruby is really easy—it looks a lot like regular English!
- Interpreted, meaning you don't need a compiler to write and run Ruby. You can write it here at Codecademy or even on your own computer (many are shipped with the Ruby interpreter built in—we'll get to the interpreter later in this lesson).
- Object-oriented, meaning it allows users to manipulate data structures called objects in order to build and execute programs. We'll learn more about objects later, but for now, all you need to know is everything in Ruby is an object.
- Easy to use. Ruby was designed by Yukihiro Matsumoto (often just called "Matz") in 1995. Matz set out to design a language that emphasized human needs over those of the computer, which is why Ruby is so easy to pick up.
2.Data Types: Numbers, Strings, Booleans
"I'm learning Ruby!"
).Computer programs exist to quickly analyze and manipulate data. For that reason, it's important for us to understand the different data types that we can use in our programs.
Reminder: never use quotation marks (' or ") with booleans, or Ruby will think you're talking about a string (a word or phrase) instead of a value that can be true or false. It's also important to remember that Ruby is case-sensitive (it cares about capitalization).
2.1.Set the following variables to the corresponding values:
my_num
to the value25
my_boolean
to the valuetrue
my_string
to the value"Ruby"
(note the capitalization!)
my_boolean = true
my_string = "Ruby"
3.Variables
One of the most basic concepts in computer programming is the variable.
You can think of a variable as a word or name that grasps a single
value. For example, let's say you needed the number 25 from our last
example, but you're not going to use it right away. You can set a
variable, say
my_num
, to grasp the value 25 and hang onto it for later use, like this:my_num = 25
Declaring variables in Ruby is easy: you just write out a name like my_num
, use =
to assign it a value, and you're done! If you need to change a variable, no sweat: just type it again and hit =
to assign it a new value.my_num
to the value 100
, then click the Run button to run your code.my_num = 100
4.Math
Ruby isn't limited to simple expressions of assignment like
There are six arithmetic operators we're going to focus on:
Addition (
Subtraction (
Multiplication (
Division (
Exponentiation (
Modulo (
The only ones that probably look weird to you are exponentiation and modulo. Exponentiation raises one number (the base) to the power of the other (the exponent). For example,
Modulo returns the remainder of division. For example,
my_num = 100
; it can also do all the math you learned about in school.There are six arithmetic operators we're going to focus on:
Addition (
+
)Subtraction (
-
)Multiplication (
*
)Division (
/
)Exponentiation (
**
)Modulo (
%
)The only ones that probably look weird to you are exponentiation and modulo. Exponentiation raises one number (the base) to the power of the other (the exponent). For example,
2**3
is 8
, since 2**3
means "give me 2 * 2 * 2" (2 multiplied together 3 times). 3**2
is 9
(3 * 3), and so on.Modulo returns the remainder of division. For example,
25 % 7
would be 4
, since 7 goes into 25 3 times with 4 left over.
5.'puts' and 'print'
The
String Methods
Methods are summoned using a
9.1.Call
puts "YUYING".downcase
print
command just takes whatever you give it and prints it to the screen. puts
(for "put string") is slightly different: it adds a new (blank) line
after the thing you want it to print. You use them like this:puts "What's up?"
print "Oxnard Montalvo"
No parentheses or semicolons needed!String Methods
6.Everything in Ruby is an Object
Because everything in Ruby is an object (more on this later), everything in Ruby has certain built-in abilities called methods.
You can think of methods as "skills" that certain objects have. For
instance, strings (words or phrases) have built-in methods that can tell
you the length of the string, reverse the string, and more.
We also promised to tell you more about the interpreter. The interpreter is the program that takes the code you write and runs it. You type code in the editor, the interpreter reads your code, and it shows you the result of running your code in the console (the bottom window on the right).
We also promised to tell you more about the interpreter. The interpreter is the program that takes the code you write and runs it. You type code in the editor, the interpreter reads your code, and it shows you the result of running your code in the console (the bottom window on the right).
7.The '.length' Method
.
. If you have a string, "I love espresso"
, and take the .length
of it, Ruby will return the length of the string (that is, the number
of characters—letters, numbers, spaces, and symbols). Check it out:"I love espresso".length
# ==> 15
Taking the length of input can be useful if, for example, you want to
make a website that takes credit card payments. Ruby can check to make
sure the credit card number appears to be valid.
8.The '.reverse' Method
The
.reverse
method is called the same way .length
is, but instead of asking Ruby to tell you how long a string is, it
spits out a backwards version of the string you gave it. For instance,"Eric".reverse
will result in"cirE"
Reversing input can be useful if you want to sort a list of values
from highest to lowest instead of lowest to highest.
9.'.upcase' & '.downcase'
The
.upcase
and .downcase
methods convert a string to ALL UPPER CASE or all lower case, respectively..upcase
on your name to make your name ALL CAPS and use puts
to print it to the screen, like this:
puts "eric".upcase
# ==> ERIC
On the next line, call .downcase
to make your name all lower case. Make sure to use puts
so you can see it printed out!
Writing Good Code
10.Single-Line Comments
The
The
Review
In Ruby, you can do this two ways: each method call on a separate line, or you can chain them together, like this:
#
sign is for comments
in Ruby. A comment is a bit of text that Ruby won't try to run as code:
it's just for humans to read. Writing good comments not only clarifies
your code for other people who may read it, but helps remind you of what
you were doing when you wrote the code days, months, or even years
earlier.The
#
sign
should come before your comment and affects anything you write after it,
so long as you're on a single line.# I'm a full line comment!
"Eric".length # I'm a comment, too!
The second example will return 4
, since the comment comes after the code that Ruby will execute.
11.Multi-Line Comments
You can write a comment that spans multiple lines by starting each line with a
#
, but there's an easier way. If you start with =begin
and end with =end
, everything between those two expressions will be a comment. =begin
I'm a comment!
I don't need any # symbols.
=end
Don't put any space between the =
sign and the words begin
or end
. You can do that with math (2 + 5
is the same as 2+5
), but in this case, Ruby will get confused. =begin
and =end
also need to be on lines all by themselves, just as shown above.
12.Naming Conventions
There are many different kinds of variables you'll
encounter as you progress through these courses, but right now we're
just concerned with regular old local variables. By convention, these variables should start with a lowercase letter and words should be separated by underscores, like
counter
and masterful_method
. Ruby won't stop you from starting your local variables with other symbols, such as capital letters, $
s, or @
s, but by convention these mean different things, so it's best to avoid confusion by doing what the Ruby community does.In Ruby, you can do this two ways: each method call on a separate line, or you can chain them together, like this:
name.method1.method2.method3
Really nice blog post.provided a helpful information.I hope that you will post more updates like this
ReplyDeleteRuby on Rails Online Course Bangalore
I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting ruby certification
ReplyDelete
ReplyDeleteThanks for sharing such a Excellent Blog! We are linking too this particularly great post on our site. Keep up the great writing.
ruby assignment help
Students say that they face many different kinds of challenges while composing Ruby assignments to maintain their academic grades. Therefore, they seek Ruby assignment help from experts. Sample assignment is the experience, and the trusted Ruby programming writing service provider that helps university scholars to deliver their assignments on time.
ReplyDeleteI just had to share my excitement about how financial accounting has transformed my academic journey and boosted my confidence in handling numbers. If you're struggling with your financial accounting assignments, I've got some fantastic news for you!
ReplyDeleteThrough professional assignment help, I discovered a world of clarity and understanding. The experts guided me through the complex concepts, ensuring I gained a solid foundation in financial accounting. From journal entries to balance sheets, everything became less intimidating.
Not only did this assistance improve my grades, but it also equipped me with practical skills that I can apply in real-life scenarios. Financial Accounting Assignment Help is no longer a daunting subject, but rather a fascinating puzzle waiting to be solved.
So, if you're feeling overwhelmed with your financial accounting assignments, don't hesitate to seek guidance. It's a game-changer that will empower you to excel academically and pave the way for a successful future!
If you are a student at nyu new classes Assignment Help and are interested in gaining additional skills or knowledge, you may want to explore their new classes. If you need any assistance with coursework or assignments, BookMyEssay has expert writers and tutors that can provide personalized help to ensure your success.
ReplyDeletethanks for sharing this post is very helpful information, Most students have many worries during their school life like completing assignments in time, meeting deadlines of projects and exams. These habits are very useful for students as they get stressed when their exams come. It will help them to get out of stress and prepare for exams properly. I'm looking for online assignment help in Singapore because my projects are pending and my exams are coming this month. I want to finish all my projects before papers so I can easily focus on papers.
ReplyDelete