crystal-community / icr

Interactive console for Crystal programming language
MIT License
505 stars 40 forks source link

Preserver previous command results (avoid rerunning commands) #128

Open owen2345 opened 3 years ago

owen2345 commented 3 years ago

Features

TODO:

Samples:

    $> 10 # => 10
    $> puts "previous result: #{__}" # => previous result: 10

    $> var1 = "Var1" # => ""
    $> puts "var1 is: #{var1}" # => var1 is: Var1

    # Array support
    $> arr = [1,2,3]
    $> puts "arr result: #{arr} and size: #{arr.size}" # => arr result: [1, 2, 3] and size: 3

    # Hash support
    $> hash = { s: "String", number: 10 }
    $> puts "result: #{hash} and number: #{hash["number"]}" # => result: {s: "String", number: 10} and number: 10

    #complex objects (Amber app)
    $> require "./config/application.cr"
    $> qty_articles = Article.count
    $> article = Article.create(title: "Article 1")
    $> puts "qty_articles is #{qty_articles} vs #{Article.count}" # qty_articles is 0 vs 1
    $> puts "last article is: #{article.title}" # last article is: Article 1

Status: In progress

jwoertink commented 3 years ago

Well this is interesting! I'll come back to review a bit later, but my first question is what is the difference between _p and __ (two underscores we currently use)?

owen2345 commented 3 years ago

Well difference between _p and __ is that __ can not be used as a variable in the next command, against _p which can be used as a normal variable like:

puts "previous value: #{_p}" 

Btw: I tried to replace _p into __ but unfortunately crystal does not accept as a valid variable name, either _;

jwoertink commented 3 years ago

__ can not be used as a variable in the next command

I'm not sure that I follow exactly. I know this works in Crystal

__ = 1

__ += 1

puts __

Here's the PR where it was added in originally https://github.com/crystal-community/icr/pull/63

I just want to make sure that if we're making this change, it's not a 1 to 1 of what we have now for the sake of "a better name" or something along those lines. I'd rather not have 2 ways to do this, so if we're going to add in _p, then we should remove __ with an explanation as to why _p is a better solution. Hope that makes sense.

owen2345 commented 3 years ago

@jwoertink you were right! Sorry my mistake. I was too tired yesterday then I could not test it very well.

jwoertink commented 3 years ago

No worries! I just want to make sure we're getting this right. Thanks for contributing ❤️