Please visit new wiki for more info and tasks!
- ruby style => OOP everywhere, e.g.
2 + 2
is a callFixnum(2).+(Fixnum(2))
[1,2,3].each {|n| puts n}
- Simple classes (
Fixnum
,String
,Float
) - Kernel (puts, gets, p)
- Simple expressions ("a" + "b", 1 + 5, etc)
- variables (x = 10)
- Loops (
1.upto(5) {|i| puts i}
,5.times do puts "Hey"; end
) - Cast (
1.to_s
,2.to_f
,10.0.to_i
) - Constants and CamelCase
- String literals and string interpolation:
puts "#{x} + #{y} = #{x+y}"
- String substitution
puts "foo bar".sub('foo', 'bar')
- Regular Expressions and
scan
"This is a string".scan(/\w/) {|s| puts s}
- Matching and capturing
- Arrays
- Hashes
- Code blocks
- Symbols
- Methods with blocks, with argument list, hash as last argument
- Time
- Extending a Fixnum with cool methods for Time (
Time.now + 5.hours
) - Fixnum -> Bignum
Examples for methods with blocks:
# simple function that puts "Hello" and invoke a block passed to it
def helloBlock(&b)
puts "Hello"
yield
end
helloBlock { puts "Yay" }
# => Hello
# Yay
# lets store a block and invoke it several times
x = lambda {|a,b| puts a+b }
x.call(1,2)
# => 3
x.call("abc","def")
# => abcdef
def sayHelloWithBlock(name, &b)
5.times do
yield name
end
end
sayHelloWithBlock("Mirsaid") {|name| puts "Hello #{name}!" }
# method with *args
def say(*words)
puts words.join(' ')
end
say "Ruby", "is", "awesome", "!"
# method with hash as last argument
def whatDoYouThinkAbout(hash = {})
if hash[:pronoun].nil?
puts "I think that #{hash[:language]} #{hash[:opinion]}.."
else
puts "I think that #{hash[:language]} #{hash[:pronoun]} #{hash[:opinion]}.."
end
end
whatDoYouThinkAbout :language => "PHP", :opinion => "Sucks"
# => I think that PHP - sucks
whatDoYouThinkAbout :language => "C", :pronoun => "is", :opinion => "prolix"
# => I think that C is prolix
whatDoYouThinkAbout :language => "Ruby", :pronoun => "is", :opinion => "cool"
# => I think that Ruby is cool
x = [1,2,3] + [4,5,6]
x.each { |elem| puts elem.to_s + " is cool!" }
%w{don't you think that ruby is cool?}.each_with_index do |word, index|
if index == 0
print "#{word.capitalize} "
else
print "#{word} "
end
end
- Kernel: http://ruby-doc.org/core-2.2.1/Kernel.html
- String: http://ruby-doc.org/core-2.2.1/String.html
- Reg Exp: http://ruby-doc.org/core-2.2.1/Regexp.html
- RegEx editor: http://rubular.com/
- Wiki: http://en.wikipedia.org/wiki/Regular_expression
- Wiki-ru: https://ru.wikipedia.org/wiki/%D0%A0%D0%B5%D0%B3%D1%83%D0%BB%D1%8F%D1%80%D0%BD%D1%8B%D0%B5_%D0%B2%D1%8B%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F
- About Constructs:
- http://www.tutorialspoint.com/ruby/ruby_if_else.htm
- http://www.tutorialspoint.com/ruby/ruby_loops.htm
- Array: http://ruby-doc.org/core-2.2.1/Array.html
- Hash: http://ruby-doc.org/core-2.2.1/Hash.html
- Time: http://ruby-doc.org/core-2.2.1/Time.html
- Ranage: http://ruby-doc.org/core-2.2.1/Range.html
- Symbol: http://ruby-doc.org/core-2.2.1/Symbol.html
About Blocks: http://www.reactive.io/tips/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
CamelCase: https://ru.wikipedia.org/wiki/CamelCase