tag:blogger.com,1999:blog-65230665730876581502024-03-13T20:08:01.687+10:00simonbairdruby, javascript, php, etcsimonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-6523066573087658150.post-22411460830237602902021-10-11T10:15:00.009+10:002021-10-14T23:47:58.295+10:00"Add support for Logitech Zone 750"<p></p><div>I bought <a href="https://www.amazon.com/gp/product/B08431F351/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=simonbaird-20&creative=9325&linkCode=as2&creativeASIN=B08431F351&linkId=82db15241e7d5bc2c1fff72b20df3d1f" rel="nofollow" target="_blank">this headset</a>*, a Logitech Zone 750, and I wanted to turn down the "sidetone", i.e. the always-on foldback. After some searching I found <a href="https://github.com/Sapd/HeadsetControl">HeadsetControl</a>, which can do it, but didn't support this particular model. It did support the Logitech Zone Wired, which is almost, if not exactly, the same device.</div><div><br /></div><div>So I figured maybe I could do something about it and solve loud sidetone problem for myself and anyone else using this headset.</div><div><br /></div><div>Running `lsusb` (in Fedora) showed me the device number, from there it was pretty straight forward.</div><ul style="text-align: left;"><li> Pull request - <a href="https://github.com/Sapd/HeadsetControl/pull/174">Sapd/HeadsetControl#174</a></li></ul><div>Ps, the headset seems alright, but I don't know if it's four times better than the <a href="https://www.amazon.com/gp/product/B0091F8F7A/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=simonbaird-20&creative=9325&linkCode=as2&creativeASIN=B0091F8F7A&linkId=e54c8fb660f5f998c70aed0b6a2a8162" rel="nofollow" target="_blank">H540</a>* it was replacing - unfortunately because the padding cover fabric was peeling and disintegrating on the top part where you can't easily replace it. Hopefully the fabric on the new one will last longer.</div><div><br /></div><div><br /></div><div><span style="font-size: xx-small;">* Amazon link with affiliate referral info included.</span></div><p></p>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-13970061471675846752021-03-31T05:08:00.005+10:002021-03-31T05:09:30.936+10:00An ansible fix for the jira module<p>It's a while ago now, but...</p><p>I fixed a bug in the jira ansible module. 😎</p><p>Details:</p><p></p><ul style="text-align: left;"><li>Issue - <a href="https://github.com/ansible-collections/community.general/issues/204">ansible-collections/community.general#204</a></li><li>Pull Request - <a href="https://github.com/ansible-collections/community.general/pull/205">ansible-collections/community.general#205</a></li></ul><p></p>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-33161106490693716102016-08-11T12:40:00.001+10:002016-08-11T12:40:19.862+10:00Tau in PythonPython <a href="https://bugs.python.org/issue12345">Issue 12345 "Add math.tau"</a> had its 5th birthday in June.<br />
<br />
Yesterday, this:<br />
<blockquote>
<a href="https://bugs.python.org/issue12345#msg272287">I'm just going to do this.</a><br />
<br />
- Guido van Rossum</blockquote>
It's happening!<br />
<br />
(See also my <a href="https://github.com/ruby/ruby/pull/644">Ruby MR 644</a> being considered and <a href="https://github.com/ruby/ruby/pull/644#issuecomment-123082639">politely rejected</a> a year ago.)simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-84349621026206388802015-03-13T11:34:00.000+10:002015-03-13T11:34:02.191+10:00Rails test helper for stubbing a constantWhen Googling for how to mock or stub a constant in your Rails test suite, I found out about RSpec's stub_const method. I don't use RSpec though, so I wrote this.<br />
<br />
The way I use it is to include it in ActiveSupport::TestCase somewhere in test/test_helper.rb. <br />
<br />
class ActiveSupport::TestCase<br />
include WithStubbedConst<br />
...<br />
end<br />
<script src="https://gist.github.com/simonbaird/416f15d8ddf697ad6132.js"></script><br />
simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-76293770241085410742013-01-21T16:48:00.001+10:002013-01-22T13:13:58.806+10:00Fixing the caps lock key in Gnome in Fedora 18I like to set my caps lock key to left control. Previously you could do this via the Gnome System Settings control panel. In Fedora 18 it's not possible as far as I can tell.<br />
<br />
The gsettings command to make caps lock act as a control key in Gnome is this:<br />
<script src="https://gist.github.com/4584070.js?file=gistfile1.sh"></script><br />
If you are worried about losing any other settings you might have in xkb-options, then do this:<script src="https://gist.github.com/4584070.js?file=gistfile2.sh"></script><br />
This is an explanation I got just now (from <a href="http://who-t.blogspot.com.au/">someone who should know</a> about this stuff):<br />
<blockquote>"GNOME 3.0 shipped with input methods separately from xkb, now the two have been brought together to have one dialog. I suspect xkb options will come back, but that'll take another iteration"</blockquote><br />
Ps, for more info on gnome input sources, see <a href="http://blogs.gnome.org/mclasen/2012/09/21/input-sources-in-gnome/">these</a> <a href="http://blogs.gnome.org/mclasen/2012/12/18/input-sources-in-gnome-3-7-4/">blog</a> <a href="http://blogs.gnome.org/mclasen/2013/01/14/input-sources-in-gnome-3-7-4-continued/">posts</a> by mclasen.<br />
<br />
Pps, I've been an anti-caps lock campaigner for <a href="http://randomibis.com/capslockmustdie/">a while now</a> :)simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-20232562060601587032012-10-31T12:35:00.001+10:002018-10-25T09:36:12.974+10:00Nice brief git log formatI'm really happy with this. There's two new features compared to what I've been using up until recently.<br />
<br />
Firstly, I just learned about the core.pager option which solves the problems I was solving previously with this ugliness:<br />
<tt style="font-size: 90%;">ll = "!git --no-pager log --pretty=nice -n30; echo"</tt><br />
<br />
Secondly, the <tt>%+d</tt>. I generally don't use the --graph log formats, (<a href="http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs">such as these</a>), that show you the branch paths, but seeing where the refs are is super useful. The + makes it appear on the next line without adding a line break when there are no refs.<br />
<script src="https://gist.github.com/3984436.js?file=.gitconfig"></script><br />
Here's a screenshot of how it looks (with details obscured to protect the innocent).<br />
<br />
<div>Update: I dropped the line break before the refs list, which is fine if you have a nice wide terminal, and tweaked some colours. Now it's:<br />
<br />
<pre style="overflow:scroll"> nice = format:%Cblue%h %C(cyan)%cr %Cgreen%an%Creset %s%C(yellow)%d %Creset
</pre></div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-9WegrZFgt7w/UJCQg0PAzKI/AAAAAAAAFHc/u-Wo2olSsGU/s1600/gitlog.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="192" src="https://2.bp.blogspot.com/-9WegrZFgt7w/UJCQg0PAzKI/AAAAAAAAFHc/u-Wo2olSsGU/s400/gitlog.jpg" width="400" /></a></div>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-13498278510590688352012-10-02T11:08:00.000+10:002013-01-21T17:07:55.022+10:00Some "progress" on my Tau in Ruby patchIts category was changed from "core" to "joke" :(... How does the quote go? "First they ignore your patch, then they classify it as a joke, then... " I forget the rest ;)<br />
<br />
<blockquote>"Once Ï„ is widely accepted in these communities, we might add it..."<br />
</blockquote><br />
But also:<br />
<br />
<blockquote>"...if it were just up to me, I'd add it just to show support. It's a rather tiny and harmless addition."<br />
</blockquote><br />
<a href="http://bugs.ruby-lang.org/issues/4897">See the ticket here.</a>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-25377408306036234322012-08-14T18:16:00.002+10:002013-01-21T17:06:05.612+10:00Haskell?It compiles so I guess that means I wrote some <a href="http://www.haskell.org/haskellwiki/Haskell">Haskell</a>! :)<br />
<br />
(See my <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a> <a href="https://github.com/jgm/pandoc/pull/585">pull request</a> and <a href="https://github.com/simonbaird/pandoc/commit/36a292f18a35c9fde3717c34391cfde544db06bb">commit</a>).simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-40358991372399954992012-08-08T18:29:00.001+10:002012-08-08T18:55:19.292+10:00Make it easier to use partials with blocks in railsThe shorter version of render :partial is widely known and used. These are equivalent, but the second version is so much more succinct and readable.
<script src="https://gist.github.com/3293321.js?file=shortrender.html.erb"></script>
To use a partial with a yield in it, (what I call a "block partial"), you can use render :layout which a. doesn't read very well, (it's a partial, not a layout clearly), and b. can't be shortened nicely the way that render :partial can.
So here is block_render:
<script src="https://gist.github.com/3293321.js?file=block_render_helper.rb"> </script>
It lets you have nice neat render calls like this:
<script src="https://gist.github.com/3293321.js?file=blockrender.html.erb"></script>
This is a real-life example of a block partial I like to use. Those classes are <a href="http://twitter.github.com/bootstrap/">bootstrap</a> classes for making a nice drop down menu.
<script src="https://gist.github.com/3293321.js?file=more_menu_button.html.erb"></script>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-61944637866177191602012-08-08T17:20:00.001+10:002012-08-08T18:34:59.003+10:00How to make pinned tabs in Firefox a bit widerI'm using <a href="https://addons.mozilla.org/en-US/firefox/addon/stylish/">Stylish</a> to apply this, but I guess you could do it with a <a href="http://lifehacker.com/197715/customize-firefox-with-userchromecss">userChrome.css</a>.
<script src="https://gist.github.com/3292963.js?file=styles.css"></script>
The main reason I want this is so the hit target is larger. I want to be able to click my pinned app tabs (currently a TiddlyWiki and my Zimbra corporate calendar) without aiming too hard.simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com1tag:blogger.com,1999:blog-6523066573087658150.post-8100144225724414822012-02-08T12:31:00.003+10:002013-01-22T10:32:04.403+10:00Make ActiveRecord::Base#create respect the :type attribute<p>I think I've needed and implemented this twice now, so let's make a post about it.</p><p>The problem occurs when you are using f.fields_for and nested forms, as per <a href="http://railscasts.com/episodes/196-nested-model-form-part-1">these</a> <a href="http://railscasts.com/episodes/197-nested-model-form-part-2">railscasts</a>, and the nested objects are using STI. </p><p>(Will leave a more detailed explanation for future posts).</p><p><b>Update 1:</b> Even though this works as described, I don't think it solves my problem. Will post again if I figure it out...</p><p><b>Update 2:</b> Found <a href="http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/">this</a> which is I think what I was using last time I had this problem... :S</p><br />
<script src="https://gist.github.com/1764629.js"></script>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-87370124349394699562011-10-03T10:17:00.001+10:002011-10-03T10:17:29.075+10:00Another approach to using helper methods in your controller actions in Ruby on Rails<script src="https://gist.github.com/1258160.js"> </script>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-69029215837552818392011-09-20T15:18:00.000+10:002012-08-08T18:37:36.366+10:00A Rails 3 gotcha with html_safeThis was causing me to scratch my head a bit today. I'm glad I know what's going on now and why you need to use safe_concat. It still seems a little unintuitive though.
For more into see the <a href="http://techspry.com/ruby_and_rails/html_safe-and-helpers-in-rails-3-mystery-solved/">explanation here</a> (under the "the tricky part") and the api docs on <a href="http://api.rubyonrails.org/classes/ActiveSupport/SafeBuffer.html">ActiveSupport::SafeBuffer</a>
<script src="https://gist.github.com/1228368.js"> </script>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com1tag:blogger.com,1999:blog-6523066573087658150.post-29206421292946418802011-09-20T13:33:00.000+10:002011-09-28T10:00:08.255+10:00My (first) contribution to RailsI fixed a little mysql configuration bug in Rails. It counted as three commits (even though they are the same thing, just in different branches), which puts me unfairly into the top 500 all time committers. ;) <a href="http://contributors.rubyonrails.org/contributors/simon-baird/commits">See here</a>.
<br/>
<br/>
You'd think DHH would be number one, but actually he's not. Take a look at <a href="http://contributors.rubyonrails.org/contributors">the list</a>.
<br/>
<br/>
Here is my <a href="https://github.com/rails/rails/pull/1952">original pull request</a> if you'd like to read more about it.
<br/>
<br/>
Ps, I also reported <a href="https://github.com/jnicklas/capybara/issues/464">a capybara bug</a> (along with a failing test) that got fixed pretty quickly. But my fork didn't get merged so none of my commits are recorded. :Psimonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-57463046997885650512011-06-17T00:50:00.005+10:002018-06-15T08:45:53.785+10:00Tau in RubyInspired by <a href="http://bugs.python.org/issue12345">http://bugs.python.org/issue12345</a> and of course <a href="http://tauday.org/">The Tau Manifesto</a>, here is a patch for ruby to define tau, the true circle constant. <script src="https://gist.github.com/1029552.js"> </script> Of course we don't need to really need to patch ruby to get this functionality (but I had fun doing it anyway). <br />
<br />
Here is tau.rb so you can just <tt>require 'tau'</tt>. (Maybe this should be a gem...) <script src="https://gist.github.com/1029636.js"> </script> Ps, I just learned about <a href="http://ruby-doc.org/core/classes/Module.html#M000451">module_function</a>. <br />
<br />
<b>Update 1:</b> I made <a href="http://redmine.ruby-lang.org/issues/4897">this ticket</a> which has been mostly ignored.<br />
<br />
<br />
<b>Update 2:</b> Ticket has been noticed and discussed! :) Ticket has been categorised as joke! :(<br />
<br />
<br />
<b>Update 3:</b> Some active debate is going on now and the ticket is now 'assigned'! :) See <a href="http://redmine.ruby-lang.org/issues/4897">the details</a>.<br />
<br />
<br />
<b>Update 4:</b> (28-Jun-2014) Made a <a href="https://github.com/ruby/ruby/pull/644">pull request in Github</a>.<br />
<br />
<br />
<b>Update 5</b>: See <a href="https://github.com/jneen/math-tau/">https://github.com/jneen/math-tau/</a>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-41717335115146816342011-03-17T11:15:00.027+10:002011-05-03T17:11:14.619+10:00beginning_of_fortnight gem is outAs <a href="http://simonbaird.blogspot.com/2011/01/some-meta-progamming-fun-with-ruby.html">promised</a> I've cleaned up my <a href="http://glosoli.blogspot.com/2010/10/beginningoffortnight-method-for-rails.html">beginning of fortnight hack</a> and turned it into a proper ruby gem.<br />
<br />
Install in the usual way:<br />
<pre class="code">sudo gem install beginning_of_fortnight</pre><br />
It requires ActiveSupport and adds {next,end_of,beginning_of}_fortnight methods to the Date and Time class. They work the same as the *_week methods defined by ActiveSupport. You can set where you want fortnight boundaries to be (or just use the default).<br />
<br />
For more info see:<br />
<ul><li><a href="https://github.com/simonbaird/beginning_of_fortnight">Code (github)</a></li>
<li><a href="http://rubydoc.info/gems/beginning_of_fortnight/1.0.1/frames">Docs (rubydoc.info)</a></li>
<li><a href="http://rubygems.org/gems/beginning_of_fortnight">Gem page (rubygems.org)</a></li>
</ul><br />
I carefully made sure the docs look good in RDoc but, rubydoc.info uses <a href="http://yardoc.org/">YARD</a>. So they don't look so great and actually the main usage examples which I put at the top of the file aren't showing at all...<br />
<br />
I guess I will convert to using YARD. Is RDoc fading away? The <a href="http://rdoc.sourceforge.net/">website</a> looks pretty musty.<br />
<br />
<small>(Just for fun, here are tweets <a href="https://twitter.com/#!/simonbaird/status/26525362848">where</a> <a href="https://twitter.com/#!/simonbaird/status/26525473774">it all</a> <a href="https://twitter.com/#!/simonbaird/status/26530170605">began</a>).</small>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-86479758940199737962011-03-17T10:59:00.007+10:002012-08-08T18:38:50.970+10:00Bash functions to dump and load a mysql databaseEveryone probably has one of these, but here is mine. It's a bit rough but works for me.
If I was going to make it better I'd probably use getopt instead of $1 and $2 and then add options for changing the mysql user. (Currently it hard codes the user as root).
Put it in your .bash_aliases or .bashrc or wherever you keep these types of things.
<script src="https://gist.github.com/873618.js?file=dbdump.sh"></script>
<b>Update:</b> I made a set of rake tasks for doing this kind of thing, so I'm not actually using this any more.simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-55768001702165075132011-02-24T15:37:00.012+10:002011-05-03T17:12:05.357+10:00Using the singleton class to include a module on-the-fly<p>I still don't entirely understand the syntax for accessing the singleton class, but I have found a use for it. :) The goal is to be able to make an object include a mixin on the fly.</p><p>If you do <small><tt>self.class.send(:include,module_name)</tt></small> instead of using the singleton class then the change affects every instance, not just the one you intended.</p><p>So it seems to work pretty well. Ruby is all like "sure buddy, have some rope, take as much as you need". :)</p><script src="https://gist.github.com/841810.js"> </script><p>I should say that this technique was designed to be a quick fix to let us work around some legacy code that really should be refactored and fixed properly. I'm not saying this is a sensible design pattern, but it is cool that ruby can do it.</p><p>Here is a good <a href="http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html">explanation of singleton classes</a> in ruby.</p>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-44942690397276222072011-01-07T01:18:00.007+10:002016-08-11T12:52:35.755+10:00Some meta-progamming fun with Ruby (a define_method example)I am currently working on my first gem which I hope to publish soon. It will be based on <a href="http://glosoli.blogspot.com/2010/10/beginningoffortnight-method-for-rails.html">this beginning_of_fortnight method</a> but will be more complete and flexible and (hopefully) it will be properly tested and documented. And it will be a gem of course, so it will be easy to install.<br />
<br />
So the logic used to determine which half of a fortnight a given day falls in I wrote to work with Time objects. I decided that re-implementing it with Date objects would not be trivial. Take a look at this:<br />
<br />
<pre style="font-size: 75%;">>> (Date.today + 1.week) - Date.today
=> Rational(7, 1)
>> (Time.now + 1.week) - Time.now
=> 604799.999613
>> (Time.now + 1.week) - Time.now
=> 604799.999532
>> 7.days.to_i
=> 604800
</pre>
<br />
The result of subtracting two Dates surprised me a little. I guess Rational(7, 1) is 7 days. But anyway I didn't want to get any deeper into that so I did it like this: <script src="https://gist.github.com/767991.js?file=meta.rb"></script><br />
So instead of figuring out how to apply my algorithm to Dates I will just convert them to Times, call the Time method on them, then convert them back to Dates afterwards. <br />
<br />
Actually Date#to_time and Time#to_date are not part of the standard library but are defined by ActiveSupport. Since my gem requires ActiveSupport that's fine. Of course I could have written out the three methods but why not add some meta-programming coolness to the lazy to DRY it right up.<br />
<br />
The splat on *args is important. My Time methods take an argument with a default value. The splatted args takes care of that so no matter how many arguments there are (or aren't), they will get passed correctly into the send. If anyone wants to check out (and/or checkout) the WIP gem, you can find it <a href="https://github.com/simonbaird/beginning_of_fortnight">here (github)</a>. Feedback is welcome.<br />
<br />
Todos include cleaning the rdoc output and adding gem dependencies to the gemspec (though I don't know if I'd want it to auto-install active support if you didn't have it already...)simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0tag:blogger.com,1999:blog-6523066573087658150.post-16104091549092134472010-11-22T10:07:00.004+10:002011-09-20T16:02:01.678+10:00hamrbl: write ruby with python style whitespace (a haml hack)<p>Did you ever wish your code looked more like your haml?</p><p>Did you ever wonder what ruby would be like with python style significant whitespace?</p><p>Without further ado, I present hamrbl.rb</p><script src="https://gist.github.com/703255.js"></script> <p>Surprisingly the syntax highlighting works fine in vim and github. :)</p><p>Ps, it's pronounced "hammerable"</p><p><small>Disclaimer: This is for amusement purposes only. It is almost certainly a terrible misuse of <a href="http://haml-lang.com/">Haml</a> :).</small></p>simonhttp://www.blogger.com/profile/07800779073409671908noreply@blogger.com0