小编Tos*_*shi的帖子

让我们输入for循环

我正在玩Swift.为什么可以let在for循环中声明类型?据我所知,let意味着不变,所以我很困惑.

    func returnPossibleTips() -> [Int : Double] {
        let possibleTipsInferred = [0.15, 0.18, 0.20]
        //let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]

        var retval = Dictionary<Int, Double>()
        for possibleTip in possibleTipsInferred {
            let inPct = Int(possibleTip * 100)
            retval[inPct] = calcTipWithTipPct(possibleTip)
        }

    return retval

    }
Run Code Online (Sandbox Code Playgroud)

constants let swift

4
推荐指数
1
解决办法
3685
查看次数

用 SMARTY + PHP 将 CSS 部分放在一起

如何完全分离css文件

我正在考虑如何将 CSS 部分放在 Smarty + php 生成的 html 文件中。CSS 应该包含在标题中,但是如果页面确实有一些单独的 CSS 样式怎么办?

我使用模板引擎 Smarty。首先,我制作了标题(收集所有常用的东西。)并使用聪明的语法将其包含在 html 文件的开头。如果页面中有 css 的某些单独部分,我会将 css 放在“include 语句”下方。

带有 Smarty 语法的 HTML 文件

{include file='_include/header.inc.html'}

    <link rel="stylesheet" href="/assets/style/top.css">
    <script type="text/javascript">
        $(function(){
            $('#eyecatch ul').bxSlider({
                pager: false,
                auto: true,
                pause: 5000
            });
        })
    </script>
    <article>
    </article>

{include file='_include/footer.inc.html'}
Run Code Online (Sandbox Code Playgroud)

但是我的同事检查了用 Smarty 生成的代码,并说“这不是一个好习惯,因为即使有效也应该放在一个地方”

标题正下方,因为我没有将此行放在标题中。

正如他指出的那样,您有什么好主意可以用 Smarty 清理/组织我的代码吗?

html css php smarty smarty3

4
推荐指数
1
解决办法
3322
查看次数

干燥重复的救援声明

我使用 stripe 作为支付处理器。

在应用程序中,我向 Stripe 发送请求以执行收费或其他类型的流程,并且基本上使用如下所示的相同错误处理样板。

rescue Stripe::InvalidRequestError => e,  
  # do something
rescue Stripe::AuthenticationError => e, 
  # do something
rescue Stripe::APIConnectionError => e, 
  # do something
rescue Stripe::StripeError => e
  # do something
rescue => e
  # do something
end
Run Code Online (Sandbox Code Playgroud)

虽然我可以肯定rescue每个 API 调用中的每种错误类型,但这是很多样板代码,我很乐意只rescue处理所有这些错误类型,然后构建一个方法来执行日志记录、发送通知等操作。

我怎样才能以一种更干净的方式将它们捆绑到一个异常处理程序中(让事情变得干燥),如下所示?

def call
   plan = Plan.new(attrs)
   return plan unless plan.valid?

   begin
     external_card_plan_service.create(api_attrs)
   rescue Exceptions::Stripe => e
     plan.errors[:base] << e.message
     return plan
   end

   plan.save
   plan.update(is_active: true, activated_at: Time.now.utc)
   plan
 end
Run Code Online (Sandbox Code Playgroud)

ruby error-handling

3
推荐指数
1
解决办法
401
查看次数

Postgres创建时间戳(当前日期+特定时间)格式

我希望有一个由组成的时间戳 <[current date] + [specific time I can specify as a string]>

例如 2016-03-08 23:59:59

  • 2016-03-08 =>当前日期
  • 23:59:59 =>我可以指定

我很想做类似的事情

cast(cast( now()::timestamp(0) AS string) || '23:59:59' ) AS timestamp )

以更多的postgres方式。

sql postgresql

2
推荐指数
1
解决办法
811
查看次数

oh-my-zsh 别名颜色高亮

我开始使用 zsh 和名为“oh-my-zsh”的插件,并在 ~/.zshrc 中设置我的个人别名。

alias ls='ls -aF'
Run Code Online (Sandbox Code Playgroud)

进而

source ~/.zshrc
Run Code Online (Sandbox Code Playgroud)

但该命令ls不会突出显示输出。(不过该命令可以正常工作。)

我实在不明白为什么。

有任何想法吗?

zsh zshrc oh-my-zsh

1
推荐指数
1
解决办法
4457
查看次数

标签 统计

constants ×1

css ×1

error-handling ×1

html ×1

let ×1

oh-my-zsh ×1

php ×1

postgresql ×1

ruby ×1

smarty ×1

smarty3 ×1

sql ×1

swift ×1

zsh ×1

zshrc ×1