Ruby Course – Codemeets– 02
November 26, 2020
Ruby programming lesson 2. Today we going to learn more about Ruby. In this tutorial, we will focus on Variable, Strings, Numbers, and Operations. It can provide extensive knowledge.
Use Variable
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.launchschool.com
Now you know what is variable, let’s use it Ruby Programming
character_name = "jenny"
character_age = "23"
puts "my name is "+ character_name
puts "my age is "+ character_age
This Code output is My Name is jenny my age is 23. you can try this code in your pc.
Working with Strings Ruby Programming
Threads are traditionally alphabetical. Now you can see how it works. see below table and get idea about ruby strings.
Phone = "Apple Iphone"
Code | Output |
puts Phone.upcase | APPLE IPHONE |
puts Phone.downcase | apple iphone |
puts Phone.strip | Apple Iphone |
puts Phone.length | 12 |
puts Phone.include?"Apple" | true |
puts Phone.include?"cake" | false |
puts Phone.[3,5] | le Ip |
puts "hp".upcase() | HP |
Work with Strings
Working With Numbers
Every computer language uses numbers in particular. This is because it allows you to easily control the data. let me show you how to work with numbers in Ruby.
Syntax | Code |
puts -5.43215 | -5.43215 |
puts 5+9 | 14 |
puts 5/9 | 0 |
puts 2*3 | 6 |
puts 2**3 | 8 |
puts 10%3 | 1 |
puts 5-2 | 3 |
Operations & Math in Ruby Programming
In mathematics, we use different operations, i.e. maximum value, square root, fall. They are also used differently in programming. The following example will give you a broader understanding.
Operations
num = 30.67
Code | Output |
puts num.abs() | 30.67 |
puts num.round() | 30 |
puts num.ceil() | 31 |
puts num.floor() | 30 |
Operations
Math
it is a hard mathematics part but using ruby we can do it easy. see below table.
Syntax | Output |
puts Math.sqrt (2) | 1.4142135623730951 |
puts Math.log (2) | 0.6931471805599453 |
Math
Now you have developed some knowledge about Ruby. Try typing this code on your computer as well. Practice
Output
Thank for all!
See you Next Ruby Lesson Soon!