BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Ruby - Modules 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:




Double quote vs single quote

We may want to prefer double quote("") to single quote(''). In the example below demonstrates when we have a variable within the quote, we should use double quote:

#!/usr/bin/ruby
# quote.rb
# How to quote

puts "Adding two numbers (4+5) = #{4+5} \n\n"
puts 'Adding two numbers (4+5) = #{4+5} \n\n'

Output:

$ ./quote.rb
Adding two numbers (4+5) = 9 

Adding two numbers (4+5) = #{4+5} \n\n

As we can see if we use single quote(''), it takes the string as literal, and does not replacing the variables.






Multiline string - EOM

How can we define a long string with several lines?

We'll use EOM to tell Ruby it's a multiline string:

#!/usr/bin/ruby
# multi.rb
# How to multiline string

long_string = <<EOM
This is a really long string
that requires several lines \n\n
EOM

puts long_string

Output:

$ ./multi.rb
This is a really long string 
that requires several lines 






string operations

In the following code, we do all sorts of operation on strings:

#!/usr/bin/ruby
# string.rb
# How to string operations

first_name = "Edwin"
last_name = "Hubble"

# simple string concatenation
full_name = first_name + last_name
puts "initial full_name = #{full_name}"

middle_name = "Powell"
# concatenate
full_name = "#{first_name} #{middle_name} #{last_name}"
puts "full_name with middle_name = #{full_name}"

# substring check
puts full_name.include?("Powell")

# string size
puts full_name.size

# counting the number of vowels and consonants
puts "Vowels : " + full_name.count("aeiou").to_s
puts "Consonants : " + full_name.count("^aeiou").to_s

# check start
puts full_name.start_with?("Hubble")

# index of a substring
puts "Index : " + full_name.index("Hubble").to_s

Output:

$ ./string.rb
initial full_name = EdwinHubble
full_name with middle_name = Edwin Powell Hubble
true
19
Vowels : 5
Consonants : 14
false
Index : 13




string comparison with .equal?

,equal? does object comparison. So, if same object it returns true.

#!/usr/bin/ruby
# comp.rb
# How to comparison operation with .equal?

# different objects
puts "\"a\".equal?(\"a\") : " + ("a".equal?"a").to_s

# same object
a = "a"
puts "a.equal?a : " + (a.equal?a).to_s

Output:

$ ./comp.rb
"a".equal?("a") : false
a.equal?a : true




Case conversion

We can control the cases:

#!/usr/bin/ruby
# case.rb
# How to case operations

full_name = "My full name"

puts full_name.upcase
puts full_name.downcase
puts full_name.swapcase

Output:

$ ./case.rb
MY FULL NAME
my full name
mY FULL NAME




Space strip

Ruby has strip function for spaces: lstrip, rstrip, and strip.

#!/usr/bin/ruby
# strip.rb
# How to strip

name = "  myname"
puts "name.size = #{name.size}"
puts "name = #{name}"
puts
puts "name.lstrip = #{name.strip}"
puts "name.lstrip.size = #{name.lstrip.size}"
puts "name.strip = #{name.strip}"
puts "strip name.strip.size = #{name.strip.size}"
puts

name = "myname  "
puts "name = #{name}"
puts "name.size = #{name.size}"
puts "name.rstrip.size = #{name.rstrip.size}"

Output:

$ ./strip.rb
name.size = 8
name =   myname

name.lstrip = myname
name.lstrip.size = 6
name.strip = myname
strip name.strip.size = 6

name = myname  
name.size = 8
name.rstrip.size = 6




Justification - rjust, ljust, center
#!/usr/bin/ruby
# just.rb
# How to just

name = "Edwin Powell Hubble"

puts name.rjust(40,'.')
puts name.ljust(40,'.')
puts name.center(40,'.')

Output:

$ ./just.rb
.....................Edwin Powell Hubble
Edwin Powell Hubble.....................
..........Edwin Powell Hubble...........




chop & chomp
#!/usr/bin/ruby
# chop.rb
# How to chop/chomp

name = "Edwin Powell Hubble"

# cut off the last chaaracter
puts name.chop

# cut off the given string
puts name.chomp('bble')
puts name.chomp('Hubble')

Output:

$ ./chop.rb
Edwin Powell Hubbl
Edwin Powell Hu
Edwin Powell 




split

In the example below, we'll split a string in two ways: 1. every character 2. space.

#!/usr/bin/ruby
# split.rb
# How to split

name = "Edwin Powell Hubble"

# split every character
array = name.split(//)
puts array

# split with a space as a delimiter
array = name.split(/ /)
puts array

Output:

$ ./split.rb
E
d
w
i
n
 
P
o
w
e
l
l
 
H
u
b
b
l
e
Edwin
Powell
Hubble












Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master