Created
December 15, 2012 10:56
-
-
Save karmi/4293767 to your computer and use it in GitHub Desktop.
IRB Snippets for the #railsgirls Prague workshop
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
require 'open-uri' | |
# Otevreme soubor z Project Gutenberg, Jane Austen, "Pride and Prejudice" | |
# | |
file = open('http://www.gutenberg.org/cache/epub/1342/pg1342.txt') | |
# Nacteme soubor jako text | |
# | |
text = file.read | |
# puts text | |
puts text.length # Pocet vsech znaku v textu | |
puts text.delete(' ').length # Pocet znaku bez mezer | |
puts text.split("\n").length # Pocet radek | |
puts text.split(". ").length # Pocet vet (hruby) | |
puts text.split(/\. |\? |! /).length # Presnejsi pocet vet | |
puts text.split(' ').length # Pocet slov | |
puts text.split(' ').uniq.length # Pocet jedinecnych slov | |
puts text.split(' ').uniq.sort # Seznam jedinecnych slov podle abecedy |
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
"banana".reverse | |
[1, 14, 7, 3].max | |
["banana", "lemon", "ananas"].size | |
["banana", "lemon", "ananas"].sort | |
["banana", "lemon", "ananas"].sort.last | |
["banana", "lemon", "ananas"].sort.last.capitalize | |
print ["banana", "lemon", "ananas"].sort.last.capitalize | |
5.times do | |
print "Hello " | |
end | |
1 + 1 | |
"1" + "1" | |
2 + 2 * 5 | |
(2 + 2) * 5 | |
"Ahoj" + "Ruby" | |
"Ahoj" + " " + "Ruby" | |
"Ahoj" + 2 | |
name = "Brno" | |
puts "Vita vas mesto #{name}" | |
# Find largest number | |
input = [3, 6, 9, 1] | |
largest = input.shift | |
input.each do |i| | |
largest = i if i > largest | |
end | |
print "Nejvyssi cislo je: #{largest} \n" | |
# "Shortcut" (Array#max) | |
largest = [3, 6, 9, 1].max | |
print "Nejvyssi cislo je: #{largest} \n" | |
a = [ 'jedna', 'dve', 'tri' ] | |
a[0] | |
book = { :jmeno => "Franz", :prijmeni => "Kafka", :nazev => 'Promena' } | |
book[:nazev] | |
["jedna", "dve", "tri"].each do |slovo| | |
puts "* " + slovo | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment