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

Session 1 Notes: 19-nil.rb maybe explain | and {}? #18

Open
paoyong opened this issue Jun 11, 2015 · 2 comments
Open

Session 1 Notes: 19-nil.rb maybe explain | and {}? #18

paoyong opened this issue Jun 11, 2015 · 2 comments

Comments

@paoyong
Copy link

paoyong commented Jun 11, 2015

Going through the tutorial right now, I am confused as to what the {...} and the |number|mean in these two lines:

array.find { |number| number * 2 == 10 } # => 5
array.find { |number| number * 2 == 11 } # => nil

Could not find an explanation in earlier notes so I had to look them up. Figured it out in the end on my own, but it would be nice if a note covered this.

@JoshCheek
Copy link
Owner

Hi, Keith. I don't see a { ... } in the code samples you provided. Scanning the code, it looks like it's from session3. In that case, I meant for the ellipsis to imply "some bit of code that isn't worth paying attention to". In other words, we're interested in the fact that some code is within curly braces, but not interested in the code itself. I'm not sure what a good alternative to this is. If you can think of something that would make sense to most beginners, I'll update it.

The |number| would mean that when the code in the block is executed, it will receive some object, and create a local variable that it can use to access the object. In our case, it's trying to find the number within the array [1,5,7] that is equal to 10 (and later 11) when multiplied by 2. I suppose I could illustrate nil without blocks invoking a method that has no body.

Going to leave this open so that when I have a bit more time, I come back to it and decide if I should do this.

@paoyong
Copy link
Author

paoyong commented Jun 19, 2015

Josh, thanks for looking into it! I'm sorry I completely forgot to mention what section it was: session 1 notes: 19-nil.rb

It would be nice if there was an explanation on lines 6-7 because the pipe symbols felt like new syntax (I'm a javascript programmer by default) and I couldn't find any previous notes that cover the pipe symbols. For example, if I were to rewrite it, I would say something roughly along these lines:

array = [1,5,7]

# For each number in array, reference that number in |number|
array.find { |number| number * 2 == 10 } # => 5
array.find { |number| number * 2 == 11 } # => nil

Again, I'm no expert in Ruby, I'm sure you can explain it much better than I did. I just felt that the pipe symbols lacked an introduction, which was a problem for me as I am new to the Ruby syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants