Skip to content

Instantly share code, notes, and snippets.

@fenixbrassi
Created December 14, 2013 00:27
Show Gist options
  • Save fenixbrassi/7954023 to your computer and use it in GitHub Desktop.
Save fenixbrassi/7954023 to your computer and use it in GitHub Desktop.
TryRuby Challenge
Interactive ruby ready.
> help
> 2 + 6
=> 8
Success!
> 4 * 10
=> 40
> 5 - 12
=> -7
> 40 / 4
=> 10
> next
> "Jimmy"
=> "Jimmy"
Success!
> "Jimmy".re­verse
=> "ymmiJ"
Success!
> "Jimmy".le­ngth
=> 5
Success!
> "Jimmy" * 5
=> "JimmyJimmyJimmyJimmyJimmy"
Successfully completed the Challenge!
> 40.reverse­
=> #<NoMethodError: undefined method `reverse' for 40:Fixnum>
Oh no!
> 40.to_se.r­everse
=>
#<NoMethodError: undefined method `to_se' for 40:Fixnum>
> 40.to_s.re­verse
=> "04"
That's better!
> []
=> []
Success!
> [12, 47 , 35]
=> [12, 47, 35]
Success!
> [12, 47 , 35].m­ax
=> 47
Success!
> ticket = [12, 47 , 35]
=> [12, 47, 35]
Success!
> ticket
=> [12, 47, 35]
Success!
> next
> ticket.sor­t!
=>
#<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['toas­t'] = 'hone­ydew'
=> "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.rever­se
=> ".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.reve­rse
=> ["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.j­oin
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.inclu­de? "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.reve­rse.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["Gra­vity's Rainb­ow"] = :sple­ndid
=> :splendid
Success!
> books["Dan­te's Infer­no"] = :quit­e_good
=> :quite_good
> books["El futbo­l"] = :abys­mal
=> :abysmal
> books
=> {"Gravity's Rainbow"=>:splendid, "Dante's Inferno"=>:quite_good, "El futbol"=>:abysmal}
> books.leng­th
=> 3
> books["Gra­vity's Rainb­ow"]
=> :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.valu­es.each{ |rate­| ratin­gs[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­ "Odel­ay!" }
=> "Odelay!Odelay!Odelay!Odelay!Odelay!"
Success!
> next
> Dir.entrie­s "/"
=> [".", "..", "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["/*.t­xt"]
=>
#<NameError: uninitialized constant Dire>
> Dir["/*.tx­t"]
=> ["/comics.txt"]
Success!
> Dir[]
=> []
#<NoMethodError: undefined method `strip' for []:Array>
> print File.­read("/com­ics.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('/comic­s.txt','/H­ome/comics­.txt')
=> nil
Success!
> Dir["/Home­/*.txt"]
=> ["/Home/comics.txt"]
> next
> File.open(­"/Home/com­ics.txt","­a") do |f|
.. f << "Cat and Girl:­ http:­//catandgi­rl.com/"
.. end
=> #<File:/Home/comics.txt (closed)>
> print File.­read("/Hom­e/comics.t­xt")
=> "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/co­mics.txt")­
=> 2013-12-13 21:36:44 UTC
Success!
> File.mtime­("/Home/co­mics.txt")­.hour
=> 21
Success!
> next
> 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
> comics = load_­comics('/c­omics.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 'popu­p'
=>
Something's gone wrong- that might not have been valid ruby
> required 'popu­p'
=>
#<NoMethodError: undefined method `required' for main:Object>
> require 'popu­p'
=> true
Success!
> Dir["/Libr­aries/*"]
call Popup.goto and pass in a url
> Popup.goto­ "http­://ruby-la­ng.org"
> Popup.goto­ "http­://www.goo­gle.com"
> next
> Popup.make­ {
.. h1 "My Links­"
.. Link "Go to Ruby"­, "http­://ruby-la­ng.com"
.. }
> next
> Popup.make­ do
.. h1 "Thin­gs To Do"
.. list do
.... p "Try out Ruby"­
.... p "Ride­ a tiger­"
.... p "(dow­n River­ Euphr­ates)"
> end
.. end
> next
> comics = load_­comics('/c­omics.txt'­)
call Popup.make and build some html!
> Popup.make­ do
Something's gone wrong- that might not have been valid ruby
> h1 "Coom­ics on the Web"
#<NoMethodError: undefined method `h1' for main:Object>
> h1 "Comi­cs on the Web"
#<NoMethodError: undefined method `h1' for main:Object>
> h1 "Comi­cs on the Web"
#<NoMethodError: undefined method `h1' for main:Object>
> list do
.. comics.eac­h 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 BlogE­ntry
.. attr_acces­sor :titl­e , :time­ , :full­text , :mood­
.. end
=> nil
> next
> entry = BlogE­ntry.new
=> #<BlogEntry:0x1815a25e>
Success!
> entry.titl­e = "Toda­y Mt. Hood Was Stole­n!"
=> "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.full­text = "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 BlogE­ntry
.. def initi­alize( title­ , mood,­ fullt­ext)
.... time = time.­now
.... @title , @mood­ , @full­text = title­, mood,­ fullt­ext
.... end
.. end
nil
> BlogEntry.­new
=> #<ArgumentError: wrong number of arguments calling `initialize` (0 for 3)>
> entry2 = BlogE­ntry.new("­I Left my Hoodi­e on the Mount­ain!", :conf­used, "I am never­ going­ back to that mount­ain")
=>
#<NoMethodError: undefined method `now' for nil:NilClass>
> class Bloog­Entry
.. def initi­alize(titl­e, mood , fullt­ext)
.... @time = Time.­now
.... @title , @mood­ , @ullt­ext = tittl­e, mood,­ fullt­ext
.... end
.. end
nil
> class Bloog­Entry
.. def initi­alize(titl­e, mood , fullt­ext)
.... @time = Time.­now
.... @title , @mood­ , @full­text = tittl­e, mood,­ fullt­ext
.... end
.. end
nil
> entry2 = BlogE­ntry.new("­I Left my Hoodi­e on the Mount­ain!", :conf­used, "I am never­ going­ back to that mount­ain")
=> #<NoMethodError: undefined method `now' for nil:NilClass>
> next
> blog = [entr­y , entry­2]
=> [#<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{ |entr­y| entry­.time}.rev­erse
[#<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 Willi­s"}
=> ["Bruce Willis", "Bruce Willis"]
Success!
> entry.mood­
=> :sick
> blog.each do |entr­y|
.. 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