Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,72 @@ Each Ruby command will return a value. Sometimes, these values are not what we e
Try the following Commands:

string = "Hello World"
=> fill_in_what_irb_returns_here
=> "Hello World"

string.reverse
=> fill_in_what_irb_returns_here
=> "dlroW olleH"

string
=> fill_in_what_irb_returns_here
=> "Hello World"

array = [3,1,2]
=> fill_in_what_irb_returns_here
=> [3, 1, 2]

array.sort
=> fill_in_what_irb_returns_here
=> [1, 2, 3]

array
=> fill_in_what_irb_returns_here
=> [3, 1, 2]

array.sort!
=> fill_in_what_irb_returns_here
=> [1, 2, 3]

array
=> fill_in_what_irb_returns_here
=> [1, 2, 3]

=> What do you think the exclamation mark indicates?
Force save the sorted array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and one of the cooler things about this is that you can expand that analogy to many other functions. ! at the end of a method generally is used to indicate that the action is destructive (a.k.a. it changes) the original object.

array.map! would actually change array, whereas array.map would not.


puts "Hello Ruby"
=> fill_in_what_irb_returns_here
=> nil

array = [1,2,3,4]
=> fill_in_what_irb_returns_here
=> [1, 2, 3, 4]

array.each do |e|
e**2
end
=> fill_in_what_irb_returns_here
=> [1, 2, 3, 4]

array.each { |e| e**2 }
=> fill_in_what_irb_returns_here
=> [1, 2, 3, 4]

array
=> fill_in_what_irb_returns_here
=> [1, 2, 3, 4]

array.map do |e|
e**2
end
=> fill_in_what_irb_returns_here
=> [1, 4, 9, 16]

array
=> fill_in_what_irb_returns_here
=> [1, 2, 3, 4]

array = [1,2,3]

array.inject(0) do |sum, e|
sum += e
sum
end
=> fill_in_what_irb_returns_here
=> 6

array
=> fill_in_what_irb_returns_here
=> [1, 2, 3]

array.reject do |e|
e > 2
end
=> fill_in_what_irb_returns_here
=> [1, 2]

array
=> fill_in_what_irb_returns_here
=> [1, 2, 3]
Empty file added ruby
Empty file.