Ruby By Examples

From Wikibooks, the open-content textbooks collection

(Redirected from Ruby by examples)
Jump to: navigation, search

The book is broken down into sections. Each section deals with a Ruby language theme, giving examples, from basic to advanced.

Contents

[edit] Basics

[edit] Strings

Example-001

puts 'That is an object'.size

[edit] Numbers

Example-001

5.times do
   puts 'Loop'
end

[edit] Arrays

Example-001

arr = [['0','0','0'],['0','$','0'],['0','0','0']]
puts arr[1][1]

[edit] Hashes


books = {'978-0140449266' => 'The Count of Monte Cristo',
         '978-0140439243' => 'The Man in the Iron Mask'
}
 
puts books['978-0140439243']
# The output is 'The Man In The Iron Mask'
 
more_books = {}
more_books['978-1593081485'] = 'The Three Musketeers'

[edit] Operators

[edit]  ::

Example-001

MR_COUNT = 0        # constant defined on main Object class
module Foo
  MR_COUNT = 0
  ::MR_COUNT = 1    # set global count to 1
  MR_COUNT = 2      # set local count to 2
end
puts MR_COUNT       # this is the global constant
puts Foo::MR_COUNT  # this is the local "Foo" constant

Example-002

CONST = ' out there'
class Inside_one
   CONST = proc {' in there'}
   def where_is_my_CONST
      ::CONST + ' inside one'
   end
end
class Inside_two
   CONST = ' inside two'
   def where_is_my_CONST
      CONST
   end
end
puts Inside_one.new.where_is_my_CONST
puts Inside_two.new.where_is_my_CONST
puts Object::CONST + Inside_two::CONST
puts Inside_two::CONST + CONST
puts Inside_one::CONST
puts Inside_one::CONST.call + Inside_two::CONST

[edit] [ ]


[edit] **


[edit] + - ! ~


[edit] * / %


[edit] << >>


[edit] &


[edit] | ^


[edit] > >= < <=


[edit] <=> == === != =~


[edit] &&


[edit] .. ...


[edit] = ( += -= )


[edit] not


[edit] and or


[edit] Intermediate

[edit] Modules and Procedures


[edit] Classes and Methods


[edit] Names, Variables, Constants and Symbols


[edit] Advanced

[edit] Active record


[edit] Sockets

Example-001

# daytimeserver.rb
puts'========================================='
require 'socket'
puts'== daytime server =='
# Open a socket to a local daytime server, read, and print.
print TCPSocket.open('129.6.15.28', 'daytime').read
puts'========================================='

Example-002

# socketmethods.rb
require 'socket'
mySocket = TCPSocket.new( 'www.wikibook.com', 80 )
puts'==================================='
# Returns information on this connection's peer socket as a struct 
# sockaddr packed into a string.
strinfo = mySocket.getpeername 
puts 'strinfo: '+strinfo
# Returns true if query returns numeric address not host name.
puts BasicSocket::do_not_reverse_lookup
puts'==================================='
# Returns information on s as a struct sockaddr packed into a string.
strinfo = mySocket.getsockname 
puts 'strinfo: '+strinfo
puts'==================================='
# Gets the specified socket option.
optval = mySocket.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
puts 'optval: '+optval.inspect
optval = optval.unpack "i"
puts 'optval: '+optval.inspect
optval = mySocket.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER)
puts 'optval: '+optval.inspect
optval = optval.unpack "ii"
puts 'optval: '+optval.inspect
# Sets reverse_lookup status
BasicSocket::do_not_reverse_lookup=true
# Sets the specified socket option.
mySocket.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
optval = mySocket.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
puts 'optval: '+optval.inspect
optval = optval.unpack "i"
puts 'optval: '+optval.inspect
puts'==================================='
# 
mySocket.shutdown(2)  # 0 shuts down receiving, 1 sending, and 2 both.
puts 'mySocket: '+mySocket.inspect
puts'==================================='
# Returns true if query returns numeric address not host name.
puts BasicSocket::do_not_reverse_lookup
puts'==================================='