Skip to content

Instantly share code, notes, and snippets.

@plexus
Created May 27, 2013 20:02
Show Gist options
  • Save plexus/5658832 to your computer and use it in GitHub Desktop.
Save plexus/5658832 to your computer and use it in GitHub Desktop.
# Summary Ruby introduction
puts "hello there!"
5+5
"this is a string"
'this is also string'
7
puts 'this is also string'
puts 7
puts "bla" + ' hello'
7+7
'foo'+'bar'
puts "bla" + 'hello'
'bla'
'hello'
'bla' + 'hello'
7*5
'bla'*'hello'
5
5
5+4
"april is the cruelest month"
"big"+'pizza'
puts 'hello folks'
'hello folks'
3.times { puts 'hello folks' }
5+5
'cookie'*7
a=1
b=2
a
b
a=3
a
puts 'hey'
a=puts 'hey'
a='hey'
puts a
a=nil
a='hello'
b='hello'.upcase
puts('hello'*8)
puts('hello'.upcase*8)
c=4*3
'hello'.upcase
puts('hello')
c='hello'
c.class
7.class
upcase
7.upcase
[]
list_of_things=['cow', 'chicken', 'tuna', 'beetroot']
list_of_things
[7, 8, 9]
list_of_things.class
list_of_things.length
4thing=['a','b','c'] # not good, variable name can't start with a number
th4ing=['a','b','c']
thing4=['a','b','c']
list=list_of_things
list.length
list.reverse
list.first
list.last
list[0]
list.second
require 'active_support/all' # this gives you the magic .third, .fourth
list.second
list.third
list[0]
list[1]
list[2]
list[3]
list[4]
list.select.with_index{|element, idx| idx%2==1} #since you really wanted to know
################################################################################
x = [ "apple pie", "chicken pie",
"strawberry tart", "chocolate chip cookies",
"chocolate cake"]
################################################################################
x.each {|food, i| puts "Man, that's some good #{food} number #{i}" }
x.each_with_index {|food, i|
puts "Man, that's some good #{food} number #{i}"
}
################################################################################
x.select {|food| food =~ /pie/}
x.select.with_index {|food, i| i%2 == 1}
if x.include?('apple pie')
puts 'it does!'
end
if x[3] =~ /chocolate/
puts "it is truthy"
else
puts "it is falsey"
end
i=0
until i == x.length
food = x[i]
puts "Man, that's just awesome #{food}"
x+=1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment