Technical Creativity Technical Creativity

String, Array and Hash, the Building Blocks of Ruby Programming

Let’s get down to brass tacks of programming with ruby. If you don’t have your development environment setup, go ahead and use Ruby development environment setup Mac OSX to take care of that. Otherwise, watch the above video grab the source code and get started !!

String

# String

order = 'wings'
puts order

other_order = "fries"
puts other_order

combo_order_1 = order + other_order 
puts combo_order_1

combo_order_2 = "#{order} and #{other_order}"  
puts combo_order_2

combo_order_3 = combo_order_2.sub(/fries/, 'waffles with syrup')
puts combo_order_3

excessive_order = 'wings with fries and more fries and even more fries'
puts excessive_order

better_order = excessive_order.sub(/fries/, 'carrots')
puts better_order

best_order = better_order.gsub(/fries/, 'carrots')
puts best_order

def describe_order(order)
  if order == 'carrots'
    return(order + ' are the best')
  else
    return(order + ' are the worst')
  end
end

some_order = 'hamburger'
some_other_order = 'carrots'

puts describe_order(some_order)
puts describe_order(some_other_order)

first_letter = some_order[0]
puts first_letter
last_letter = some_order[-1]
puts last_letter


healthy_order = 'kale chips with vegan brownies'
iterations = healthy_order.length
puts iterations

iterations.times do |integer|
  puts integer
  puts healthy_order[integer]
end

iterations.times do |integer|
  puts integer
  puts healthy_order[healthy_order.length - integer -1]
end

Array

things = [ 'john', 'jane', :happy, nil, false, {}, [] ]
p things
p things.class
p things.length

p things[3]
p things[-1]
p things[0]

iterations = things.length

iterations.times do |integer|
  p things[integer]
end

iterations.times do |integer|
  index_position = things.length - integer - 1
  p things[index_position]
end

people = ['jane', 'john', 'jim', 'jenny']

people.each do |person|
  p "this person's name #{person}"
end

other_people = people.each do |person|
  p person.reverse
end

p other_people
p people == other_people

scrambled_people = people.map do |person|
  person.reverse
end

p scrambled_people
p people == scrambled_people

def create_funny_sentence(people)
  people.inject('') do |sentence, person|
    sentence << " look at #{person} go,"
    sentence
  end
end

p people

p people.pop
p people
p people.push('jonathon')
p people
p people.shift()
p people
p people.unshift('jennifer')
p people

Hash

person = { name: 'happy person', eye_color: 'blue', address: '500 fell st, st', happy: true, sad: :maybe}

p person
p person[:eye_color]
p person[:happy]


item = { name: 'thing1', location: :drawer, siblings: ['thing2', 'thing3'], parent: { name: 'thing parent'}}

p item
p item[:name]
p item[:parent]


car = { 'name' => 'chevy', 'motor_type' => :combustion, 'location' => [ 'somewhere in california'] }

p car['name']
p car['motor_type']

car.each do |key, value|
  p "This is the car's #{key} and it is #{value}."
end
Talk

A Dream Team

We have traveled many miles.

We will help you build, scale, grow.