Roshan Book

My Tech Notebook

Learning Ruby Part 12 Changing data type and Declaring constant

c=13 (here c has a integer value)

c.to_f – This changes c data type to float

This type of data changing is called Casting

Similarly for strings

num = 10
num.to_s => “10”

Declaring a constant

Start with capital letter. Like Variable=10

Learning Ruby Part 11 Looping

4.times {puts “manish”} – > prints manish 4 times

3.upto<7> {puts “manish”} – > Prints manish counting 3 to 7

15.downto<12>{puts “manish”} – > Prints manish from 15 upto 12. That is 4 times

0.step<15,5> {puts “manish”} – > Prints  manish 4 times. It steps by 5 , rather than 1. Counting starts from 0

1.upto<10> {|variable| puts variable} –> It prints the value inside variable and print it out like



3 to 10



Learning Ruby Part 10 Decision Making

== –> It compares values

!= – > If not equal to

< – > less than

<= – > less than equal to

> = –> greater than equal to

> –> greater than

&& – > When you want to test more than one conditions

Learning Ruby Part 9 Expressions





It gives 9

Assigning value of variable to variable



Now if we change value of d to 3, f still has value 33.





Same thing could be written as


Same thing could be done with –, *, /

Learning Ruby Part 8–Variables

We can create Methods that don’t belong to any class.

Declaring a common method

def crap

puts “I am a crappy crap”


Calling Method directly



Adding value to variable



Few things to keep in mind

A variable should start with letters or _ .

No spaces allowed

No symbols like %

Variables are case sensitive

Learning Ruby Part 7–Strings

String means bunch of text like “my name is manish”


Declaring a string

My name is Manish

Methods of Strings

.length  – >Returns number of characters. It includes space

.upcase –> Converts everything to uppercase

+ –> It adds two strings

.reverse-> It reverse the strings

.reverse.upcae – > Here we added two methods for a string. It will reverse and change string to uppercase

Full list of string methods can be found here

Learning Ruby Part 6–Argument

Learning Ruby Part 5–More on Objects

In Ruby every thing is an Object.

When we write 12, it is actually an object of class called FixNum.

To find out what class objects belong to, use




Kernel methods are common methods,which any class can used.

Some examples of Kernel methods



For kernel methods, we can directly write it, without mentioning class.

Learning Ruby Part 4–Methods

Methods are something which Objects do.

Defining a Method

class Manish
def talk
puts "Hi, my name is Manish"


Notice the two ends. One end , ends method and other one ends class. Now we have a class Manish having a Method Talk.


Calling Method

Objects are characteristics of Class and Methods are what Classes do.

Learning Ruby Part 3 – Inheritance

Inheritance is simply copying all the attributes from a base class to new class. It will save you lots of typing.


class Animal

attr_accessor :name, :age , :trait


This is how we declare a base class

class Dog < Animal


And this is how Dog class copies attributes from base class Animal. Now Dog has same attributes as Animal.

Adding Additional Attributes

Suppose you want to add extra attribute called type, declare Dog class like below

class Dog < Animal

attr_accessor :type


And now we have added attribute to Dog class, which is not in Base class “Animal”


%d bloggers like this: