Created
July 6, 2017 02:21
-
-
Save forresty/b2f98ace60f36dc5b52212564e7c77de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Bieber', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham' | |
| ] | |
| # def print_musicians(musicians) | |
| # musicians.each do |musician| | |
| # puts musician | |
| # return if musician == 'Bieber' | |
| # end | |
| # end | |
| # print_musicians(musicians) | |
| # exit | |
| num_j_musicians = musicians.count do |musician| | |
| # puts musician[0] | |
| musician[0] == 'J' | |
| end | |
| # block = proc do |musician| | |
| # musician[0] == 'J' | |
| # end | |
| # num_j_musicians = musicians.count(&block) | |
| # if nil | |
| # this_will_not_run | |
| # end | |
| # puts num_j_musicians | |
| # def hello(message) | |
| # end | |
| # p hello |
This file contains hidden or 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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Robert Plant', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham'] | |
| # musicians.each do |musician| | |
| # index = musicians.index(musician) | |
| # puts "#{index + 1} - #{musician}" | |
| # end | |
| # (0...musicians.size).each do |index| | |
| # puts "#{index + 1} - #{musicians[index]}" | |
| # end | |
| musicians.each_with_index do |musician, index| | |
| puts "#{index + 1} - #{musician}" | |
| end |
This file contains hidden or 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
| def greet(first_name, last_name) | |
| full_name = "#{first_name.capitalize} #{last_name.upcase}" | |
| return "Hello, #{full_name}" | |
| end | |
| # puts greet('john', 'lennon') # "Hello, John LENNON" | |
| def greet2(first_name, last_name) | |
| full_name = "#{first_name.capitalize} #{last_name.upcase}" | |
| greeting_message = yield(full_name) | |
| return greeting_message | |
| end | |
| greeting = greet2('forrest', 'ye') do |full_name| | |
| "你好 #{full_name}" | |
| end | |
| puts greeting | |
| greeting = greet2('allen', 'sanchez') do |full_name| | |
| "hey #{full_name}" | |
| end | |
| puts greeting |
This file contains hidden or 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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham'] | |
| upcased_musicians = musicians.map do |musician| | |
| musician.upcase | |
| end | |
| # p upcased_musicians | |
| # p musicians | |
| first_names = musicians.map do |musician| | |
| names = musician.split(' ') | |
| # puts "manipulating #{names}" | |
| names.first | |
| end | |
| # first_names = musicians.map { |musician| musician.split(' ').first } | |
| p first_names | |
| # def hello | |
| # world = 'haha' | |
| # end | |
| # hello | |
| # puts world | |
This file contains hidden or 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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Robert Plant', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham' | |
| ] | |
| for index in (0...musicians.size) | |
| puts "#{index + 1} - #{musicians[index]}" | |
| end | |
| # for musician in musicians | |
| # puts musician | |
| # end |
This file contains hidden or 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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Bieber', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham' | |
| ] | |
| j_musicians = musicians.select do |musician| | |
| # rand > 0.5 | |
| musician[0] == 'J' | |
| # true | |
| # false | |
| # nil | |
| end | |
| p j_musicians | |
This file contains hidden or 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
| tag('html') do | |
| tag('body') do | |
| tag('h1') | |
| end | |
| end | |
| # <html><body><h1></h1></body></html> |
This file contains hidden or 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
| def timer | |
| puts "starting timer" | |
| start_time = Time.now | |
| puts 'starting first yield' | |
| yield | |
| puts 'starting second yield' | |
| yield | |
| puts "time elapsed: #{Time.now - start_time}" | |
| puts "done!" | |
| end | |
| # timer() do | |
| # puts "doing something in the block" | |
| # # simulate some heavy work | |
| # sleep(3) | |
| # puts "ending block" | |
| # end | |
| def n_times(n) | |
| (0...n).each do | |
| yield | |
| end | |
| end | |
| n_times(10) do | |
| puts "haha" | |
| end | |
| # 10.times do | |
| # puts 'haha' | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment