Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 15:05
    beta-ziliani edited #11334
  • 15:04
    beta-ziliani synchronize #11334
  • 14:59
    straight-shoota labeled #11331
  • 14:59
    maxfierke closed #11329
  • 14:52
    straight-shoota synchronize #11317
  • 14:49
    straight-shoota edited #11316
  • 14:46
    straight-shoota labeled #11335
  • 14:46
    straight-shoota unlabeled #11335
  • 14:29
    mishushakov opened #11335
  • 14:29
    mishushakov labeled #11335
  • 14:21
    beta-ziliani opened #11334
  • 13:35
    straight-shoota synchronize #11317
  • 13:30
    straight-shoota assigned #11318
  • 13:25
    straight-shoota milestoned #11318
  • 13:03
    straight-shoota labeled #11328
  • 13:03
    straight-shoota labeled #11326
  • 13:03
    straight-shoota labeled #11326
  • 13:03
    straight-shoota labeled #11326
  • 13:02
    straight-shoota labeled #11327
  • 12:58
    straight-shoota labeled #11322
emanzx
@emanzx
I always encounter this error in crystal and most of the solutions are tries and error..
I have a hash like this.
```
configs = {"bind_conf" => {"path" => "/usr/home/system/configs/bind", "ext" => "conf", "cmd" => {"exec" => "rndc", "args" => "reconfig"}}, }
if File.file?("#{watch_log_path}")
        File.each_line("#{watch_log_path}") do |line|
        split_line = line.split(" ")
        file_path = split_line[0] + split_line[2]
        file_extension = split_line[2].split(".").last if split_line[2].split(".").size > 1
        events[file_path] = {"file_name" => split_line[2], "event" => split_line[1], "ext" => file_extension}
        end
        #cleanup the file after read
        File.open "#{watch_log_path}", "w+" { }
    end
    #dynamic event processing begin below.
    #copy the event and clean up the current.
    process_events = events.clone                                                        
    events.clear
    configs.each do |job,param|
        puts "Processing #{job}"
        process_events.each do |change, event_params|
            monitor_path = param["path"]
            if change.includes?(monitor_path)
                puts "Processing change #{change}"
            end 
        end
    end
Taupiqueur
@alexherbo2
can I use return if I yield instead of block.call?
emanzx
@emanzx
does crystal implement catch and throw like in ruby to handle nested loop?
From IRC (bridge bot)
@FromIRC
<jhass[m]> no
<jhass[m]> I never ever needed that feature in Ruby either and always found any usages of it I found strange and hard to follow
From IRC (bridge bot)
@FromIRC
<mfiano> How can I define a generic struct with all of its fields being of type T (homogeneous), where T is restricted to Float32 or Float64?
From IRC (bridge bot)
@FromIRC
<jhass[m]> We don't support restricting type variables yet. A hack is to check the type in macro land using https://carc.in/#/r/bk8d
<mfiano> :⁠( Thanks
From IRC (bridge bot)
@FromIRC
<xyhuvud> shouldn't it be possible by setting type restrictions on the constructors though?
George Dietrich
@Blacksmoke16
you could, but then you end up with a union of the two
manveru
@manveru:matrix.org
[m]
is there some trick to using @type.instance_vars in a module to get the variables of a class that it was included into?
i have this code, but both annoations and instance vars are empty:
module Test
  annotation Flag
  end

  module Configuration
    macro included
      {% puts @type %}
      {% puts @type.annotations(Flag) %}
      {% puts @type.instance_vars %}
    end
  end

  struct Config
    include Configuration

    @[Flag(name: "a", short: "a", type: String?)]
    @a : String?
  end
end
George Dietrich
@Blacksmoke16
it has to be called in the context of a method
manveru
@manveru:matrix.org
[m]
ah, i guess the problem is with the macro included...
  module Configuration
    def initialize
      {% puts @type %}
      {% puts @type.annotations(Flag) %}
      {% puts @type.instance_vars %}
    end
  end
this now gives me instance vars, but not annotations
George Dietrich
@Blacksmoke16
the problem is @type.instance_vars has to be used in the context of a method, which it is now
and annotation is empty because the macro is being expanded within the module i'd guess
manveru
@manveru:matrix.org
[m]
ah, the annotation is on the instance var
thanks :)
From IRC (bridge bot)
@FromIRC
<mfiano> I'm trying to affect how an instance of a struct prints with puts, but defining a custom to_s in the struct scope doesn't seem to do anything. What is the correct way?
George Dietrich
@Blacksmoke16
def to_s(io : IO) : Nil
then write the content to that io
From IRC (bridge bot)
@FromIRC
<mfiano> Perfect, thanks.
George Dietrich
@Blacksmoke16
there's also def inspect(io : IO) : Nil which is similar but diff
From IRC (bridge bot)
@FromIRC
* mfiano reads
<mfiano> I see.
<mfiano> Crystal is just amazing me the more I learn it. I'm pretty excited to be getting started. It's so nice to be able to just write what I mean with minimal syntax/short symbols like "to_s".
George Dietrich
@Blacksmoke16
fun fact, to_s also gets invoked if you add a var into a string. I.e. puts "Hello #{user}" is the same as puts "Hello #{user.to_s}"
From IRC (bridge bot)
@FromIRC
<mfiano> I remember reading the performance gotchas about that
From IRC (bridge bot)
@FromIRC
<mfiano> I'm struggling with multiple struct constructors. I have a struct, Foo, with @a and @b variables. I'd like to invoke it as Foo.new(1.2, 3.4), or Foo.new(1.2), in which case, @a and @b would be set to 1.2.
George Dietrich
@Blacksmoke16
is the 2nd value just the first one doupeld?
doubled
From IRC (bridge bot)
@FromIRC
<mfiano> Given one argument, both variables should be set to the same value.
George Dietrich
@Blacksmoke16
def self.new(value : Float64) : self
  new value, value
end

def initialize(@a : Float64, @b : Float64); end
should do it
From IRC (bridge bot)
@FromIRC
<mfiano> Ah so you can only have 1 initialize method?
<mfiano> I was trying to overload that multiple times :⁠/
George Dietrich
@Blacksmoke16
no, but
tl;dr i always just mke initialize represent the actual state of the instance, but then have overloaded .new to transform the arguments into something the initializer constructs
expects*
From IRC (bridge bot)
@FromIRC
<mfiano> That post was very well explained, thanks.
George Dietrich
@Blacksmoke16
From IRC (bridge bot)
@FromIRC
<mfiano> Is there a way to alias a module at the file level?