Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 13:47
    straight-shoota closed #12051
  • 13:47
    straight-shoota closed #12050
  • 12:17
    beta-ziliani edited #7318
  • May 15 08:43
    r00ster91 closed #7318
  • May 15 08:17
    r00ster91 closed #7690
  • May 14 17:01
    Blacksmoke16 labeled #11995
  • May 14 13:03
    webhive closed #12055
  • May 14 10:19
    straight-shoota milestoned #12051
  • May 14 10:00
    I3oris edited #12051
  • May 14 08:45
    oprypin review_requested #11977
  • May 13 23:29
    Blacksmoke16 unlabeled #12061
  • May 13 22:56
    Blacksmoke16 edited #12061
  • May 13 22:56
    Blacksmoke16 edited #12061
  • May 13 22:54
    Blacksmoke16 labeled #12061
  • May 13 22:54
    Blacksmoke16 labeled #12061
  • May 13 22:54
    Blacksmoke16 labeled #12061
  • May 13 22:54
    Blacksmoke16 opened #12061
  • May 13 20:14
    straight-shoota labeled #12060
  • May 13 20:14
    straight-shoota labeled #12060
  • May 13 20:14
    straight-shoota opened #12060
What you guys think ;)
Stephan Langeveld
@Fanna1119
how would i get the value of items inside a 2d kind of a hash array, i honestly don't even know what terminology to use :D

content = {:id => "3kTMd",
  :lists => [{:tags => "false",
  :url => "http://localhost:8080/"}]
}

x = content[:lists]

puts x
Ghost
@ghost~55586c4815522ed4b3e08557
@eliasjpr :+1:
Elias Perez
@eliasjpr
Im ordering a set of them :)
Ghost
@ghost~55586c4815522ed4b3e08557
@eliasjpr I was thinking about one with big logo on the back (with Crystal written under) (with my pseudo at the top! ^^) on a little <logo + Crystal> on the front left, but those a great start! :p
Ary Borenszweig
@asterite
content[:lists].as(Array)
Stephan Langeveld
@Fanna1119
@asterite Awesome! Thank You!! :)
Zafar Ansari
@ansarizafar
If a method name is stored in a variable how can I run it?
Ghost
@ghost~55586c4815522ed4b3e08557
you can't
Jason Waldrip
@jwaldrip
Crystal is not as meta as ruby.
@ansarizafar can you provide an example?
Zafar Ansari
@ansarizafar
After making several unsuccessful attempts of storing a proc in a Hash without specifying return type. Now I am trying to store a class method in a Hash so that I can later run it.
Like that
store[key] = User.list
how can I run list method which is store in a hash
Ghost
@ghost~55586c4815522ed4b3e08557
do you need to pass arguments to thoses methods when calling them?
Zafar Ansari
@ansarizafar
yes
Ghost
@ghost~55586c4815522ed4b3e08557
are they known when you add the method to the hash?
Zafar Ansari
@ansarizafar
no
Ary Borenszweig
@asterite
case key; when "list" then User.list; etc.
Zafar Ansari
@ansarizafar
I want to make it generic. Is there any syntax that I can use to run the method stored in hash directly.
Jason Waldrip
@jwaldrip
Can the proc/method return any type?
Zafar Ansari
@ansarizafar
Ideally I would like to store a proc block in hash without specifying return type as return type is not known in advance, but I think its not possible.
Jason Waldrip
@jwaldrip
If you know what the possible types could be you could store them in a union
Zafar Ansari
@ansarizafar
Like that
@@store = {} of String => (Hash(String, JSON::Type) -> _)
Jason Waldrip
@jwaldrip
or, let me ask a question... do you expect your method to always return something?
Or are you just trying to invoke a method that may just invoke something, not nessicarily return something?
Ghost
@ghost~55586c4815522ed4b3e08557
@ansarizafar do you need the return value when calling it?
Zafar Ansari
@ansarizafar
yes, It could be a NameTuple or an Array Of NamedTuples
Jason Waldrip
@jwaldrip
So use a union
Zafar Ansari
@ansarizafar
Yes I need return value
Jason Waldrip
@jwaldrip
If you know the various return types, just set it up as a union
Zafar Ansari
@ansarizafar
Return types are not known in advance. Namedtuple will be generated with the fields returned from Postgre Db depending on the query.
Will
@Willamin
Hi everyone! I've got an absolutely crazy question for you. I want to reopen the Integer structs and add a macro def that throws a compile time exception based on the value of the Integer. I can't find a definitive list of macro variables such as @type and @def. Can anyone here help?
Ghost
@ghost~55586c4815522ed4b3e08557
throw a compile time error when doing what @Willamin ?
Will
@Willamin
Contrived example: I'd like to add a .is_this_a_0_at_compile_time to Integers that throws a compile time error if it's called on a non-zero integer
0.is_this_a_0_at_compile_time # should compile fine
1.is_this_a_0_at_compile_time # should throw a compile time error
Ghost
@ghost~55586c4815522ed4b3e08557
how is this supposed to be used?
Zafar Ansari
@ansarizafar
Here is my current code
module WebWare
  abstract class Method
    abstract def call(args)
  end

  class Api
    @@store = {} of String => Method

    def initialize
    end
    def self.register(name : String, method : Method)
      @@store[name] = method
    end

    def self.run(context : HTTP::Server::Context)
      begin
        data = Tools.json_param(context)
        finalResult = data.map do |key, value|
          {key => @@store[key].call(value.as(Hash))}
        end

        return finalResult.to_json
      rescue ex
        # context.response.respond_with_error("Internal error", 500)
        "Internal error in Method"
      end
    end
  end
end
Ghost
@ghost~55586c4815522ed4b3e08557
The syntax you ask is not possible because macros are not methods with a receiver, they're on the class/module/top level, not instance level, but you could have is_this_a_0_at_compile_time(0) quite easily @Willamin
Zafar Ansari
@ansarizafar
Here is User class
include WebWare

class Users < Method
  def call(args)
    begin
      count = Db.scalar "select count(*) from restock.users"
      data = [] of {id: String, name: String}

      Db.query "select id, name from restock.users" do |rs|
        rs.each do
          id, name = rs.read(String, String)
          data << {id: id, name: name}
        end
      end

      {success: true, data: data}
    rescue ex
      {success: false, data: [] of {id: String, name: String}}
    end
  end
end

Api.register "user.list", Users.new
Will
@Willamin
@bew even when it's a macro on a literal struct like an Int32?
From IRC (bridge bot)
@FromIRC
<oprypin> @eliasjpr, do u have rights to sell Crystal logo?
Ghost
@ghost~55586c4815522ed4b3e08557
yes, if you write a macro my_macro in Int32's struct, you'll be able to call it as Int32.my_macro or just my_macro when called in the scope of the Int32 struct
From IRC (bridge bot)
@FromIRC
<Papierkorb> William, how should the program know at compile-time, which run-time value the integer will have?
<Papierkorb> Crystal isn't a time machine
Ghost
@ghost~55586c4815522ed4b3e08557
\o/
Will
@Willamin
hahaha
From IRC (bridge bot)
@FromIRC
<Papierkorb> A method that takes an integer (or anything) as argument doesn't know if it was passed in as literal or through an variable (or ...), if that's what you mean.
Will
@Willamin
I was hoping that I would also be able to tell if the macro was called from a literal (eg. 0.is_this_a_0_at_compile_time) vs called from a variable (eg. x.is_this_a_0_at_compile_time),