In this post I will show you how to make a ruby project.
Go to a coding platform. Make a new folder and call it adventure.rb. Let’s start off with a variable. Enter this into the code
places = [
"airport",
"train station",
"hedge maze",
"road"
]So this is cool but we never use it anywhere. Add this to the code
puts "You are going to the " + places.sample + "."Here we are putting a sample/random item from the array we made, but what if we wanted to use the place we are at in the story line? Add this before the puts:
place = places.sampleThen replace:
puts "You are going to the " + places.sample + "."With:
puts "You are going to the " + place + "."And now we can know which place we go to. Now we can do this:
puts "On the way to the " + place + ", you see a chicken."With this these will put at around the same time. To fix this, between:
puts "You are going to the " + place + "."And:
puts "On the way to the " + place + ", you see a chicken."Add:
sleep(1)Let’s test these out. Go to terminal
cd ~/ //plus the folder adventure.rb is inThen to run it
ruby ./adventure.rbYou should see:
You go to the {insert place here}.
On the way to the {insert place here}, you see a chicken.Obviously the {insert place here} would be: hedge maze, airport, train station or road. One thing though… on the way to an airport you would probably not see a chicken. So lets make some variables up at the top:
airport = {
}
train_station = {
}
hedge_maze = {
}
road = {
}Add some content:
airport = {
animal: "stray dog"
}
train_station = {
animal: "bird"
}
hedge_maze = {
animal: "cow"
}
road = {
animal: "squirrel"
}Now how would we find the animal based on the place… Functions! Let’s make a findAnimal function:
def find_animal(place, airport, train_station, hedge_maze, road)
if var == "airport"
airport[:animal]
elsif var == "train station"
train_station[:animal]
elsif var == "hedge maze"
hedge_maze[:animal]
elsif var == "road"
road[:animal]
end
endSo this checks what place is and returns the correct animal. Now let’s replace
puts "On the way to the " + place + ", you see a chicken."With:
puts "On the way to the " + place + ", you see a " + find_animal(place, airport, train_station, hedge_maze, road) + "."Checking your code.
We have gotten the start of the adventure made. Let’s make sure we did it right. Is this your code:
places = [
"airport",
"train station",
"hedge maze",
"road"
]
airport = {
animal: "stray dog"
}
train_station = {
animal: "bird"
}
hedge_maze = {
animal: "cow"
}
road = {
animal: "squirrel"
}
def find_animal(place, airport, train_station, hedge_maze, road)
if var == "airport"
airport[:animal]
elsif var == "train station"
train_station[:animal]
elsif var == "hedge maze"
hedge_maze[:animal]
elsif var == "road"
road[:animal]
end
end
place = places.sample
puts "You are going to the " + place + "."
sleep(1)
puts "On the way to the " + place + ", you see a " + find_animal(place, airport, train_station, hedge_maze, road) + "."If not then you can just copy and paste by highlighting the text and pressing ⌘(command) and C then going to your code and pressing ⌘ and V.