Ruby and Rails: DAY 3

iOS
6 min readMay 7, 2019

--

In the previous articles I talked about setting up Ruby and Rails on Ubuntu, explained the MVC architecture, how a rails application is structured. In this article I am going to explain the basics of Ruby, after reading this article you will understand:

  1. What are the style conventions in Ruby?
  2. What is IRB?
  3. What are the Basic of Ruby (Strings, Symbols, Numbers, nil, Operations, Type Conversion, basic Data Structures, Expressions and Return, puts vs Return)
  4. What is a Variable?
  5. What are methods for and why do we need them?

I have been brushing up on my Ruby and Rails skills if you need to do the same please read my previous articles:

Ruby History/setup:

Ruby on Rails file structure:

What are the style conventions in Ruby?

  1. Tabs: set to 2 spaces and indenting should be set to use spaces
  2. Comment: # is a comment
  3. When defining a method, variable, or file use snake_case all, all lowercase with _ separating words.
  4. Constants: ALL CAPS
  5. When working with do/end blocks, prefer {} when the entire code expression fits on one line.
  6. Classes : CamelCase

What is IRB?

Ruby has a built in interactive environment called “irb” can help run ruby code at the command line.

What are the Basic of Ruby (Strings, Symbols, Numbers, nil, Operations, Type Conversion, basic Data Structures, Expressions and Return, puts vs Return)?

String: Is a list of characters in a specific sequence. Strings can be surrounded by ‘’ or “”.

String Interpolation: Merge Ruby code with string, basic syntax #{ruby expression goes here }, only works with “”.

Symbols (immutable): created by placing a :before a word, used when you want to reference something like a string but don’t ever intend to print it to the screen or change it. Immutable while not 100% technically correct it is a user mnemonic device for now.

Numbers: basic form of a number is called integer (whole number). float contains a decimal

nil: To express nothing in Ruby use nil. A variable with a value of nil could be described as having ‘nothing’ or being ‘completely empty’. To check if something is a nil type use .nil?.

operations: +, — , %, *, ==

string concatenation: join two strings together

type conversion: If you have ‘35’ which is a string but you want to increment it by 2. You cant just do ‘35’ + 2 (Fixnum error). Convert the string to an int by using to_i method.

Data Structures: Most common arrays and hashes,

  1. Arrays — are used to organize information into an ordered list. List can be made up of strings, floats, booleans, or any other data type. An array is denoted by square brackets. Inside the brackets you can create a list of elements separated by commas.
  2. Hashes — is a set of key-value pairs, it is represented with curly braces {}. A key-value pair is an association where a key is assigned a specific value.

Expressions and Return: => is a hash rocket, an expression is anything that can be evaluated, and pretty much everything you write in Ruby is an expression. An expression is Ruby always returns something, even if thats an error message or nil.

puts: Tells ruby to print something to screen

return: Expressions do something and also return something, the value returned is not necessarily the action that was performed

What is a variable?

A way to label data, act as containers that hold information. Their sole purpose is to label and store data in memory. To assign a value to a variable use the = symbol. The name of the variable goes on the left and the value you want to store in the variable goes on the right. Variables point to values in memory.

How to get data from user?

Use gets, which stands for “get string”, it waits for the user to type information and press the enter key add \n. To get rid of the \n use chomp.

Explain Variable Scope?

Scope determines where in a program a variable is available for use. Scope is defined by where the variable is initialized or created. Variable scope is defined by a block. A block is a piece of code following a method invocation, usually delimited by end.

Explain Inner and outer scope?

Loops -> data inside loop is only available within the loop not outside of it

Look into for loops?????????????????

Explain different type of Variables?

Constants: Declared by all caps, used for storing data that never needs to change. IN RUBY YOU CAN CHANGE THE VALUE A CONSTANT HOLDS!!!

Global: Declared by starting the variable name with the dollar sign($). Available throughout entire app, overriding all scope boundaries. STAY AWAY THERE CAN BE UNEXPECTED complications when using them.

class variables: Declared by starting the variable name with @@, accessible by instances of your class as well as the class itself. Class variables must be initialized at the class level, outside of any method definitions. They can be altered using class or instance method definitions.

Instance variables: Declared by starting the variable name with one @ sign. These variables are available throughout the current instance of the parent class. Instance variables can cross some scope boundaries, but not all of them.

Local Variables: Declared by starting the variables name with neither $ or @, as well as not capitalizing the entire variable name.

What are methods for and why do we need them?

When developing code you might need a piece of code to run multiple times, instead of writing that piece of code over again, you would put that code into a procedure which allows you to extract the code to one place. In ruby this is called a method, before you use a method you must define it first with the def reserved word followed by the method name closed by the end reserved word.

Methods can take parameters, parameters are used when you have data outside of a method definition’s scope, but you need access to it within the method definition.

def talk(phrase)
puts phrase
end
talk("Hello how are you")

What is the method definition?

def talk(phrase)
puts phrase
end

What is the method signature?

def talk(phrase)

What are arguments?

talk("Hello how are you doing")

Arguments are pieces of information that sent to the method, when it is called to be modified or used to return a specific result

What are default parameters?

def talk(phrase="Hi")
puts phrase
end

Now the method can be called without any parameters and it will still work

What is mutating the caller?

Sometimes when calling a method, the argument call be altered permanently. I talked easily about scope and how local variables can not altered outside their method definition. Well there is an exception to this rule. The exception is when we perform some action on the argument that mutates the caller, we can in fact permanently alter variables outside the method definition’s scope.

a = [1, 2, 3]  
# Example of a method definition that modifies its argument permanently
def mutate(array)
array.pop => removes last element from array
end
p "Before mutate method: #{a}" => [1,2,3]
mutate(a) ==> 3
p "After mutate method: #{a}" =>[1,2]

Ruby is a pass-by-value or pass-by-reference language???????????? I will explain this in a later article.

I hope you understand the basic of Ruby and in the next article I will be explain Flow Control

  1. Loops & Iterators
  2. Arrays
  3. Hashes
  4. Variables as Pointers
  5. Blocks and Procs
  6. Exception Handling
  7. Exceptions & Stack Traces

Day 3 Part 2: https://medium.com/@tommarler/ruby-on-rails-day-3-part-2-f965b0979141

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

iOS
iOS

Written by iOS

iOS Developer, Go, Java, C#, Blockchain enthusiast, Data junkie

No responses yet

Write a response