Created
December 14, 2013 00:27
-
-
Save fenixbrassi/7954023 to your computer and use it in GitHub Desktop.
TryRuby Challenge
This file contains hidden or 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
Interactive ruby ready. | |
> help | |
> 2 + 6 | |
=> 8 | |
Success! | |
> 4 * 10 | |
=> 40 | |
> 5 - 12 | |
=> -7 | |
> 40 / 4 | |
=> 10 | |
> next | |
> "Jimmy" | |
=> "Jimmy" | |
Success! | |
> "Jimmy".reverse | |
=> "ymmiJ" | |
Success! | |
> "Jimmy".length | |
=> 5 | |
Success! | |
> "Jimmy" * 5 | |
=> "JimmyJimmyJimmyJimmyJimmy" | |
Successfully completed the Challenge! | |
> 40.reverse | |
=> #<NoMethodError: undefined method `reverse' for 40:Fixnum> | |
Oh no! | |
> 40.to_se.reverse | |
=> | |
#<NoMethodError: undefined method `to_se' for 40:Fixnum> | |
> 40.to_s.reverse | |
=> "04" | |
That's better! | |
> [] | |
=> [] | |
Success! | |
> [12, 47 , 35] | |
=> [12, 47, 35] | |
Success! | |
> [12, 47 , 35].max | |
=> 47 | |
Success! | |
> ticket = [12, 47 , 35] | |
=> [12, 47, 35] | |
Success! | |
> ticket | |
=> [12, 47, 35] | |
Success! | |
> next | |
> ticket.sort! | |
=> | |
#<NameError: undefined local variable or method `ticket' for main:Object> | |
> ticket | |
=> | |
#<NameError: undefined local variable or method `ticket' for main:Object> | |
> print poem | |
=> "My toast has flown from my hand | |
And my toast has gone to the moon. | |
But when I saw it on television, | |
Planting our flag on Halley's comet, | |
More still did I want to eat it." | |
Success! | |
> poem['toast'] = 'honeydew' | |
=> "honeydew" | |
Success! | |
> print poem | |
=> "My honeydew has flown from my hand | |
And my toast has gone to the moon. | |
But when I saw it on television, | |
Planting our flag on Halley's comet, | |
More still did I want to eat it." | |
> next | |
> poem.reverse | |
=> ".ti tae ot tnaw I did llits eroM | |
,temoc s'yellaH no galf ruo gnitnalP | |
,noisivelet no ti was I nehw tuB | |
.noom eht ot enog sah tsaot ym dnA | |
dnah ym morf nwolf sah wedyenoh yM" | |
Success! | |
> poem.lines.to_a.reverse | |
=> ["More still did I want to eat it. | |
", "Planting our flag on Halley's comet, | |
", "But when I saw it on television, | |
", "And my toast has gone to the moon. | |
", "My honeydew has flown from my hand | |
"] | |
Success! | |
> print poem.lines.to_a.reverse.join | |
More still did I want to eat it. | |
Planting our flag on Halley's comet, | |
But when I saw it on television, | |
And my honeydew has gone to the moon. | |
My honeydew has flown from my hand | |
Success! | |
> next | |
> poem.include? "my hand" | |
=> | |
Something's gone wrong- that might not have been valid ruby | |
> poem | |
=> "My honeydew has flown from my hand | |
And my toast has gone to the | |
moon. | |
But when I saw it on television, | |
Planting our flag on Halley's | |
comet, | |
More still did I want to eat it. | |
" | |
books is the wrong thing. Do books = {} | |
> poem.lines.to_a.reverse.join | |
=> "More still did I want to eat it. | |
comet, | |
Planting our flag on Halley's | |
But when I saw it on television, | |
moon. | |
And my toast has gone to the | |
My honeydew has flown from my hand | |
" | |
books is the wrong thing. Do books = {} | |
> books = {} | |
=> {} | |
Success! | |
> books["Gravity's Rainbow"] = :splendid | |
=> :splendid | |
Success! | |
> books["Dante's Inferno"] = :quite_good | |
=> :quite_good | |
> books["El futbol"] = :abysmal | |
=> :abysmal | |
> books | |
=> {"Gravity's Rainbow"=>:splendid, "Dante's Inferno"=>:quite_good, "El futbol"=>:abysmal} | |
> books.length | |
=> 3 | |
> books["Gravity's Rainbow"] | |
=> :splendid | |
Success! | |
> books.keys | |
=> ["Gravity's Rainbow", "Dante's Inferno", "El futbol"] | |
> next | |
> raitings = Hash.new(0) | |
=> {} | |
#<NameError: undefined local variable or method `ratings' for main:Object> | |
> ratings = Hash.new(0) | |
=> {} | |
Success! | |
> books.values.each{ |rate| ratings[rate] += 1} | |
=> [:splendid, :quite_good, :abysmal] | |
> raitings | |
=> #<NameError: undefined local variable or method `raitings' for main:Object> | |
> ratings | |
=> {:splendid=>1, :quite_good=>1, :abysmal=>1} | |
> next | |
> 5.times{ print "Odelay!" } | |
=> "Odelay!Odelay!Odelay!Odelay!Odelay!" | |
Success! | |
> next | |
> Dir.entries "/" | |
=> [".", "..", "Home", "Libraries", "MouseHole", "Programs", "Tutorials", "comics.txt"] | |
Success! | |
> print poem | |
=> | |
#<NameError: undefined local variable or method `poem' for main:Object> | |
> print "pre" | |
=> nil | |
Try: Dir["/*.txt"] | |
> Dire["/*.txt"] | |
=> | |
#<NameError: uninitialized constant Dire> | |
> Dir["/*.txt"] | |
=> ["/comics.txt"] | |
Success! | |
> Dir[] | |
=> [] | |
#<NoMethodError: undefined method `strip' for []:Array> | |
> print File.read("/comics.txt") | |
=> "Achewood: http://achewood.com/ | |
Dinosaur Comics: http://qwantz.com/ | |
Perry Bible Fellowship: http://cheston.com/pbf/archive.html | |
Get Your War On: http://mnftiu.cc/ | |
" | |
Success! | |
> FileUtils.cp('/comics.txt','/Home/comics.txt') | |
=> nil | |
Success! | |
> Dir["/Home/*.txt"] | |
=> ["/Home/comics.txt"] | |
> next | |
> File.open("/Home/comics.txt","a") do |f| | |
.. f << "Cat and Girl: http://catandgirl.com/" | |
.. end | |
=> #<File:/Home/comics.txt (closed)> | |
> print File.read("/Home/comics.txt") | |
=> "Achewood: http://achewood.com/ | |
Dinosaur Comics: http://qwantz.com/ | |
Perry Bible Fellowship: http://cheston.com/pbf/archive.html | |
Get Your War On: http://mnftiu.cc/ | |
Cat and Girl: http://catandgirl.com/" | |
> next | |
> File.mtime("/Home/comics.txt") | |
=> 2013-12-13 21:36:44 UTC | |
Success! | |
> File.mtime("/Home/comics.txt").hour | |
=> 21 | |
Success! | |
> next | |
> def load_comics(path) | |
.. comics = {} | |
.. File.foreach(path) do |line| | |
.... name , url = line.split(': ') | |
.... comics[name] = url.strip | |
.... end | |
.. comics | |
.. end | |
=> nil | |
> comics = load_comics('/comics.txt') | |
=> {"Achewood"=>"http://achewood.com/", "Dinosaur Comics"=>"http://qwantz.com/", "Perry Bible Fellowship"=>"http://cheston.com/pbf/archive.html", "Get Your War | |
On"=>"http://mnftiu.cc/"} | |
> next | |
> require 'popup' | |
=> | |
Something's gone wrong- that might not have been valid ruby | |
> required 'popup' | |
=> | |
#<NoMethodError: undefined method `required' for main:Object> | |
> require 'popup' | |
=> true | |
Success! | |
> Dir["/Libraries/*"] | |
call Popup.goto and pass in a url | |
> Popup.goto "http://ruby-lang.org" | |
> Popup.goto "http://www.google.com" | |
> next | |
> Popup.make { | |
.. h1 "My Links" | |
.. Link "Go to Ruby", "http://ruby-lang.com" | |
.. } | |
> next | |
> Popup.make do | |
.. h1 "Things To Do" | |
.. list do | |
.... p "Try out Ruby" | |
.... p "Ride a tiger" | |
.... p "(down River Euphrates)" | |
> end | |
.. end | |
> next | |
> comics = load_comics('/comics.txt') | |
call Popup.make and build some html! | |
> Popup.make do | |
Something's gone wrong- that might not have been valid ruby | |
> h1 "Coomics on the Web" | |
#<NoMethodError: undefined method `h1' for main:Object> | |
> h1 "Comics on the Web" | |
#<NoMethodError: undefined method `h1' for main:Object> | |
> h1 "Comics on the Web" | |
#<NoMethodError: undefined method `h1' for main:Object> | |
> list do | |
.. comics.each do |name, url| | |
.... link name , url | |
.... end | |
.. end | |
> end | |
> next | |
> Hash.new | |
=> {} | |
Success! | |
> 4 * 10 | |
=> 40 | |
Open up a new BlogEntry class, pretty pretty please. | |
> class BlogEntry | |
.. attr_accessor :title , :time , :fulltext , :mood | |
.. end | |
=> nil | |
> next | |
> entry = BlogEntry.new | |
=> #<BlogEntry:0x1815a25e> | |
Success! | |
> entry.title = "Today Mt. Hood Was Stolen!" | |
=> "Today Mt. Hood Was Stolen!" | |
> entr.time = Time.now | |
=> | |
#<NameError: undefined local variable or method `entr' for main:Object> | |
> entry.time = Time.now | |
2013-12-14 00:08:08 +0000 | |
Success! | |
> entry.mood = :sick | |
:sick | |
> entry.fulltext = "I can't" | |
"I can't" | |
> entry | |
#<BlogEntry:0x7fdd5a6c @time=2013-12-14 00:08:45 +0000, @title="Today Mt. Hood Was Stolen!", @fulltext="I can't", @mood=:sick> | |
> next | |
> class BlogEntry | |
.. def initialize( title , mood, fulltext) | |
.... time = time.now | |
.... @title , @mood , @fulltext = title, mood, fulltext | |
.... end | |
.. end | |
nil | |
> BlogEntry.new | |
=> #<ArgumentError: wrong number of arguments calling `initialize` (0 for 3)> | |
> entry2 = BlogEntry.new("I Left my Hoodie on the Mountain!", :confused, "I am never going back to that mountain") | |
=> | |
#<NoMethodError: undefined method `now' for nil:NilClass> | |
> class BloogEntry | |
.. def initialize(title, mood , fulltext) | |
.... @time = Time.now | |
.... @title , @mood , @ulltext = tittle, mood, fulltext | |
.... end | |
.. end | |
nil | |
> class BloogEntry | |
.. def initialize(title, mood , fulltext) | |
.... @time = Time.now | |
.... @title , @mood , @fulltext = tittle, mood, fulltext | |
.... end | |
.. end | |
nil | |
> entry2 = BlogEntry.new("I Left my Hoodie on the Mountain!", :confused, "I am never going back to that mountain") | |
=> #<NoMethodError: undefined method `now' for nil:NilClass> | |
> next | |
> blog = [entry , entry2] | |
=> [#<BlogEntry:0x4e30b8d9 @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!!", @time=2013-12-14 00:17:34 UTC, @title="Today Mt. Hood Was Stolen!", @mood=:sick>, #<BlogEntry:0x60b7d6a9 @fulltext="I am | |
never going back to that mountain and I hope a giraffe steals it.", @time=2013-12-14 00:17:34 UTC, @title="I Left my Hoodie on the Mountain!", @mood=:confused>] | |
Success! | |
> next | |
> blog.sort_by{ |entry| entry.time}.reverse | |
[#<BlogEntry:0x346d61cf @title="I Left my Hoodie on the Mountain!", @time=2013-12-14 00:18:45 +0000, @fulltext="I am\ | |
never going back to that mountain and I hope a giraffe steals it.", @mood=:confused>, #<BlogEntry:0x793077ec @title="Today Mt. Hood Was Stolen!", @time=2013-12-14 | |
00:18:45 +0000, @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!!", | |
@mood=:sick>] | |
Success! | |
> blog.map{"Bruce Willis"} | |
=> ["Bruce Willis", "Bruce Willis"] | |
Success! | |
> entry.mood | |
=> :sick | |
> blog.each do |entry| | |
.. h2 entry.title | |
.. p entry.fulltext | |
.. end | |
LOL the ending of the challenge is very funny! | |
Once you have Ruby installed, you can use Interactive Ruby by running irb on your system's command line utility. | |
You really deserve a double-layer cake with double-double frosting and a guy playing one of those guitars that's a double guitar. I mean you finished, you really did! | |
No doubt about it, you're a certified red-blooded smartiac! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment