learn_ruby_ch2_pt5 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

User alias (greet, ahello)

A

def greet
puts “hello”
end

alias ahello greet

ahello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A block to iterate through pacific

A

A block in Ruby is more than just a code block or group of statements

pacific = [ “Washington”, “Oregon”, “California” ]
pacific.each do |element|
puts element
end

or

pacific.each { |e| puts e }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use yield to run a block of code (gimme)

A
def gimme
  if block_given?
    yield
  else
    puts "I'm blockless!"
  end
end

gimme { print “Say hi.” } # => Say hi.

gimme # => I’m blockless!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Use proc and lambda

count, your_proc, my_proc

A

!/usr/bin/env ruby

count = Proc.new { [1,2,3,4,5].each do |i| print i end; puts }
your_proc = lambda { puts "Lurch: 'You rang?'" }
my_proc = proc { puts "Morticia: 'Who was at the door, Lurch?'" }
# What kind of objects did you just create?
puts count.class, your_proc.class, my_proc.class

Calling all procs
count.call
your_proc.call
my_proc.call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

create a symbol

name = “Matz”

A

symbols are like placeholders for identifiers and strings

name = “Matz”
name.to_sym # => :Matz
:Matz.id2name # => “Matz”
name == :Matz.id2name # => true

Symbol.all_symbols # about 1000 symbols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Get info on print in the ruby kernel

A

ri Kernel.print

requires ruby-doc.noarch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly