Ruby Course – Codemeets– 02
November 26, 2020
Ruby Programming Lesson 2: Today, we will dive deeper into Ruby. In this tutorial, we will focus on Variables, Strings, Numbers, and Operations. By the end, you’ll have a solid understanding of these essential concepts in Ruby programming
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
In Ruby, strings are typically arranged in alphabetical order. Below is a table demonstrating how strings work in Ruby. You can use this to get a better understanding of how string manipulation and ordering function in Ruby.
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 programming language uses numbers to manage and manipulate data effectively. In Ruby, working with numbers is straightforward. Let me show you how to handle 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 various operations such as finding the maximum value, calculating square roots, and more. These operations are also implemented in programming but in slightly different ways. The following example will give you a deeper understanding of how to perform such operations in Ruby.
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!