Range
Ruby range includes the last index.
str = "helloworld"
#print "hel"
puts str[0..2]
#print 0, 1, 2
(0..2).each do |i|
puts i
end
# range with step (here get 0, 3, 6)
(0..8).step(3) do |i|
puts i
end
String
1. find index of char in a string
i = email.index('@')
2. substring
# the first 3 chars
e1 = email[0..2]
# get the last char in string
lastC = email[-1]
# get single char by index. The following will return "b"
s = "ab"
puts s[1]
3. string length
l = email.length
4. split string by space to array
w = "hello world"
# split will remove leadin and trailing spaces. Also it will split by multiple spaces
#return ["hello", "world"]
arr = w.split
5. string to int; int to string
"5".to_i
5.to_s
6. concatenation
# print "ab"
puts "a" + "b"
7. loop through
"abc".each_char do |c|
puts c
end
8. convert number to different base
255.to_s(36) #=> "73"
255.to_s(16) #=> "ff"
255.to_s(2) #=> "11111111"
9. array of 26 letters
arr = "abcdefghijklmnopqrstuvwxyz".chars()
# find char from begin or from end. return 0 and 3 respectively
"abca".index('a')
"abca".rindex("a")
10. use range
arr = ('a'..'z').to_a
arr.index('b')
11. string to array
column_title = "ABC"
arr = column_title.chars
12. char to Ascii value. get 97
"a".ord
13. get assii values for chars in string (97, 98)
"ab".each_byte do |c|
puts c
end
Array
# pre define array with 10 elements of empty string
arr = Array.new(10, "")
# join array into string
# will return "Hello world"
str = ["Hello", "world"].join(" ")
# shift will remove the first element
arr.shift
#unshift will add element in the begin
arr.unshift("hi")
# reverse array
arr = arr.reverse()
# sort array in house
arr = [4,2,6,1,9]
arr.sort!
# to access last element of arr
arr[-1]
# sort array without change original array
arr2 = arr.sort()
# drop. drop the first n element and return result
arr = [1,2,3,4,5]
# result will be [3,4,5]
result = arr.drop(2)
# check if array has value
n=3
if (arr.include?n)
# delete element by value
n =3
nums2.delete_at(nums2.find_index(n))
#loop through array using each_with_index
nums = [1,2,3,4]
nums.each_with_index do |val, index|
puts val
puts index
end
#loop through array using each
nums.each {|val| puts val }
nums.each do |val|
puts val
end
#loop through array using for .. in
for val in nums
puts val
end
for i in (0..nums.length-1)
puts nums[i]
end
# loop through array reverse with index
(0..n-1).reverse_each do |i|
...
end
# access last element
arr[-1]
# add last and remove from last
arr = []
arr.apend(1)
arr.pop()
#find index. return 1
arr = "abcdefghijklmnopqrstuvwxyz".chars()
puts arr.index('b')
# conconcat array to string
arr.join("")
#remove the first element. e is 1 and arr becomes [2, 3, 4]
arr = [1,2, 3,4]
e = arr.shift
Hash
#create hash
hash1 = {}
#initiate hash with default data. Not set default value to be array
# Otherwise, they are pointing to the same array
hash2 = Hash.new(1)
#assigin value
hash1['a'] = 10
# access hash
count = hash1['a']
# get default value. should print out 1 because we set default to be 1
puts hash2['b']
# check if key exist in hash. should get false
puts hash2.key?('b')
# loop through hash
hash1.each do |key, val|
puts key
puts val
end
# get hash length
puts hash.length()
# empty a hash
hash.clear()
Set
# create a new set
s = Set.new()
# add element to set. also can do: s.add(1)
s<<1
# get length
s.length
Queue
queue = Queue.new
# add element
queue.push(node)
# remove element
node = queue.pop
# size
size = queue.length
Math
# return 9
(17.to_f/2).round
#return 8
17/2
# find min or max
[a, b].max
[a, b].min
# do math pow
26**3
# convert number to any base. Return string
s = n.to_s(b)
Class
Can add an method to a object directly
obj = Object.new
def obj.event
"small event"
end
puts obj.event
Can overriding method in the same class
class Ticket
def event
"can not really specify yet..."
end
def event
"wht define me again"
end
end
ticket = Ticket.new
puts ticket.event
Reopen a class to add more methods
class Ticket
def event
"can not really specify yet..."
end
end
class Ticket
def price
puts "100 yuan"
end
end
ticket = Ticket.new
puts ticket.event
ticket.price
equal sign(=) method name
class Ticket
def price=(price)
@price = price
end
def price
@price
end
end
ticket = Ticket.new
ticket.price=(10)
puts ticket.price
Modules
#load a file that is relative to the file containing the require_relative statement.
require_relative "stacklike"
class Stack
# module name
include Stacklike
end
More..
#skip a loop. ruby version continue
next if ansM.key?(re)
No comments:
Post a Comment