Skip to content

Instantly share code, notes, and snippets.

@vaichidrewar
Created October 27, 2013 21:49
Show Gist options
  • Save vaichidrewar/7188278 to your computer and use it in GitHub Desktop.
Save vaichidrewar/7188278 to your computer and use it in GitHub Desktop.
Ruby String Operations
[12, 47, 35].m­ax
ticket.sor­t!
You've opened up a new BlogEntry class. What is your blog entry made of? A title, sure. Also, a time when the entry was posted. The full text of the entry.
We'll do a mood setting, too, just like LiveJournal. The Internet has really brought back stick people and smileys out of bankruptcy. Emote!
Okay, so you've got the first line of the class, here's the rest:
class BlogEntry
attr_accessor :title, :time, :fulltext, :mood
end
When you want to move on to the next lesson, type next
entry = BlogE­ntry.new
=> #<BlogEntry:0x4efb0b1f>
Success!
> entry.titl­e = "Toda­y Mt. Hood Was Stole­n!"
=> "Today Mt. Hood Was Stolen!"
> entry.time­ = Time.­now
2013-10-27 22:24:25 +0000
Success!
> entry.mood­ = :sick­
:sick
> entry.full­text = "I can't­ belie­ve Mt. Hood was stole­n! I am speec­hless! It was stole­n by a giraf­fe who drove­ away in his Cadil­lac Sevil­le very nonch­alant!!"
"I can't believe Mt. Hood was stolen! I am speechless! It was stolen by a giraffe who drove away in his Cadillac Seville very nonchalant!!"
Cool, your blog is awesome. Hey, let's make things a bit easier on you. You're not going to want to set the time like that every time you post. You just want to type in the title and the entry and the mood quickly, right?
Let's add an initialize method.
class BlogEntry
def initialize( title, mood, fulltext )
@time = Time.now
@title, @mood, @fulltext = title, mood, fulltext
end
end
Dir.entrie­s "/"
You've just listed out everything in the top directory. The root directory, indicated by a single slash.
So, what is the Dir.entries method? Well, it's just a method, right? entries is a method called on the Dir variable. And Dir has a collection of methods for checking out file directories.
Dir["/*.tx­t"]
=> ["/comics.txt"]
The Dir[] method is like entries but you search for files with wildcard characters. Notice how they still mean, "I am looking for _____?"
More specifically: "I am looking for files which end with .txt."
Let's crack open this comics file, then. Here's the way:
print File.read("/comics.txt")
Let's make a copy of the comics file You\'ll want to:
FileUtils.cp('/comics.txt', '/Home/comics.txt')
Opening file in append mode and writing to it
File.open(­"/Home/com­ics.txt", "a") do |f|
.. f << "Cat and Girl:­ http:­//catandgi­rl.com/"
.. end
=> #<File:/Home/comics.txt (closed)>
When was the last change made to the file
File.mtime­("/Home/co­mics.txt")­
=> 2013-10-27 22:07:38 +0000
If you want to check just what hour it was, hit the up arrow key and change the line to:
File.mtime("/Home/comics.txt").hour
> def load_­comics(pat­h)
.. comics = {}
.. File.forea­ch(path) do |line­|
.... name,url = line.­split(': ')
.... comics[nam­e] = url.s­trip
.... end
.. comics
.. end
=> nil
File.foreach is a method which opens a file and hands each line to the block. The line variable inside the do...end block took turns with each line in the file.
books.keys
"Jimmy".re­verse
"Jimmy".length
"Jimmy" * 5
=> "JimmyJimmyJimmyJimmyJimmy"
40.to_s.reverse
poem.include? "my hand"
name,url = line.­split(': ')
comics[nam­e] = url.s­trip
#split is a method for strings, which breaks the string up into an array. An axe is laid on the colon and the line is chopped in half, giving us the url and name for each comic.
#strip removes extra spaces around the url. Just in case.
to_s converts things to strings.
to_i converts things to integers (numbers.)
to_a converts things to arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment