SlideShare a Scribd company logo
Ruby (on Rails)
Presented By
Tushar Pal
About the Section
• Introduce the Ruby programming language
• Basic understanding of ruby
What is Ruby?
• Programming Language
• Object-oriented
• Interpreted
Ruby Introduction
• Ruby originated in Japan during the mid-1990s
• Ruby is Based on Perl,Smalltalk,Eiffel,Ada and Lisp.
• Ruby offers automatic memory management.
• Ruby is Written in c
• Ruby is a dynamic interpreted language which has many
strong features of various languages.
• It is a strong Object oriented programming language.
Ruby is open source
• Ruby can be embeded into Hypertext Markup Language (HTML).
• Ruby has similar syntax to that of many programming languages such as
C++ and Perl.
Interpreted Languages
• Not compiled like Java
• Code is written and then directly executed by
an interpreter
• Type commands into interpreter and see
immediate results
Computer
Runtime
Environment
CompilerCodeJav
a:
ComputerInterpreterCodeRub
y:
What is Ruby on Rails (RoR)
• Development framework for web applications
written in Ruby
• Used by some of your favorite sites!
Advantages of a framework
• Standard features/functionality are built-in
• Predictable application organization
– Easier to maintain
– Easier to get things going
Installation
• Mac/Linux
– Probably already on your computer
– OS X 10.4 ships with broken Ruby! Go here…
• http://hivelogic.com/articles/view/ruby-rails-mongrel-
mysql-osx
RVM
Before any other step install mpapis public key (might need gpg2) (see
security)
gpg --keyserver hkp://keys.gnupg.net --recv-keys
409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable --ruby
hello_world.rb
puts "hello world!"
puts vs. print
• "puts" adds a new line after it is done
– analogous System.out.println()
• "print" does not add a new line
– analogous to System.out.print()
Running Ruby Programs
• Use the Ruby interpreter
ruby hello_world.rb
– “ruby” tells the computer to use the Ruby
interpreter
• Interactive Ruby (irb) console
irb
– Get immediate feedback
– Test Ruby features
Comments
# this is a single line comment
=begin
this is a multiline comment
nothing in here will be part of the code
=end
Variables
• Declaration – No need to declare a "type"
• Assignment – same as in Java
• Example:
x = "hello world" # String
y = 3 # Fixnum
z = 4.5 # Float
r = 1..10 # Range
Objects
• Everything is an object.
– Common Types (Classes): Numbers, Strings, Ranges
– nil, Ruby's equivalent of null is also an object
• Uses "dot-notation" like Java objects
• You can find the class of any variable
x = "hello"
x.class → String
• You can find the methods of any variable or class
x = "hello"
x.methods
String.methods
Objects (cont.)
• There are many methods that all Objects have
• Include the "?" in the method names, it is a
Ruby naming convention for boolean methods
• nil?
• eql?/equal?
• ==, !=, ===
• instance_of?
• is_a?
• to_s
Ruby defined? operators:
defined? is a special operator that takes the form of a method call to determine
whether or not the passed expression is defined.It returns a description string
of the expression, or nil if the expression isn't defined.
There are various usage of defined? Operator:
Example
foo = 42
defined? foo # => "local-variable"
defined? $_ # => "global-variable"
defined? bar # => nil (undefined)
Numbers
• Numbers are objects
• Different Classes of Numbers
– FixNum, Float
3.eql?2 → false
-42.abs → 42
3.4.round → 3
3.6.rount → 4
3.2.ceil → 4
3.8.floor → 3
3.zero? → false
String Methods
"hello world".length → 11
"hello world".nil? → false
"".nil? → false
"ryan" > "kelly" → true
"hello_world!".instance_of?String → true
"hello" * 3 → "hellohellohello"
"hello" + " world" → "hello world"
"hello world".index("w") → 6
Operators and Logic
• Same as Java
– Multiplication, division, addition, subtraction, etc.
• Also same as Java AND Python (WHA?!)
– "and" and "or" as well as "&&" and "||"
• Strange things happen with Strings
– String concatenation (+)
– String multiplication (*)
• Case and Point: There are many ways to solve
a problem in Ruby
if/elsif/else/end
• Must use "elsif" instead of "else if"
• Notice use of "end". It replaces closing curly
braces in Java
• Example:
if (age < 35)
puts "young whipper-snapper"
elsif (age < 105)
puts "80 is the new 30!"
else
puts "wow… gratz..."
end
Inline "if" statements
• Original if-statement
if age < 105
puts "don't worry, you are still young"
end
• Inline if-statement
puts "don't worry, you are still young" if age < 105
for-loops
• for-loops can use ranges
• Example 1:
for i in 1..10
puts i
end
• Can also use blocks (covered next week)
3.times do
puts "Ryan! "
end
for-loops and ranges
• You may need a more advanced range for
your for-loop
• Bounds of a range can be expressions
• Example:
for i in 1..(2*5)
puts i
end
while-loops
• Can also use blocks (next week)
• Cannot use "i++"
• Example:
i = 0
while i < 5
puts i
i = i + 1
end
unless
• "unless" is the logical opposite of "if"
• Example:
unless (age >= 105) # if (age < 105)
puts "young."
else
puts "old."
end
until
• Similarly, "until" is the logical opposite of
"while"
• Can specify a condition to have the loop stop
(instead of continuing)
• Example
i = 0
until (i >= 5) # while (i < 5), parenthesis not required
puts I
i = i + 1
end
Methods
• Structure
def method_name( parameter1, parameter2, …)
statements
end
• Simple Example:
def print_ryan
puts "Ryan"
end
Parameters
• No class/type required, just name them!
• Example:
def cumulative_sum(num1, num2)
sum = 0
for i in num1..num2
sum = sum + i
end
return sum
end
# call the method and print the result
puts(cumulative_sum(1,5))
Return
• Ruby methods return the value of the last
statement in the method, so…
def add(num1, num2)
sum = num1 + num2
return sum
end
can become
def add(num1, num2)
num1 + num2
end
User Input
• "gets" method obtains input from a user
• Example
name = gets
puts "hello " + name + "!"
• Use chomp to get rid of the extra line
puts "hello" + name.chomp + "!"
• chomp removes trailing new lines
Changing types
• You may want to treat a String a number or a
number as a String
• to_i – converts to an integer (FixNum)
• to_f – converts a String to a Float
• to_s – converts a number to a String
• Examples
"3.5".to_i → 3
"3.5".to_f → 3.5
3.to_s → "3"
Constants
• In Ruby, constants begin with an Uppercase
• They should be assigned a value at most once
• This is why local variables begin with a
lowercase
• Example:
Width = 5
def square
puts ("*" * Width + "n") * Width
end
Ruby Blocks
✓ You have seen how Ruby defines methods where you can put
number of statements and then you call that method. Similarly
Ruby has a concept of Block.
✓ A block consists of chunks of code.
✓ You assign a name to a block.
✓ The code in the block is always enclosed within braces ({}).
✓ A block is always invoked from a function with the same name
as that of the block. This means that if you have a block with the
name test, then you use the function test to invoke this block.
✓ You invoke a block by using the yield statement.
Syntax:
block_name{
statement1
statement2
..........
}
Here you will learn to invoke a block by using a simple yield
statement. You will also learn to use a yield statement with
parameters for invoking a block. You will check the sample
code with both types of yield statements.
The yield Statement:
Let us look at an example of the yield statement:
def test
puts "You are in the method"
yield
puts "You are again back to the method"
yield
end
test {puts "You are in the block"}
This will produce following result:
You are in the method
You are in the block
You are again back to the method
You are in the block
You also can pass parameters with the yield statement. Here is an
example:
def test
yield 5
puts "You are in the method test"
yield 100
end
test {|i| puts "You are in the block #{i}"}
This will produce following result:
You are in the block 5
You are in the method test
You are in the block 100
Here the yield statement is written followed by parameters. You can
even pass more than one parameter. In the block, you place a
variable between two vertical lines (||) to accept the parameters.
Therefore, in the preceding code, the yield 5 statement passes the
value 5 as a parameter to the test block.
Variables in a Ruby Class:
Ruby provides four types of variables:
1.Local Variables: Local variables are the variables that
are defined in a method. Local variables are not available outside
the method. You will see more detail about method in subsequent
chapter. Local variables begin with a lowercase letter or _.
2.Instance Variables: Instance variables are available
across methods for any particular instance or object. That means
that instance variables change from object to object. Instance
variables are preceded by the at sign (@) followed by the variable
name.
3. Class Variables: Class variables are available across
different objects. A class variable belongs to the class and is a
characteristic of a class. They are preceded by the sign @@ and are
followed by the variable name.
4.Global Variables: Class variables are not available
across classes. If you want to have a single variable, which is
available across classes, you need to define a global variable.
The global variables are always preceded by the dollar sign ($).
Variables in a Ruby Class:
Ruby Classes & Objects
class Customer
@@no_of_customers=0
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
Flow of Code
# Call Methods
cust1.display_details()
cust1.total_no_of_customers()
cust2.display_details()
cust2.total_no_of_customers()
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Total number of customers: 1
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Total number of customers: 2
✓ Inheritance is a relation between two classes.
✓ Inheritance is indicated with <.
class Mammal
def breathe
puts "inhale and exhale"
end
end
class Cat < Mammal
def speak
puts "Meow"
end
end
rani = Cat.new
rani.breathe
rani.speak
Inheritance
Features of Ruby
✓ Object-Oriented
✓ Portable
✓ Automatic Memory-Management (garbage collection)
✓ Easy And Clear Syntax
✓ Case Sensitive
Lowercase letters and uppercase letters are distinct. The
keyword end, for example, is completely different from the
keyword END. and features
✓Statement Delimiters
Multiple statements on one line must be separated by semicolons,
but they are not required at the end of a line; a linefeed is
treated like a semicolon.
✓ Keywords
Also known as reserved words in Ruby typically cannot be
used for other purposes.
A dynamic, open source programming language
with a focus on simplicity and productivity
✓ Open Source
References
• Web Sites
– http://www.ruby-lang.org/en/
– http://rubyonrails.org/
• Books
– Programming Ruby: The Pragmatic Programmers'
Guide (http://www.rubycentral.com/book/)
– Agile Web Development with Rails
– Rails Recipes
– Advanced Rails Recipes

More Related Content

What's hot

Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
Kushal Jangid
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
Knoldus Inc.
 
Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画
mametter
 
Java
JavaJava
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
ssoroka
 
Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015
Martin (高馬丁) Skarsaune
 
Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
Aniruddha Chakrabarti
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
Chiyoung Song
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Kotlin Types for Java Developers
Kotlin Types for Java DevelopersKotlin Types for Java Developers
Kotlin Types for Java Developers
Chandra Sekhar Nayak
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
mircodotta
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
Lou Loizides
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
Rafael Magana
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
GeekNightHyderabad
 

What's hot (17)

Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
 
Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画
 
Java
JavaJava
Java
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015
 
Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Kotlin Types for Java Developers
Kotlin Types for Java DevelopersKotlin Types for Java Developers
Kotlin Types for Java Developers
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 

Similar to Ruby basics

Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Ruby
RubyRuby
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 
Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)
Joachim Baumann
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
Shimi Bandiel
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
AbdulRazaqAnjum
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
Cory Foy
 
Java Closures
Java ClosuresJava Closures
Java Closures
Ben Evans
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
Andrei Rinea
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
NagaLakshmi_N
 
ppt7
ppt7ppt7
ppt7
callroom
 
ppt2
ppt2ppt2
ppt2
callroom
 
name name2 n
name name2 nname name2 n
name name2 n
callroom
 
ppt9
ppt9ppt9
ppt9
callroom
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
amiable_indian
 
ppt18
ppt18ppt18
ppt18
callroom
 

Similar to Ruby basics (20)

Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Ruby
RubyRuby
Ruby
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt9
ppt9ppt9
ppt9
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
 
ppt18
ppt18ppt18
ppt18
 

Recently uploaded

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

Ruby basics

  • 2. About the Section • Introduce the Ruby programming language • Basic understanding of ruby
  • 3. What is Ruby? • Programming Language • Object-oriented • Interpreted
  • 4. Ruby Introduction • Ruby originated in Japan during the mid-1990s • Ruby is Based on Perl,Smalltalk,Eiffel,Ada and Lisp. • Ruby offers automatic memory management. • Ruby is Written in c • Ruby is a dynamic interpreted language which has many strong features of various languages. • It is a strong Object oriented programming language. Ruby is open source • Ruby can be embeded into Hypertext Markup Language (HTML). • Ruby has similar syntax to that of many programming languages such as C++ and Perl.
  • 5. Interpreted Languages • Not compiled like Java • Code is written and then directly executed by an interpreter • Type commands into interpreter and see immediate results Computer Runtime Environment CompilerCodeJav a: ComputerInterpreterCodeRub y:
  • 6. What is Ruby on Rails (RoR) • Development framework for web applications written in Ruby • Used by some of your favorite sites!
  • 7. Advantages of a framework • Standard features/functionality are built-in • Predictable application organization – Easier to maintain – Easier to get things going
  • 8. Installation • Mac/Linux – Probably already on your computer – OS X 10.4 ships with broken Ruby! Go here… • http://hivelogic.com/articles/view/ruby-rails-mongrel- mysql-osx
  • 9. RVM Before any other step install mpapis public key (might need gpg2) (see security) gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 curl -sSL https://get.rvm.io | bash -s stable --ruby
  • 11. puts vs. print • "puts" adds a new line after it is done – analogous System.out.println() • "print" does not add a new line – analogous to System.out.print()
  • 12. Running Ruby Programs • Use the Ruby interpreter ruby hello_world.rb – “ruby” tells the computer to use the Ruby interpreter • Interactive Ruby (irb) console irb – Get immediate feedback – Test Ruby features
  • 13. Comments # this is a single line comment =begin this is a multiline comment nothing in here will be part of the code =end
  • 14. Variables • Declaration – No need to declare a "type" • Assignment – same as in Java • Example: x = "hello world" # String y = 3 # Fixnum z = 4.5 # Float r = 1..10 # Range
  • 15. Objects • Everything is an object. – Common Types (Classes): Numbers, Strings, Ranges – nil, Ruby's equivalent of null is also an object • Uses "dot-notation" like Java objects • You can find the class of any variable x = "hello" x.class → String • You can find the methods of any variable or class x = "hello" x.methods String.methods
  • 16. Objects (cont.) • There are many methods that all Objects have • Include the "?" in the method names, it is a Ruby naming convention for boolean methods • nil? • eql?/equal? • ==, !=, === • instance_of? • is_a? • to_s
  • 17. Ruby defined? operators: defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined.It returns a description string of the expression, or nil if the expression isn't defined. There are various usage of defined? Operator: Example foo = 42 defined? foo # => "local-variable" defined? $_ # => "global-variable" defined? bar # => nil (undefined)
  • 18. Numbers • Numbers are objects • Different Classes of Numbers – FixNum, Float 3.eql?2 → false -42.abs → 42 3.4.round → 3 3.6.rount → 4 3.2.ceil → 4 3.8.floor → 3 3.zero? → false
  • 19. String Methods "hello world".length → 11 "hello world".nil? → false "".nil? → false "ryan" > "kelly" → true "hello_world!".instance_of?String → true "hello" * 3 → "hellohellohello" "hello" + " world" → "hello world" "hello world".index("w") → 6
  • 20. Operators and Logic • Same as Java – Multiplication, division, addition, subtraction, etc. • Also same as Java AND Python (WHA?!) – "and" and "or" as well as "&&" and "||" • Strange things happen with Strings – String concatenation (+) – String multiplication (*) • Case and Point: There are many ways to solve a problem in Ruby
  • 21. if/elsif/else/end • Must use "elsif" instead of "else if" • Notice use of "end". It replaces closing curly braces in Java • Example: if (age < 35) puts "young whipper-snapper" elsif (age < 105) puts "80 is the new 30!" else puts "wow… gratz..." end
  • 22. Inline "if" statements • Original if-statement if age < 105 puts "don't worry, you are still young" end • Inline if-statement puts "don't worry, you are still young" if age < 105
  • 23. for-loops • for-loops can use ranges • Example 1: for i in 1..10 puts i end • Can also use blocks (covered next week) 3.times do puts "Ryan! " end
  • 24. for-loops and ranges • You may need a more advanced range for your for-loop • Bounds of a range can be expressions • Example: for i in 1..(2*5) puts i end
  • 25. while-loops • Can also use blocks (next week) • Cannot use "i++" • Example: i = 0 while i < 5 puts i i = i + 1 end
  • 26. unless • "unless" is the logical opposite of "if" • Example: unless (age >= 105) # if (age < 105) puts "young." else puts "old." end
  • 27. until • Similarly, "until" is the logical opposite of "while" • Can specify a condition to have the loop stop (instead of continuing) • Example i = 0 until (i >= 5) # while (i < 5), parenthesis not required puts I i = i + 1 end
  • 28. Methods • Structure def method_name( parameter1, parameter2, …) statements end • Simple Example: def print_ryan puts "Ryan" end
  • 29. Parameters • No class/type required, just name them! • Example: def cumulative_sum(num1, num2) sum = 0 for i in num1..num2 sum = sum + i end return sum end # call the method and print the result puts(cumulative_sum(1,5))
  • 30. Return • Ruby methods return the value of the last statement in the method, so… def add(num1, num2) sum = num1 + num2 return sum end can become def add(num1, num2) num1 + num2 end
  • 31. User Input • "gets" method obtains input from a user • Example name = gets puts "hello " + name + "!" • Use chomp to get rid of the extra line puts "hello" + name.chomp + "!" • chomp removes trailing new lines
  • 32. Changing types • You may want to treat a String a number or a number as a String • to_i – converts to an integer (FixNum) • to_f – converts a String to a Float • to_s – converts a number to a String • Examples "3.5".to_i → 3 "3.5".to_f → 3.5 3.to_s → "3"
  • 33. Constants • In Ruby, constants begin with an Uppercase • They should be assigned a value at most once • This is why local variables begin with a lowercase • Example: Width = 5 def square puts ("*" * Width + "n") * Width end
  • 34. Ruby Blocks ✓ You have seen how Ruby defines methods where you can put number of statements and then you call that method. Similarly Ruby has a concept of Block. ✓ A block consists of chunks of code. ✓ You assign a name to a block. ✓ The code in the block is always enclosed within braces ({}). ✓ A block is always invoked from a function with the same name as that of the block. This means that if you have a block with the name test, then you use the function test to invoke this block. ✓ You invoke a block by using the yield statement.
  • 35. Syntax: block_name{ statement1 statement2 .......... } Here you will learn to invoke a block by using a simple yield statement. You will also learn to use a yield statement with parameters for invoking a block. You will check the sample code with both types of yield statements.
  • 36. The yield Statement: Let us look at an example of the yield statement: def test puts "You are in the method" yield puts "You are again back to the method" yield end test {puts "You are in the block"} This will produce following result: You are in the method You are in the block You are again back to the method You are in the block
  • 37. You also can pass parameters with the yield statement. Here is an example: def test yield 5 puts "You are in the method test" yield 100 end test {|i| puts "You are in the block #{i}"} This will produce following result: You are in the block 5 You are in the method test You are in the block 100 Here the yield statement is written followed by parameters. You can even pass more than one parameter. In the block, you place a variable between two vertical lines (||) to accept the parameters. Therefore, in the preceding code, the yield 5 statement passes the value 5 as a parameter to the test block.
  • 38. Variables in a Ruby Class: Ruby provides four types of variables: 1.Local Variables: Local variables are the variables that are defined in a method. Local variables are not available outside the method. You will see more detail about method in subsequent chapter. Local variables begin with a lowercase letter or _. 2.Instance Variables: Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name. 3. Class Variables: Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.
  • 39. 4.Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($). Variables in a Ruby Class:
  • 40. Ruby Classes & Objects class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
  • 41. Flow of Code # Call Methods cust1.display_details() cust1.total_no_of_customers() cust2.display_details() cust2.total_no_of_customers() Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Total number of customers: 1 Customer id 2 Customer name Poul Customer address New Empire road, Khandala Total number of customers: 2
  • 42. ✓ Inheritance is a relation between two classes. ✓ Inheritance is indicated with <. class Mammal def breathe puts "inhale and exhale" end end class Cat < Mammal def speak puts "Meow" end end rani = Cat.new rani.breathe rani.speak Inheritance
  • 43. Features of Ruby ✓ Object-Oriented ✓ Portable ✓ Automatic Memory-Management (garbage collection) ✓ Easy And Clear Syntax ✓ Case Sensitive Lowercase letters and uppercase letters are distinct. The keyword end, for example, is completely different from the keyword END. and features
  • 44. ✓Statement Delimiters Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line; a linefeed is treated like a semicolon. ✓ Keywords Also known as reserved words in Ruby typically cannot be used for other purposes. A dynamic, open source programming language with a focus on simplicity and productivity ✓ Open Source
  • 45. References • Web Sites – http://www.ruby-lang.org/en/ – http://rubyonrails.org/ • Books – Programming Ruby: The Pragmatic Programmers' Guide (http://www.rubycentral.com/book/) – Agile Web Development with Rails – Rails Recipes – Advanced Rails Recipes