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 Input/Output, Objects, Load 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:




Input (gets) / output (puts)

Let's learn how to get some input from a user and how to print it:

# r1.rb
# Reading input & printing output

print "Enter a number:"
num1 = gets.to_i
print "Enter another number:"
num2 = gets.to_i
puts num1.to_s + " + " + num2.to_s + " = " + (num1+num2).to_s

Output:

$ ruby r1.rb
Enter a number:10
Enter another number:20
10 + 20 = 30

Note that we used to_i and to_s for get method to convert a string to int or integer to string.

We can run the script as we do with shell script.

#!/usr/bin/ruby
# r1.rb
...

Then:

$ chmod +x r1.rb

$ ./r1.rb





5 arithmetic operators
#!/usr/bin/ruby
# r2.rb
# 5 Arithmetic operators

puts "7 + 3 = " + (7+3).to_s
puts "7 - 3 = " + (7-3).to_s
puts "7 * 3 = " + (7*3).to_s
puts "7 / 3 = " + (7/3).to_s
puts "7 % 3 = " + (7%3).to_s

Output:

$ ./r2.rb
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2
7 % 3 = 1




float
#!/usr/bin/ruby
# r3.rb
# floating point numbers

num_one = 1.000
num_0999 = 0.999

puts "num_one - num_0999 = " + (num_one-num_0999).to_s

Output:

$ ./r3.rb
num_one - num_0999 = 0.0010000000000000009




Object

Basically, everything in Ruby is an object.

#!/usr/bin/ruby
# r4.rb
# Objects

puts 1.class
puts 1.0123.class
puts "A string".class

Output:

./r4.rb
Fixnum
Float
String




File read/write

We can create a file like this:

#!/usr/bin/ruby
# r6.rb
# File write

f = File.new("myfile.out","w")
f.puts("Test")
f.close

Output:

$ cat myfile.out 
Test

We can read from the file we created:

#!/usr/bin/ruby
# r7.rb
# File write / read

f = File.new("myfile.out","w")
f.puts("Test")
f.close

data = File.read("myfile.out")
puts "reading from a file data = " + data

Output:

$ ./r7.rb
reading from a file data = Test




Loading a Ruby file

We can load a Ruby file:

$ cat my_ruby.rb 
puts "Testing loading a 'rb' file"

Here is our code:

#!/usr/bin/ruby
# r8.rb
# loading 'rb' file

load "my_ruby.rb"

Output:

$ ./r8.rb
Testing loading a 'rb' file






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