Created
October 27, 2013 21:49
-
-
Save vaichidrewar/7188278 to your computer and use it in GitHub Desktop.
Ruby String Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[12, 47, 35].max | |
ticket.sort! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = BlogEntry.new | |
=> #<BlogEntry:0x4efb0b1f> | |
Success! | |
> entry.title = "Today Mt. Hood Was Stolen!" | |
=> "Today Mt. Hood Was Stolen!" | |
> entry.time = Time.now | |
2013-10-27 22:24:25 +0000 | |
Success! | |
> entry.mood = :sick | |
:sick | |
> entry.fulltext = "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!!" | |
"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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dir.entries "/" | |
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["/*.txt"] | |
=> ["/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/comics.txt", "a") do |f| | |
.. f << "Cat and Girl: http://catandgirl.com/" | |
.. end | |
=> #<File:/Home/comics.txt (closed)> | |
When was the last change made to the file | |
File.mtime("/Home/comics.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(path) | |
.. comics = {} | |
.. File.foreach(path) do |line| | |
.... name,url = line.split(': ') | |
.... comics[name] = url.strip | |
.... 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. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
books.keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Jimmy".reverse | |
"Jimmy".length | |
"Jimmy" * 5 | |
=> "JimmyJimmyJimmyJimmyJimmy" | |
40.to_s.reverse | |
poem.include? "my hand" | |
name,url = line.split(': ') | |
comics[name] = url.strip | |
#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