tricks | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Mon, 07 Sep 2015 23:48:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Memo #3: Advanced usage of Ruby hashes and arrays https://kpumuk.info/ruby-on-rails/memo-3-advanced-usage-of-ruby-hashes-and-arrays/ https://kpumuk.info/ruby-on-rails/memo-3-advanced-usage-of-ruby-hashes-and-arrays/#comments Fri, 23 Jan 2009 00:59:58 +0000 http://kpumuk.info/?p=254 One of the most used features in any programming language is a Hash. Today we are going to talk about some of the Ruby’s Hash features, which are well documented, but rarely used — parameters of the Hash constructor. In the second part of this article we will take a look at the arguments of […]

The post Memo #3: Advanced usage of Ruby hashes and arrays first appeared on Dmytro Shteflyuk's Home.]]>
Ruby One of the most used features in any programming language is a Hash. Today we are going to talk about some of the Ruby’s Hash features, which are well documented, but rarely used — parameters of the Hash constructor. In the second part of this article we will take a look at the arguments of the Array class’ constructor.

Take a look at the following example.

1
2
3
4
5
6
a = %w(apple banana apple)
h = a.inject({}) do |h, fruit|
  h[fruit] ||= 0
  h[fruit] += 1
  h
end

Here we have an array of fruits and we need to calculate a frequency of each fruit. As you can see, in the line 3 we are initializing frequency value to 0 if there are was no fruit with this name before. We can simplify this code:

1
2
3
4
5
a = %w(apple banana apple)
h = a.inject(Hash.new(0)) do |h, fruit|
  h[fruit] += 1
  h
end

In line 2 we are creating a new hash, which default value is 0. This means that if we would try to retrieve value for a non-existing key, 0 would be returned.

Let’s check another example:

1
2
3
4
5
6
a = %w(apple banana apple)
h = {}
a.each_with_index do |fruit, i|
  h[fruit] ||= []
  h[fruit] << i
end

Here we are collecting indexes of each fruit in the source array. But now we can’t just create a new hash and pass [] as the default value, because all keys in this hash will refer to the same array, so in result we will get an array [1, 2, 3] for each fruit. So let’s try the following:

1
2
3
4
5
a = %w(apple banana apple)
h = Hash.new { |h, key| h[key] = [] }
a.each_with_index do |fruit, i|
  h[fruit] << i
end

In this case we are creating a new array object for any non-existing key, that was accessed. So

1
  h['some non-existing key']

will return [] and save it in the hash. When you will hit this key next time, previously created array will be returned.

You can pass a block to Array constructor too. For example, you need an array with 10 random numbers:

1
2
a = []
10.times { a << rand(100) }

You can simplify it using map method:

1
a = (1..10).map { rand(100) }

But you can do it even easier:

1
a = Array.new(10) { rand(100) }

Next Memo will cover managing Ruby Gems, so stay tuned.

The post Memo #3: Advanced usage of Ruby hashes and arrays first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/ruby-on-rails/memo-3-advanced-usage-of-ruby-hashes-and-arrays/feed/ 6
Memo #2: Useful Git tricks with remote branches https://kpumuk.info/development/memo-2-useful-git-tricks-with-remote-branches/ https://kpumuk.info/development/memo-2-useful-git-tricks-with-remote-branches/#comments Mon, 22 Dec 2008 22:59:15 +0000 http://kpumuk.info/?p=245 Here in Scribd we are using Git as primary version control system. We have tons of branches created, merged and destroyed every day. Someday I hope to describe our workflow with Git, but today I want to write about some useful techniques of working with this incredible tool. It’s implied that you know what is […]

The post Memo #2: Useful Git tricks with remote branches first appeared on Dmytro Shteflyuk's Home.]]>
Git Here in Scribd we are using Git as primary version control system. We have tons of branches created, merged and destroyed every day. Someday I hope to describe our workflow with Git, but today I want to write about some useful techniques of working with this incredible tool.

It’s implied that you know what is Git itself and how to work with it. Below you can find some advanced tricks, that should be helpful for you (at least they were helpful for me).

As you may know, to remove local branch git branch -d branch_name command is used. But if this branch was pushed to the origin repo, it will never be deleted there. To remove a remote branch use the following command:

1
git push origin :branch_name

where branch_name — the name of your branch.

When you are working in a team, everybody can create and push their own branches, which will be retrieved from the Git server during git pull or git fetch. If a branch being removed from the server, it will remain in your local repo forever. To clean these stale branches up, use this:

1
git remote prune origin

Also don’t forget to take a look at the git_remote_branch command-line tool, which makes work with remote branches as easy as it is possible.

Have any questions? Ask me and I will answer you in following Memo– posts.

The post Memo #2: Useful Git tricks with remote branches first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/development/memo-2-useful-git-tricks-with-remote-branches/feed/ 3
Memo #1: Installing mysql and memcached gems on Mac OS X with MacPorts https://kpumuk.info/ruby-on-rails/memo-1-installing-mysql-and-memcached-gems-on-mac-os-x-with-macports/ https://kpumuk.info/ruby-on-rails/memo-1-installing-mysql-and-memcached-gems-on-mac-os-x-with-macports/#comments Mon, 22 Dec 2008 19:20:39 +0000 http://kpumuk.info/?p=215 I have not posted anything here for a long time. It’s hard to start blogging again, so I will write a short tips and tricks series called “Memo“. Today I’m going to talk about two Ruby gems I’m using in all my Ruby on Rails project: mysql and memcached. Every time I try to install […]

The post Memo #1: Installing mysql and memcached gems on Mac OS X with MacPorts first appeared on Dmytro Shteflyuk's Home.]]>
I have not posted anything here for a long time. It’s hard to start blogging again, so I will write a short tips and tricks series called “Memo“. Today I’m going to talk about two Ruby gems I’m using in all my Ruby on Rails project: mysql and memcached. Every time I try to install or update those gems on Mac OS X following error occurs:

1
2
3
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
    ERROR: Failed to build gem native extension.

And then I’m googling on how to install these gems. It’s time simplify my life and post commands here.

Installing the ruby mysql gem on Mac OS X and MacPorts

Installing mysql5 from MacPorts:

1
sudo port install mysql5

Now we can install mysql gem:

1
2
3
4
kpumuk@kpumuk-mbp~: sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5
Building native extensions.  This could take a while...
Successfully installed mysql-2.7
1 gem installed

Installing the ruby memcached gem on Mac OS X and MacPorts

First you need to install memcached and libmemcached:

1
sudo port install memcached libmemcached

And then memcached gem:

1
2
3
4
kpumuk@kpumuk-mbp~: sudo env ARCHFLAGS="-arch i386" gem install memcached --no-ri --no-rdoc -- --with-libmemcached-dir=/opt/local
Building native extensions.  This could take a while...
Successfully installed memcached-0.12
1 gem installed

If you have any questions that could be covered in this series — ask me in comments.

The post Memo #1: Installing mysql and memcached gems on Mac OS X with MacPorts first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/ruby-on-rails/memo-1-installing-mysql-and-memcached-gems-on-mac-os-x-with-macports/feed/ 1
Coalesce ?? operator in C# 2.0 https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/#comments Mon, 14 Jan 2008 19:18:27 +0000 http://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often: 12345public string Caption {     get { return ViewState["Caption"] != null […]

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often:

1
2
3
4
5
public string Caption
{
    get { return ViewState["Caption"] != null ? (string) ViewState["Caption"] : ""; }
    set { ViewState["Caption"] = value; }
}

Or even:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public string VisibleStatistic
{
    get
    {
        string retval = (string) ViewState["VisibleStatistic"];
        return retval == null ? "" : retval;
    }
    set
    {
        ViewState["VisibleStatistic"] = value;
    }
}

public string SelectedCategoryName
{
    get
    {
        object retval = ViewState["SelectedCategoryName"];
        if (retval != null)
            return (string) retval;
        return String.Empty;
    }
    set
    {
        ViewState["SelectedCategoryName"] = value;
    }
}

Almost 50% of this could be easily removed, and you will get beautiful and clear code, just right after you will understand what the hell is operator ??. Here is information from MSDN:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

Easy and clean explanation. Let’s try to rewrite all these examples using this operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public string Caption
{
    get { return (string) ViewState["Caption"] ?? String.Empty; }
    set { ViewState["Caption"] = value; }
}

public string VisibleStatistic
{
    get { return (string) ViewState["VisibleStatistic"] ?? String.Empty; }
    set { ViewState["VisibleStatistic"] = value; }
}

public string SelectedCategoryName
{
    get { return (string) ViewState["SelectedCategoryName"] ?? String.Empty; }
    set { ViewState["SelectedCategoryName"] = value; }
}

In case of value-types this approach will not work: you will get NullReferenceException. But do not worry — in this case we can use Nullable-types. Before:

1
2
3
4
5
public int FirstWidth
{
    get { return ViewState["FirstWidth"] != null ? (int) ViewState["FirstWidth"] : 0; }
    set { ViewState["FirstWidth"] = value; }
}

After:

1
2
3
4
5
public int FirstWidth
{
    get { return (int?) ViewState["FirstWidth"] ?? 0; }
    set { ViewState["FirstWidth"] = value; }
}

When you are using ?? operator, expression will be evaluated in the left to right order, so you can use something like this:

1
string name = FirstName ?? LastName ?? "Anonymous";

Of course, it looks more readably than the:

1
2
3
4
5
6
7
string name;
if (FirstName != null)
    name = FirstName;
else if (LastName != null)
    name = LastName;
else
    name = "Anonymous";

And a fortiori than:

1
2
3
string name = FirstName != null
        ? FirstName
        : (LastName != null ? LastName : "Anonymous");

Add it to your armoury!

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/feed/ 18