-
Notifications
You must be signed in to change notification settings - Fork 40
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
Conversation
private def process_command(command : String) | ||
command = command.to_s.gsub(/\b_\b/) { last_value.to_s.strip } |
There was a problem hiding this comment.
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 }
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 _
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this 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 👍
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 ;) |
Valid chunks of Crystal with 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 |
I like that 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 |
I think |
Inspired by https://github.com/pry/pry/wiki/Special-Locals#Last_result
Super simple (5 LOCs) but very useful 🔨