Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Splat arguments - can receive more than 1 arguments
- def what_up(greeting, *friends)
- friends.each { |friend| puts "#{greeting}, #{friend}!" }
- end
- what_up("What up", "Ian", "Zoe", "Zenas", "Eleanor")
- # Blocks can be defined with either the keywords do and end or with curly braces ({}).
- 1.times do
- puts "I'm a code block!"
- end
- 1.times { puts "As am I!" }
- =begin
- However, the block that we define (following .each) will only be called once, and in the context of the array that we are iterating over.
- Hence it's better to use method than blocks if there is a need of reuse.
- =end
- # method that capitalizes a word
- def capitalize(string)
- puts "#{string[0].upcase}#{string[1..-1]}"
- end
- capitalize("ryan") # prints "Ryan"
- capitalize("jane") # prints "Jane"
- # block that capitalizes each string in the array
- ["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement