learn_ruby_ch2_pt4 Flashcards

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

method naming convention for Boolean returns

A

x = 1.0
y = 1.0
x.eql? y # => true

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

method naming convention for destructive methods

name = “Matz!”

A
name = "Matz!" # => "Matz!"
name.delete( "!" ) # => "Matz"
puts name # => Matz!
name.delete!( "!" ) # => "Matz"
puts name # => Matz
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

method naming convention for setter methods

Name, family_name, given_name

A
class Name
  def family_name=( family )
    @family_name = family
  end
  def given_name=( given )
    @given_name = given
  end
end

n = Name.new
n.family_name= “Matsumoto” # => “Matsumoto”
n.given_name= “Yukihiro” # => “Yukihiro”
p n # => ltName:0x1d441c @family_name=”Matsumoto”, @given_name=”Yukihiro”>

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

default arguments (repeat)

A

def repeat( word=”Hello! “, times=3 )
puts word * times
end

repeat # => Hello! Hello! Hello!

repeat( “Goodbye! “, 5 ) # => Goodbye! Goodbye! Goodbye! Goodbye! Goodbye!

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

variable arguments (num_args)

A

def num_args( *args )
length = args.size
label = length == 1 ? “ argument” : “ arguments”
num = length.to_s + label + “ ( “ + args.inspect + “ )”
num
end

puts num_args

puts num_args(1)

puts num_args( 100, 2.5, “three” )

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