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

Last result local '_' #60

Closed
wants to merge 1 commit into from
Closed

Last result local '_' #60

wants to merge 1 commit into from

Conversation

veelenga
Copy link
Member

@veelenga veelenga commented Sep 14, 2017

Inspired by https://github.com/pry/pry/wiki/Special-Locals#Last_result

Super simple (5 LOCs) but very useful 🔨

icr(0.23.1) > a = 42
 => 42
icr(0.23.1) > _
 => 42
icr(0.23.1) > _ + 1
 => 43
icr(0.23.1) > 1 + _
 => 44
icr(0.23.1) > _.to_s
 => "44"
icr(0.23.1) > _.to_i
 => 44
icr(0.23.1) > inc = -> { _ + 1 }
 => #<Proc(Int32):0x1002ea350>
icr(0.23.1) > inc.call
 => 45
...

private def process_command(command : String)
command = command.to_s.gsub(/\b_\b/) { last_value.to_s.strip }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we can use an underscore for ignoring block values, I think this would still match something like x.each { |_| nil } which would be

"x.each { |_| nil }".to_s.gsub(/\b_\b) { "42"} #=> x.each { |42| nil }

Maybe if the whole command was just _ so command.to_s.gsub(/^\b_\b$/) { last_value.to_s.strip }?

Copy link
Member Author

Choose a reason for hiding this comment

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

@jwoertink yes, also _ can be used in such way:

a, _ = [1, 2]
a #=> 1

Making it just a "whole command" does not make sense for me, because the general purpose of _ is the following:

icr(0.23.1) > User.last
...
icr(0.23.1) > user = _
icr(0.23.1) > user.firstname

Will try to fix those issues. Thanks for a comment

Copy link
Collaborator

Choose a reason for hiding this comment

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

So I had an epiphany while I was sleeping. You're right that it can't be a whole command, so my regex won't work. However, I think a regex is the wrong way to go anyway. I feel like it should just be a macro! That's basically what it is anyway. That way if _ is used in a block, or method argument it's not caught.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jwoertink did you mean Crystal's macro? Unfortunately, a compiler does not allow methods/macros named _.

Copy link
Collaborator

Choose a reason for hiding this comment

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

aww lame. That would have been the perfect use. Oh well.

end
end

it "is not interpreted as last value if is a part of var name or literal" do
Copy link
Member

Choose a reason for hiding this comment

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

as last -> as the last

Copy link
Member

@greyblake greyblake left a comment

Choose a reason for hiding this comment

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

Despite, the Crystal itself does not support _ as far as I can see, the changes look OK to me 👍

@veelenga
Copy link
Member Author

Well, seems like it requires much more effort than this naive implementation. Currently, it could bring more problems than benefits. If someone has ideas or wants to try to implement it, i am ready to help ;)

@veelenga veelenga closed this Sep 17, 2017
@veelenga
Copy link
Member Author

veelenga commented Sep 17, 2017

Valid chunks of Crystal with _ that will produce an unexpected result with current implementation:

a, _ = [1, 2]

def foo(x : _)
end

def foo(x : _, _ -> Int32)
end

def some_proc(&block : Int32 -> _)
  block
end

[1,2,3].each { |_| puts "yo!" }
{"k" => "v"}.each { |k, _| puts k }

"super _ b"

Maybe we can use some other symbol(s) rather than _. For example, !! (just like a similar bash command)...

@jwoertink
Copy link
Collaborator

I like that !! lines up with bash, but maybe still an issue because of doing a = 1; !!a. Though, doing a regex for !! would be a lot easier since it's probably only used in a string or at the beginning of some value.

I do agree; however, that something other than _ could be used. It's nice to work like IRB, but I don't think we need to be 1 <-> 1 match with it. Especially since there's that paste command that was added. What about __? 2 underscores. I can't think of any place that would be used.

@veelenga veelenga changed the title Last result local '_' Last result local '__' Oct 2, 2017
@veelenga
Copy link
Member Author

veelenga commented Oct 2, 2017

I think __ is a good idea... refs #63

@veelenga veelenga changed the title Last result local '__' Last result local '_' Oct 2, 2017
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

Successfully merging this pull request may close these issues.

3 participants